Skip to content

Commit 4cdd9a6

Browse files
committed
feat: separate autofield select query into hooks for reuse
1 parent a21e7da commit 4cdd9a6

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

packages/react/src/react/components/compound/auto-field/index.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { type ReactNode, startTransition, useMemo } from 'react'
44
import { useFieldArray, useFormContext, useWatch } from 'react-hook-form'
55

66
import { EnvelopeIcon } from '@phosphor-icons/react'
7-
import { useQuery } from '@tanstack/react-query'
87

98
import {
109
BaseIcon,
@@ -34,8 +33,9 @@ import {
3433
useFormItemController,
3534
} from '@genseki/react'
3635

36+
import { useOptionsHelperQuery } from './query-options-helper'
37+
3738
import type {
38-
FieldOptionsCallbackReturn,
3939
FieldRelationShapeClient,
4040
FieldsClient,
4141
FieldShapeClient,
@@ -208,18 +208,10 @@ export function AutoSelectField(props: AutoSelectField) {
208208
control: form.control,
209209
})
210210

211-
const query = useQuery<{ status: 200; body: FieldOptionsCallbackReturn }>({
212-
queryKey: ['POST', props.optionsFetchPath, { pathParams: { name: props.optionsName } }],
213-
queryFn: async () => {
214-
const response = await fetch(`/api${props.optionsFetchPath}?name=${props.optionsName}`, {
215-
method: 'POST',
216-
body: JSON.stringify(value),
217-
headers: { 'Content-Type': 'application/json' },
218-
})
219-
if (!response.ok) throw new Error('Failed to fetch options')
220-
return response.json()
221-
},
222-
enabled: false,
211+
const { query } = useOptionsHelperQuery({
212+
optionsFetchPath: props.optionsFetchPath,
213+
optionsName: props.optionsName,
214+
queryFnBodyValue: value,
223215
})
224216

225217
useDebounce(value, () => query.refetch(), 500)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useQuery } from '@tanstack/react-query'
2+
3+
import type { FieldOptionsCallbackReturn } from '../../../../core/field'
4+
5+
export function useOptionsHelperQuery(props: {
6+
optionsFetchPath: string
7+
optionsName: string
8+
queryFnBodyValue?: any
9+
retryCount?: number
10+
}) {
11+
const query = useQuery<{ status: 200; body: FieldOptionsCallbackReturn }>({
12+
queryKey: ['POST', props.optionsFetchPath, { pathParams: { name: props.optionsName } }],
13+
queryFn: async () => {
14+
const response = await fetch(`/api${props.optionsFetchPath}?name=${props.optionsName}`, {
15+
method: 'POST',
16+
body: props.queryFnBodyValue ? JSON.stringify(props.queryFnBodyValue) : undefined,
17+
headers: { 'Content-Type': 'application/json' },
18+
})
19+
if (!response.ok) throw new Error('Failed to fetch options')
20+
return response.json()
21+
},
22+
enabled: false,
23+
retry: props.retryCount, // defaults is 3
24+
})
25+
return { query }
26+
}

packages/react/src/react/views/collections/list/toolbar/components/filter/components/filter-options.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { useState } from 'react'
22

3-
import { useQuery } from '@tanstack/react-query'
4-
53
import { BaseFilterBox, type BaseFilterBoxInterface } from './base'
64

7-
import type { FieldOptionsCallbackReturn } from '../../../../../../../../core/field'
85
import {
96
Label,
107
Select,
@@ -13,6 +10,7 @@ import {
1310
SelectOption,
1411
SelectTrigger,
1512
} from '../../../../../../../components'
13+
import { useOptionsHelperQuery } from '../../../../../../../components/compound/auto-field/query-options-helper'
1614
import { useDebounce } from '../../../../../../../hooks/use-debounce'
1715

1816
interface FilterOptionsInterface extends BaseFilterBoxInterface {
@@ -29,19 +27,10 @@ export function FilterOptions(props: FilterOptionsInterface) {
2927

3028
const optionKey = `${optionsFetchPath} - ${optionsName}`
3129

32-
const query = useQuery<{ status: 200; body: FieldOptionsCallbackReturn }>({
33-
queryKey: ['POST', optionsFetchPath, { pathParams: { name: optionsName } }],
34-
queryFn: async () => {
35-
const response = await fetch(`/api/${optionsFetchPath}?name=${optionsName}`, {
36-
method: 'POST',
37-
// body: JSON.stringify(value),
38-
headers: { 'Content-Type': 'application/json' },
39-
})
40-
if (!response.ok) throw new Error('Failed to fetch options')
41-
return response.json()
42-
},
43-
enabled: false,
44-
retry: 2,
30+
const { query } = useOptionsHelperQuery({
31+
optionsFetchPath: optionsFetchPath,
32+
optionsName: optionsName,
33+
retryCount: 2,
4534
})
4635

4736
useDebounce(

packages/react/src/react/views/collections/list/toolbar/components/filter/panel-content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export function CollectionListFilterPanel(props: CollectionListFilterPanelProps)
215215
handleRemoveChosenFilter(filterField.fieldShape)
216216
}}
217217
label={fieldLabel}
218-
optionsFetchPath={`${props.slug}/create/options`}
218+
optionsFetchPath={`/${props.slug}/create/options`}
219219
optionsName={filterField.options}
220220
/>
221221
)

0 commit comments

Comments
 (0)