add similar section to posts
This commit is contained in:
parent
9823969418
commit
ed153b5199
@ -18,7 +18,7 @@ const STOP_WORDS = ['a', 'an', 'and', 'are', 'as', 'at', 'be', 'but',
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
Query: {
|
Query: {
|
||||||
related: async (parent, { title, id, cursor, limit }, { me, models, search }) => {
|
related: async (parent, { title, id, cursor, limit, minMatch }, { me, models, search }) => {
|
||||||
const decodedCursor = decodeCursor(cursor)
|
const decodedCursor = decodeCursor(cursor)
|
||||||
if (!title || title.trim().split(/\s+/).length < 1) {
|
if (!title || title.trim().split(/\s+/).length < 1) {
|
||||||
if (id) {
|
if (id) {
|
||||||
@ -45,7 +45,7 @@ export default {
|
|||||||
body: {
|
body: {
|
||||||
query: {
|
query: {
|
||||||
bool: {
|
bool: {
|
||||||
should: [
|
must: [
|
||||||
{
|
{
|
||||||
more_like_this: {
|
more_like_this: {
|
||||||
fields: ['title'],
|
fields: ['title'],
|
||||||
@ -54,14 +54,14 @@ export default {
|
|||||||
min_doc_freq: 1,
|
min_doc_freq: 1,
|
||||||
min_word_length: 2,
|
min_word_length: 2,
|
||||||
max_query_terms: 25,
|
max_query_terms: 25,
|
||||||
minimum_should_match: '20%',
|
minimum_should_match: minMatch || '20%',
|
||||||
stop_words: STOP_WORDS
|
stop_words: STOP_WORDS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
must_not: [{ exists: { field: 'parentId' } }, ...mustNot],
|
must_not: [{ exists: { field: 'parentId' } }, ...mustNot],
|
||||||
filter: {
|
filter: {
|
||||||
range: { wvotes: { gte: 0.2 } }
|
range: { wvotes: { gte: minMatch ? 0 : 0.2 } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -8,7 +8,7 @@ export default gql`
|
|||||||
comments(id: ID!, sort: String): [Item!]!
|
comments(id: ID!, sort: String): [Item!]!
|
||||||
pageTitleAndUnshorted(url: String!): TitleUnshorted
|
pageTitleAndUnshorted(url: String!): TitleUnshorted
|
||||||
dupes(url: String!): [Item!]
|
dupes(url: String!): [Item!]
|
||||||
related(cursor: String, title: String, id: ID, limit: Int): Items
|
related(cursor: String, title: String, id: ID, minMatch: String, limit: Int): Items
|
||||||
allItems(cursor: String): Items
|
allItems(cursor: String): Items
|
||||||
search(q: String, sub: String, cursor: String, what: String, sort: String, when: String): Items
|
search(q: String, sub: String, cursor: String, what: String, sort: String, when: String): Items
|
||||||
auctionPosition(sub: String, id: ID, bid: Int!): Int!
|
auctionPosition(sub: String, id: ID, bid: Int!): Int!
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
import { Form, Input, MarkdownInput, SubmitButton } from '../components/form'
|
import { Form, Input, MarkdownInput, SubmitButton } from '../components/form'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import * as Yup from 'yup'
|
import * as Yup from 'yup'
|
||||||
import { gql, useApolloClient, useMutation } from '@apollo/client'
|
import { gql, useApolloClient, useLazyQuery, useMutation } from '@apollo/client'
|
||||||
import TextareaAutosize from 'react-textarea-autosize'
|
import TextareaAutosize from 'react-textarea-autosize'
|
||||||
import Countdown from './countdown'
|
import Countdown from './countdown'
|
||||||
import AdvPostForm, { AdvPostInitial, AdvPostSchema } from './adv-post-form'
|
import AdvPostForm, { AdvPostInitial, AdvPostSchema } from './adv-post-form'
|
||||||
import { MAX_TITLE_LENGTH } from '../lib/constants'
|
import { MAX_TITLE_LENGTH } from '../lib/constants'
|
||||||
import FeeButton, { EditFeeButton } from './fee-button'
|
import FeeButton, { EditFeeButton } from './fee-button'
|
||||||
|
import { ITEM_FIELDS } from '../fragments/items'
|
||||||
|
import AccordianItem from './accordian-item'
|
||||||
|
import Item from './item'
|
||||||
|
|
||||||
export function DiscussionForm ({
|
export function DiscussionForm ({
|
||||||
item, editThreshold, titleLabel = 'title',
|
item, editThreshold, titleLabel = 'title',
|
||||||
@ -25,6 +28,18 @@ export function DiscussionForm ({
|
|||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const [getRelated, { data: relatedData }] = useLazyQuery(gql`
|
||||||
|
${ITEM_FIELDS}
|
||||||
|
query related($title: String!) {
|
||||||
|
related(title: $title, minMatch: "75%", limit: 3) {
|
||||||
|
items {
|
||||||
|
...ItemFields
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`, {
|
||||||
|
fetchPolicy: 'network-only'
|
||||||
|
})
|
||||||
|
|
||||||
const DiscussionSchema = Yup.object({
|
const DiscussionSchema = Yup.object({
|
||||||
title: Yup.string().required('required').trim()
|
title: Yup.string().required('required').trim()
|
||||||
.max(MAX_TITLE_LENGTH,
|
.max(MAX_TITLE_LENGTH,
|
||||||
@ -32,6 +47,8 @@ export function DiscussionForm ({
|
|||||||
...AdvPostSchema(client)
|
...AdvPostSchema(client)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const related = relatedData?.related?.items || []
|
||||||
|
|
||||||
// const cost = linkOrImg ? 10 : me?.freePosts ? 0 : 1
|
// const cost = linkOrImg ? 10 : me?.freePosts ? 0 : 1
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -65,6 +82,13 @@ export function DiscussionForm ({
|
|||||||
required
|
required
|
||||||
autoFocus
|
autoFocus
|
||||||
clear
|
clear
|
||||||
|
onChange={async (formik, e) => {
|
||||||
|
if (e.target.value) {
|
||||||
|
getRelated({
|
||||||
|
variables: { title: e.target.value }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<MarkdownInput
|
<MarkdownInput
|
||||||
topLevel
|
topLevel
|
||||||
@ -88,6 +112,18 @@ export function DiscussionForm ({
|
|||||||
ChildButton={SubmitButton} variant='secondary'
|
ChildButton={SubmitButton} variant='secondary'
|
||||||
/>}
|
/>}
|
||||||
</div>
|
</div>
|
||||||
|
<div className={`mt-3 ${related.length > 0 ? '' : 'invisible'}`}>
|
||||||
|
<AccordianItem
|
||||||
|
header={<div style={{ fontWeight: 'bold', fontSize: '92%' }}>similar</div>}
|
||||||
|
body={
|
||||||
|
<div>
|
||||||
|
{related.map((item, i) => (
|
||||||
|
<Item item={item} key={item.id} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</Form>
|
</Form>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,32 @@ export function LinkForm ({ item, editThreshold }) {
|
|||||||
}`, {
|
}`, {
|
||||||
fetchPolicy: 'network-only'
|
fetchPolicy: 'network-only'
|
||||||
})
|
})
|
||||||
|
const [getRelated, { data: relatedData }] = useLazyQuery(gql`
|
||||||
|
${ITEM_FIELDS}
|
||||||
|
query related($title: String!) {
|
||||||
|
related(title: $title, minMatch: "75%", limit: 3) {
|
||||||
|
items {
|
||||||
|
...ItemFields
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`, {
|
||||||
|
fetchPolicy: 'network-only'
|
||||||
|
})
|
||||||
|
|
||||||
|
const related = []
|
||||||
|
for (const item of relatedData?.related?.items || []) {
|
||||||
|
let found = false
|
||||||
|
for (const ditem of dupesData?.dupes || []) {
|
||||||
|
if (ditem.id === item.id) {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
related.push(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const [upsertLink] = useMutation(
|
const [upsertLink] = useMutation(
|
||||||
gql`
|
gql`
|
||||||
@ -80,6 +106,13 @@ export function LinkForm ({ item, editThreshold }) {
|
|||||||
overrideValue={data?.pageTitleAndUnshorted?.title}
|
overrideValue={data?.pageTitleAndUnshorted?.title}
|
||||||
required
|
required
|
||||||
clear
|
clear
|
||||||
|
onChange={async (formik, e) => {
|
||||||
|
if (e.target.value) {
|
||||||
|
getRelated({
|
||||||
|
variables: { title: e.target.value }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Input
|
<Input
|
||||||
label='url'
|
label='url'
|
||||||
@ -129,7 +162,18 @@ export function LinkForm ({ item, editThreshold }) {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</div>}
|
</div>}
|
||||||
|
<div className={`mt-3 ${related.length > 0 ? '' : 'invisible'}`}>
|
||||||
|
<AccordianItem
|
||||||
|
header={<div style={{ fontWeight: 'bold', fontSize: '92%' }}>similar</div>}
|
||||||
|
body={
|
||||||
|
<div>
|
||||||
|
{related.map((item, i) => (
|
||||||
|
<Item item={item} key={item.id} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</Form>
|
</Form>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user