Add labels and state to save api in web frontend

This commit is contained in:
Hongbo Wu 2023-04-19 15:53:30 +08:00
parent 1a5dfb5104
commit 4b85251c00

View file

@ -3,7 +3,12 @@ import { v4 as uuidv4 } from 'uuid'
import { SaveResponseData } from '../../lib/networking/mutations/saveUrlMutation'
import { ssrFetcher } from '../../lib/networking/networkHelpers'
const saveUrl = async (req: NextApiRequest, url: URL) => {
const saveUrl = async (
req: NextApiRequest,
url: URL,
labels: string[] | undefined,
state: string | undefined
) => {
const clientRequestId = uuidv4()
const mutation = `
mutation SaveUrl($input: SaveUrlInput!) {
@ -26,6 +31,8 @@ const saveUrl = async (req: NextApiRequest, url: URL) => {
clientRequestId,
url: url.toString(),
source: 'api-save-url',
labels: labels?.map((label) => ({ name: label })),
state,
},
})
@ -47,8 +54,10 @@ export default async (
res: NextApiResponse
): Promise<void> => {
const urlStr = req.query['url']
const labels = req.query['labels'] as string[] | undefined
const state = req.query['state'] as string | undefined
const url = new URL(urlStr as string)
const saveResult = await saveUrl(req, url)
const saveResult = await saveUrl(req, url, labels, state)
console.log('saveResult: ', saveResult)
if (saveResult?.url) {
res.redirect(`?url=${encodeURIComponent(url.toString())}`)