Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

PWA share target implementation #129

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,55 @@

import {getFullBaseUrl} from './helpers/getFullBaseUrl'

import {useTaskStore} from '@/stores/tasks'
import {useAuthStore} from '@/stores/auth'

declare let self: ServiceWorkerGlobalScope

const fullBaseUrl = getFullBaseUrl()
const authStore = useAuthStore()
RoboMagus marked this conversation as resolved.
Show resolved Hide resolved
const taskStore = useTaskStore()
const workboxVersion = 'v7.0.0'


const shareTargetHandler = async ({event}) => {
console.log('share-target() ', event)

// Form extraction works fine...
const formData = await event.request.formData()
console.log('FormData:() ', formData)
const title = formData.get('name')
const description = formData.get('description')

console.log('shareTargetHandler(), title: ', title)
console.log('shareTargetHandler(), description: ', description)

const defaultProjectId = authStore?.settings?.defaultProjectId
console.log('shareTargetHandler(), defaultProjectId: ', defaultProjectId)

if (defaultProjectId) {
const task = await taskStore.createNewTask({
title,
projectId: defaultProjectId,
})

if (description) {
task.description = description
}

console.log('Created task with ID: ', task.id)

// After Task creation succeeds, redirect to show the task.
const redirectionUrl = `${fullBaseUrl}tasks/${task.id}`
return Response.redirect(redirectionUrl, 303)
}
else {
// ToDo: improve handling of undefined default projectID
const redirectionUrl = `${fullBaseUrl}unspecified_default_project_id`
return Response.redirect(redirectionUrl, 303)
}
}

importScripts(`${fullBaseUrl}workbox-${workboxVersion}/workbox-sw.js`)
workbox.setConfig({
modulePathPrefix: `${fullBaseUrl}workbox-${workboxVersion}`,
Expand All @@ -30,6 +74,12 @@ workbox.routing.registerRoute(
new workbox.strategies.NetworkOnly(),
)

workbox.routing.registerRoute(
'/_share-target',
shareTargetHandler,
'POST',
)

// This code listens for the user's confirmation to update the app.
self.addEventListener('message', (e) => {
if (!e.data) {
Expand Down
9 changes: 9 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ export default defineConfig(({mode}) => {
url: '/teams',
},
],
share_target: {
action: '/_share-target',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's a route, why not use the vue routing system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit is required for the PWA manifest to register the app as a share-target

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kolaente, I did change the share_target to use the GET method, and now with the processing added to index.ts everthing seems to work as I intended :)

I'll remove the draft status as its ready for review.

enctype: 'multipart/form-data',
method: 'POST',
params: {
title: 'name',
text: 'description',
},
},
},
}),
viteSentry(getSentryConfig(env)),
Expand Down