Merge pull request #2084 from omnivore-app/feature/save-api-with-labels
Add labels and state to save api in web frontend
This commit is contained in:
@ -129,8 +129,12 @@ export const createLabels = async (
|
||||
})
|
||||
.getMany()
|
||||
|
||||
const existingLabels = labelEntities.map((l) => l.name)
|
||||
const newLabels = labels.filter((l) => !existingLabels.includes(l.name))
|
||||
const existingLabelsInLowerCase = labelEntities.map((l) =>
|
||||
l.name.toLowerCase()
|
||||
)
|
||||
const newLabels = labels.filter(
|
||||
(l) => !existingLabelsInLowerCase.includes(l.name.toLowerCase())
|
||||
)
|
||||
// create new labels
|
||||
const newLabelEntities = await getRepository(Label).save(
|
||||
newLabels.map((l) => ({
|
||||
|
||||
@ -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,11 +54,16 @@ export default async (
|
||||
res: NextApiResponse
|
||||
): Promise<void> => {
|
||||
const urlStr = req.query['url']
|
||||
if (req.query['labels'] && typeof req.query['labels'] === 'string') {
|
||||
req.query['labels'] = [req.query['labels']]
|
||||
}
|
||||
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())}`)
|
||||
if (saveResult) {
|
||||
res.redirect(`/article?url=${encodeURIComponent(url.toString())}`)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user