Skip to content

Commit

Permalink
finish query parameter passing & incremental cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rleed committed Sep 26, 2023
1 parent 0ec4135 commit ea8d479
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
19 changes: 14 additions & 5 deletions api/resolvers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -232,10 +244,7 @@ export default {
{
range:
{
createdAt: {
lte: decodedCursor.time,
gte: whenGte
}
createdAt: whenRange
}
},
{ range: { wvotes: { gt: -1 * ITEM_FILTER_THRESHOLD } } }
Expand Down
2 changes: 1 addition & 1 deletion api/typeDefs/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
Expand Down
9 changes: 4 additions & 5 deletions components/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 })

Expand All @@ -604,9 +603,9 @@ export function DatePicker ({ fromName, toName, noForm, onMount, ...props }) {
<ReactDatePicker
{...props}
onChange={([from, to], e) => {
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)
}}
/>
)
Expand Down
14 changes: 6 additions & 8 deletions components/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 (
<>
Expand Down Expand Up @@ -108,7 +106,7 @@ export default function Search ({ sub }) {
<Select
groupClassName='mb-0 ms-2'
onChange={(formik, e) => {
search({ ...formik?.values, when: e.target.value, from: from || dayMonthYear(new Date()), to: to || dayMonthYear(new Date()) })
search({ ...formik?.values, when: e.target.value, from: from || new Date().toISOString(), to: to || new Date().toISOString() })
setDatePicker(e.target.value === 'custom')
if (e.target.value === 'custom') setRange({ start: new Date(), end: new Date() })
}}
Expand All @@ -122,12 +120,12 @@ export default function Search ({ sub }) {
fromName='from' toName='to'
className='form-control ms-2 p-0 px-2'
onMount={() => {
setRange({ start: new Date(from + timepart), end: new Date(to + timepart) })
setRange({ start: new Date(from), end: new Date(to) })
return [from, to]
}}
onChange={(formik, [start, end], e) => {
setRange({ start, end })
search({ ...formik?.values, from: start && dayMonthYear(start), to: end && dayMonthYear(end) })
search({ ...formik?.values, from: start && start.toISOString(), to: end && end.toISOString() })
}}
selected={range.start}
startDate={range.start} endDate={range.end}
Expand Down
4 changes: 2 additions & 2 deletions fragments/subs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export const SUB_ITEMS = gql`
export const SUB_SEARCH = gql`
${SUB_FIELDS}
${ITEM_FULL_FIELDS}
query SubSearch($sub: String, $q: String, $cursor: String, $sort: String, $what: String, $when: String) {
query SubSearch($sub: String, $q: String, $cursor: String, $sort: String, $what: String, $when: String, $from: String, $to: String) {
sub(name: $sub) {
...SubFields
}
search(sub: $sub, q: $q, cursor: $cursor, sort: $sort, what: $what, when: $when) {
search(sub: $sub, q: $q, cursor: $cursor, sort: $sort, what: $what, when: $when, from: $from, to: $to) {
cursor
items {
...ItemFullFields
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"react-avatar-editor": "^13.0.0",
"react-bootstrap": "^2.8.0",
"react-countdown": "^2.3.5",
"react-datepicker": "^4.17.0",
"react-datepicker": "^4.18.0",
"react-dom": "^18.2.0",
"react-longpressable": "^1.1.1",
"react-markdown": "^8.0.7",
Expand Down Expand Up @@ -118,4 +118,4 @@
"eslint": "^8.47.0",
"standard": "^17.1.0"
}
}
}

0 comments on commit ea8d479

Please sign in to comment.