From c80e4d107c6432d5f7d9d293d6ecb5f25770e09e Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 28 Oct 2021 15:49:51 -0500 Subject: [PATCH] dupe alerts on link posts --- api/resolvers/item.js | 9 +++++++++ api/typeDefs/item.js | 1 + components/link-form.js | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 340dae54..94cc9f77 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -165,6 +165,15 @@ export default { } catch (e) { return null } + }, + dupes: async (parent, { url }, { models }) => { + const urlObj = new URL(ensureProtocol(url)) + return await models.$queryRaw(` + ${SELECT} + FROM "Item" + WHERE url LIKE ($1 || '?%') OR url = $1 + ORDER BY created_at DESC + LIMIT 3`, urlObj.origin + urlObj.pathname) } }, diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js index 5200591a..dbd60152 100644 --- a/api/typeDefs/item.js +++ b/api/typeDefs/item.js @@ -6,6 +6,7 @@ export default gql` moreFlatComments(cursor: String, name: String!): Comments item(id: ID!): Item pageTitle(url: String!): String + dupes(url: String!): [Item!] } enum ItemAct { diff --git a/components/link-form.js b/components/link-form.js index 77750149..fd5deae4 100644 --- a/components/link-form.js +++ b/components/link-form.js @@ -6,6 +6,9 @@ import { ensureProtocol } from '../lib/url' import ActionTooltip from '../components/action-tooltip' import Countdown from './countdown' import AdvPostForm, { AdvPostInitial, AdvPostSchema } from './adv-post-form' +import { ITEM_FIELDS } from '../fragments/items' +import Item from './item' +import AccordianItem from './accordian-item' export const LinkSchema = Yup.object({ title: Yup.string().required('required').trim(), @@ -26,13 +29,24 @@ export const LinkSchema = Yup.object({ }) export function LinkForm ({ item, editThreshold }) { + const router = useRouter() + const [getPageTitle, { data }] = useLazyQuery(gql` query PageTitle($url: String!) { pageTitle(url: $url) }`, { fetchPolicy: 'network-only' }) - const router = useRouter() + const [getDupes, { data: dupesData }] = useLazyQuery(gql` + ${ITEM_FIELDS} + query Dupes($url: String!) { + dupes(url: $url) { + ...ItemFields + } + }`, { + fetchPolicy: 'network-only' + }) + const [createLink] = useMutation( gql` mutation createLink($title: String!, $url: String!, $boost: Int) { @@ -41,6 +55,7 @@ export function LinkForm ({ item, editThreshold }) { } }` ) + const [updateLink] = useMutation( gql` mutation updateLink($id: ID!, $title: String!, $url: String!) { @@ -66,6 +81,8 @@ export function LinkForm ({ item, editThreshold }) { } ) + console.log(dupesData) + return (
{!item && } - {item ? 'save' : 'post'} + {dupesData?.dupes?.length > 0 && +
+ dupes
} + body={ +
+ {dupesData.dupes.map((item, i) => ( + + ))} +
+ } + /> + } + ) }