Skip to content

Commit 2ea5686

Browse files
committed
refactor(search-route): Extract filter input
1 parent 66c4c7f commit 2ea5686

2 files changed

Lines changed: 38 additions & 39 deletions

File tree

src/app/routes/search/search-route.tsx

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useState } from "react"
1+
import { Dispatch, useEffect, useMemo, useState } from "react"
22

33
import { ContextInfo } from "components/ui/context-info"
44
import { FilterInput } from "components/ui/filter-input"
@@ -70,6 +70,42 @@ interface Filters {
7070
untilDate?: string
7171
}
7272

73+
const SearchFilters = ({ onChange }: { onChange: Dispatch<Filters> }) => (
74+
<FilterInput
75+
className="block"
76+
placeholder="Search for time entries"
77+
onChange={({ text, tags }) => {
78+
onChange({
79+
description: text,
80+
category: tags.category,
81+
fromDate: tags.from,
82+
untilDate: tags.until,
83+
})
84+
}}
85+
tagConfigs={{
86+
category: {},
87+
until: {
88+
validate: value => value.length < 4 || dateHelpers.isValid(value),
89+
format: value => {
90+
if (value.length < 4) return ""
91+
const isYear = /^\d{4}$/.test(value)
92+
const date = dateHelpers.parse(isYear ? `${value}-12-31` : value)
93+
return dateHelpers.stringify(date)
94+
},
95+
},
96+
from: {
97+
validate: value => value.length < 4 || dateHelpers.isValid(value),
98+
format: value => {
99+
if (value.length < 4) return ""
100+
const isYear = /^\d{4}$/.test(value)
101+
const date = dateHelpers.parse(isYear ? `${value}-01-01` : value)
102+
return dateHelpers.stringify(date)
103+
},
104+
},
105+
}}
106+
/>
107+
)
108+
73109
export const SearchRoute = () => {
74110
const raw = useAtomValue(timeEntriesData)
75111
const allFlat = useMemo(
@@ -131,43 +167,7 @@ export const SearchRoute = () => {
131167
return (
132168
<div className={cn(vstack({}), "h-full px-10 pt-6")}>
133169
<div className="mb-4">
134-
<FilterInput
135-
className="block"
136-
placeholder="Search for time entries"
137-
onChange={({ text, tags }) => {
138-
setFilter({
139-
description: text,
140-
category: tags.category,
141-
fromDate: tags.from,
142-
untilDate: tags.until,
143-
})
144-
}}
145-
tagConfigs={{
146-
category: {},
147-
until: {
148-
validate: value => value.length < 4 || dateHelpers.isValid(value),
149-
format: value => {
150-
if (value.length < 4) return ""
151-
const isYear = /^\d{4}$/.test(value)
152-
const date = dateHelpers.parse(
153-
isYear ? `${value}-12-31` : value
154-
)
155-
return dateHelpers.stringify(date)
156-
},
157-
},
158-
from: {
159-
validate: value => value.length < 4 || dateHelpers.isValid(value),
160-
format: value => {
161-
if (value.length < 4) return ""
162-
const isYear = /^\d{4}$/.test(value)
163-
const date = dateHelpers.parse(
164-
isYear ? `${value}-01-01` : value
165-
)
166-
return dateHelpers.stringify(date)
167-
},
168-
},
169-
}}
170-
/>
170+
<SearchFilters onChange={setFilter} />
171171
</div>
172172

173173
{filtered.length === 0 ? (
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from "./filter-input"
2-
export { type Filters } from "./parse-filter"

0 commit comments

Comments
 (0)