-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add customer & address form fields queries
- Loading branch information
1 parent
195764f
commit f352de8
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-client": patch | ||
--- | ||
|
||
add customer & address form fields queries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { graphql } from '../graphql'; | ||
|
||
export const FORM_FIELDS_FRAGMENT = graphql(` | ||
fragment FormFields on FormField { | ||
entityId | ||
label | ||
sortOrder | ||
isBuiltIn | ||
isRequired | ||
__typename | ||
... on CheckboxesFormField { | ||
options { | ||
entityId | ||
label | ||
} | ||
} | ||
... on DateFormField { | ||
defaultDate | ||
minDate | ||
maxDate | ||
} | ||
... on MultilineTextFormField { | ||
defaultText | ||
rows | ||
} | ||
... on NumberFormField { | ||
defaultNumber | ||
maxLength | ||
minNumber | ||
maxNumber | ||
} | ||
... on PasswordFormField { | ||
defaultText | ||
maxLength | ||
} | ||
... on PicklistFormField { | ||
choosePrefix | ||
options { | ||
entityId | ||
label | ||
} | ||
} | ||
... on RadioButtonsFormField { | ||
options { | ||
entityId | ||
label | ||
} | ||
} | ||
... on TextFormField { | ||
defaultText | ||
maxLength | ||
} | ||
} | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { cache } from 'react'; | ||
|
||
import { client } from '..'; | ||
import { FORM_FIELDS_FRAGMENT } from '../fragments/form-fields'; | ||
import { graphql, VariablesOf } from '../graphql'; | ||
import { revalidate } from '../revalidate-target'; | ||
|
||
const GET_ADDRESS_FORM_FIELDS = graphql( | ||
` | ||
query getAddressFormFields($filters: FormFieldFiltersInput, $sortBy: FormFieldSortInput) { | ||
site { | ||
settings { | ||
formFields { | ||
shippingAddress(filters: $filters, sortBy: $sortBy) { | ||
...FormFields | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
[FORM_FIELDS_FRAGMENT], | ||
); | ||
|
||
type Variables = VariablesOf<typeof GET_ADDRESS_FORM_FIELDS>; | ||
|
||
interface AddressFormFields { | ||
filters?: Variables['filters']; | ||
sortBy?: Variables['sortBy']; | ||
} | ||
|
||
export const getAddressFormFields = cache(async ({ filters, sortBy }: AddressFormFields) => { | ||
const response = await client.fetch({ | ||
document: GET_ADDRESS_FORM_FIELDS, | ||
variables: { filters, sortBy }, | ||
fetchOptions: { next: { revalidate } }, | ||
}); | ||
|
||
const { settings } = response.data.site; | ||
|
||
if (!settings) { | ||
return null; | ||
} | ||
|
||
return settings.formFields.shippingAddress; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { cache } from 'react'; | ||
|
||
import { client } from '..'; | ||
import { FORM_FIELDS_FRAGMENT } from '../fragments/form-fields'; | ||
import { graphql, VariablesOf } from '../graphql'; | ||
import { revalidate } from '../revalidate-target'; | ||
|
||
const GET_CUSTOMER_FORM_FIELDS = graphql( | ||
` | ||
query getCustomerFormFields($filters: FormFieldFiltersInput, $sortBy: FormFieldSortInput) { | ||
site { | ||
settings { | ||
formFields { | ||
customer(filters: $filters, sortBy: $sortBy) { | ||
...FormFields | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
[FORM_FIELDS_FRAGMENT], | ||
); | ||
|
||
type Variables = VariablesOf<typeof GET_CUSTOMER_FORM_FIELDS>; | ||
|
||
interface CustomerFormFields { | ||
filters?: Variables['filters']; | ||
sortBy?: Variables['sortBy']; | ||
} | ||
|
||
export const getCustomerFormFields = cache(async ({ filters, sortBy }: CustomerFormFields) => { | ||
const response = await client.fetch({ | ||
document: GET_CUSTOMER_FORM_FIELDS, | ||
variables: { filters, sortBy }, | ||
fetchOptions: { next: { revalidate } }, | ||
}); | ||
|
||
const { settings } = response.data.site; | ||
|
||
if (!settings) { | ||
return null; | ||
} | ||
|
||
return settings.formFields.customer; | ||
}); |