Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add customer & address form fields queries #697

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-pens-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

add customer & address form fields queries
54 changes: 54 additions & 0 deletions apps/core/client/fragments/form-fields.ts
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
}
}
`);
46 changes: 46 additions & 0 deletions apps/core/client/queries/get-address-form-fields.ts
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;
});
46 changes: 46 additions & 0 deletions apps/core/client/queries/get-customer-form-fields.ts
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;
});
Loading