From ea8d479fbeea90ba2d786cccf8739f914cb2c3e4 Mon Sep 17 00:00:00 2001 From: rleed Date: Tue, 26 Sep 2023 06:55:38 -0400 Subject: [PATCH] finish query parameter passing & incremental cleanup --- api/resolvers/search.js | 19 ++++++++++++++----- api/typeDefs/item.js | 2 +- components/form.js | 9 ++++----- components/search.js | 14 ++++++-------- fragments/subs.js | 4 ++-- package-lock.json | 14 +++++++------- package.json | 4 ++-- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/api/resolvers/search.js b/api/resolvers/search.js index be6e94b42e..42cac7f71d 100644 --- a/api/resolvers/search.js +++ b/api/resolvers/search.js @@ -79,7 +79,7 @@ export default { items } }, - search: async (parent, { q: query, sub, cursor, sort, what, when }, { me, models, search }) => { + search: async (parent, { q: query, sub, cursor, sort, what, when, from: whenFrom, to: whenTo }, { me, models, search }) => { const decodedCursor = decodeCursor(cursor) let sitems @@ -199,6 +199,18 @@ export default { break } + const whenRange = when === 'custom' + ? + { + gte: new Date(whenFrom), + lte: new Date(Math.min(new Date(whenTo), decodedCursor.time)) + } + : + { + lte: decodedCursor.time, + gte: whenGte + } + try { sitems = await search.search({ index: 'item', @@ -232,10 +244,7 @@ export default { { range: { - createdAt: { - lte: decodedCursor.time, - gte: whenGte - } + createdAt: whenRange } }, { range: { wvotes: { gt: -1 * ITEM_FILTER_THRESHOLD } } } diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js index 6a6fc51cb8..25be5ff2c4 100644 --- a/api/typeDefs/item.js +++ b/api/typeDefs/item.js @@ -7,7 +7,7 @@ export default gql` pageTitleAndUnshorted(url: String!): TitleUnshorted dupes(url: String!): [Item!] related(cursor: String, title: String, id: ID, minMatch: String, limit: Int): 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, from: String, to: String): Items auctionPosition(sub: String, id: ID, bid: Int!): Int! itemRepetition(parentId: ID): Int! } diff --git a/components/form.js b/components/form.js index 1841544e12..00a1588245 100644 --- a/components/form.js +++ b/components/form.js @@ -23,7 +23,6 @@ import { useInvoiceable } from './invoice' import { numWithUnits } from '../lib/format' import ReactDatePicker from 'react-datepicker' import 'react-datepicker/dist/react-datepicker.css' -import { dayMonthYear } from '../lib/time' export function SubmitButton ({ children, variant, value, onClick, disabled, cost, ...props @@ -588,7 +587,7 @@ export function Select ({ label, items, groupClassName, onChange, noForm, overri export function DatePicker ({ fromName, toName, noForm, onMount, ...props }) { const formik = noForm ? null : useFormikContext() - const onChangeSupplied = props.onChange + const onChangeHandler = props.onChange const [,, fromHelpers] = noForm ? [{}, {}, {}] : useField({ ...props, name: fromName }) const [,, toHelpers] = noForm ? [{}, {}, {}] : useField({ ...props, name: toName }) @@ -604,9 +603,9 @@ export function DatePicker ({ fromName, toName, noForm, onMount, ...props }) { { - fromHelpers.setValue(dayMonthYear(from)) - toHelpers.setValue(dayMonthYear(to)) - onChangeSupplied(formik, [from, to], e) + fromHelpers.setValue(from?.toISOString()) + toHelpers.setValue(to?.toISOString()) + onChangeHandler(formik, [from, to], e) }} /> ) diff --git a/components/search.js b/components/search.js index 890d699511..3e15f3f055 100644 --- a/components/search.js +++ b/components/search.js @@ -4,7 +4,6 @@ import SearchIcon from '../svgs/search-line.svg' import { useEffect, useRef, useState } from 'react' import { Form, Input, Select, DatePicker, SubmitButton } from './form' import { useRouter } from 'next/router' -import { dayMonthYear } from '../lib/time' export default function Search ({ sub }) { const router = useRouter() @@ -48,14 +47,13 @@ export default function Search ({ sub }) { const what = router.pathname.startsWith('/stackers') ? 'stackers' : router.query.what || 'all' const sort = router.query.sort || 'zaprank' const when = router.query.when || 'forever' - const from = router.query.from || dayMonthYear(new Date()) - const to = router.query.to || dayMonthYear(new Date()) + const from = router.query.from || new Date().toISOString() + const to = router.query.to || new Date().toISOString() const [datePicker, setDatePicker] = useState(when === 'custom') // The following state is needed for the date picker (and driven by the date picker). // Substituting router.query or formik values would cause network lag and/or timezone issues. - const timepart = 'T' + new Date().toISOString().split('T')[1] - const [range, setRange] = useState({ start: new Date(from + timepart), end: new Date(to + timepart) }) + const [range, setRange] = useState({ start: new Date(from), end: new Date(to) }) return ( <> @@ -108,7 +106,7 @@ export default function Search ({ sub }) {