mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
✨ feat: useAvailableCountries
:%s
This commit is contained in:
33
framework/vendure/schema.d.ts
vendored
33
framework/vendure/schema.d.ts
vendored
@@ -3052,10 +3052,10 @@ export type CartFragment = { __typename?: 'Order' } & Pick<
|
|||||||
| 'totalWithTax'
|
| 'totalWithTax'
|
||||||
| 'currencyCode'
|
| 'currencyCode'
|
||||||
> & {
|
> & {
|
||||||
shippingAddress?: Maybe<{ __typename?: 'OrderAddress' } & Pick<OrderAddress, 'streetLine1' | 'fullName' | 'city' | 'province' | 'postalCode' |'countryCode' | 'phoneNumber'>>
|
shippingAddress?: Maybe<{ __typename?: 'OrderAddress' } & Pick<OrderAddress, 'streetLine1' | 'fullName' | 'city' | 'province' | 'postalCode' | 'countryCode' | 'phoneNumber'>>
|
||||||
discounts: Array<
|
discounts: Array<
|
||||||
{ __typename?: 'Discount' } & Pick<Discount, 'type' | 'description' | 'amount' | 'amountWithTax'>
|
{ __typename?: 'Discount' } & Pick<Discount, 'type' | 'description' | 'amount' | 'amountWithTax'>
|
||||||
>
|
>
|
||||||
customer?: Maybe<{ __typename?: 'Customer' } & Pick<Customer, 'id' | 'firstName' | 'lastName' | 'emailAddress'>>
|
customer?: Maybe<{ __typename?: 'Customer' } & Pick<Customer, 'id' | 'firstName' | 'lastName' | 'emailAddress'>>
|
||||||
lines: Array<
|
lines: Array<
|
||||||
{ __typename?: 'OrderLine' } & Pick<
|
{ __typename?: 'OrderLine' } & Pick<
|
||||||
@@ -3166,10 +3166,10 @@ export type ApplyCouponCodeMutationVariables = Exact<{
|
|||||||
|
|
||||||
export type ApplyCouponCodeMutation = {
|
export type ApplyCouponCodeMutation = {
|
||||||
applyCouponCode:
|
applyCouponCode:
|
||||||
| TestOrderFragmentFragment
|
| TestOrderFragmentFragment
|
||||||
| Pick<CouponCodeExpiredError, 'errorCode' | 'message'>
|
| Pick<CouponCodeExpiredError, 'errorCode' | 'message'>
|
||||||
| Pick<CouponCodeInvalidError, 'errorCode' | 'message'>
|
| Pick<CouponCodeInvalidError, 'errorCode' | 'message'>
|
||||||
| Pick<CouponCodeLimitError, 'errorCode' | 'message'>;
|
| Pick<CouponCodeLimitError, 'errorCode' | 'message'>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ApplyCouponCodeMutation = { __typename?: 'Mutation' } & {
|
export type ApplyCouponCodeMutation = { __typename?: 'Mutation' } & {
|
||||||
@@ -3344,6 +3344,25 @@ type Favorite = Node & {
|
|||||||
customer: Customer!
|
customer: Customer!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type GetAvailableCountriesQueryVariables = Exact<{ [key: string]: never; }>;
|
||||||
|
|
||||||
|
|
||||||
|
// export type GetAvailableCountriesQuery = { countries: (
|
||||||
|
// { __typename?: 'CountryList' }
|
||||||
|
// & { items: Array<(
|
||||||
|
// { __typename?: 'Country' }
|
||||||
|
// & Pick<Country, 'id' | 'code' | 'name' | 'enabled'>
|
||||||
|
// )> }
|
||||||
|
// ) };
|
||||||
|
|
||||||
|
export type GetAvailableCountriesQuery = {
|
||||||
|
availableCountries:
|
||||||
|
{ __typename?: 'CountryList' }
|
||||||
|
& Array<(
|
||||||
|
{ __typename?: 'Country' }
|
||||||
|
& Pick<Country, 'id' | 'code' | 'name' | 'enabled'>
|
||||||
|
)>
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
type FavouriteOption = Customer & {
|
type FavouriteOption = Customer & {
|
||||||
|
16
framework/vendure/utils/queries/available-countries-query.ts
Normal file
16
framework/vendure/utils/queries/available-countries-query.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
export const availableCountriesQuery = /* GraphQL */ `
|
||||||
|
query availableCountriesQuery {
|
||||||
|
availableCountries {
|
||||||
|
...Country
|
||||||
|
__typename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment Country on Country {
|
||||||
|
id
|
||||||
|
code
|
||||||
|
name
|
||||||
|
enabled
|
||||||
|
__typename
|
||||||
|
}
|
||||||
|
`
|
@@ -1,4 +1,5 @@
|
|||||||
export { default as useSetCustomerForOrder } from './useSetCustomerForOrder'
|
export { default as useSetCustomerForOrder } from './useSetCustomerForOrder'
|
||||||
export { default as useSetOrderShippingAddress } from './useSetOrderShippingAddress'
|
export { default as useSetOrderShippingAddress } from './useSetOrderShippingAddress'
|
||||||
export { default as useApplyCouponCode } from './useApplyCouponCode'
|
export { default as useApplyCouponCode } from './useApplyCouponCode'
|
||||||
|
export { default as useAvailableCountries } from './useAvailableCountries'
|
||||||
|
|
||||||
|
14
src/components/hooks/order/useAvailableCountries.tsx
Normal file
14
src/components/hooks/order/useAvailableCountries.tsx
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { GetAvailableCountriesQuery } from '@framework/schema'
|
||||||
|
import { availableCountriesQuery } from '@framework/utils/queries/available-countries-query'
|
||||||
|
import gglFetcher from 'src/utils/gglFetcher'
|
||||||
|
import useSWR from 'swr'
|
||||||
|
|
||||||
|
const useAvailableCountries = () => {
|
||||||
|
const { data, isValidating } = useSWR<GetAvailableCountriesQuery>([availableCountriesQuery], gglFetcher)
|
||||||
|
return {
|
||||||
|
countries: data?.availableCountries,
|
||||||
|
loading: isValidating,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useAvailableCountries
|
@@ -2,7 +2,7 @@ import { Form, Formik } from 'formik'
|
|||||||
import React, { useEffect, useRef } from 'react'
|
import React, { useEffect, useRef } from 'react'
|
||||||
import { ButtonCommon, InputFiledInForm, SelectFieldInForm } from 'src/components/common'
|
import { ButtonCommon, InputFiledInForm, SelectFieldInForm } from 'src/components/common'
|
||||||
import { useMessage } from 'src/components/contexts'
|
import { useMessage } from 'src/components/contexts'
|
||||||
import { useSetOrderShippingAddress } from 'src/components/hooks/order'
|
import { useAvailableCountries, useSetOrderShippingAddress } from 'src/components/hooks/order'
|
||||||
import { COUNTRY_CODE } from 'src/domains/data/countryCode'
|
import { COUNTRY_CODE } from 'src/domains/data/countryCode'
|
||||||
import { LANGUAGE } from 'src/utils/language.utils'
|
import { LANGUAGE } from 'src/utils/language.utils'
|
||||||
import { CustomInputCommon } from 'src/utils/type.utils'
|
import { CustomInputCommon } from 'src/utils/type.utils'
|
||||||
@@ -28,6 +28,9 @@ const displayingErrorMessagesSchema = Yup.object().shape({
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const DEFAULT_COUNTRY_CODE = 'MY'
|
||||||
|
const DEFAULT_PROVINCE = 'Sabah'
|
||||||
|
|
||||||
const provinceOptions = [
|
const provinceOptions = [
|
||||||
{
|
{
|
||||||
name: 'Hồ Chí Minh',
|
name: 'Hồ Chí Minh',
|
||||||
@@ -37,12 +40,17 @@ const provinceOptions = [
|
|||||||
name: 'Hà Nội',
|
name: 'Hà Nội',
|
||||||
value: 'Hà Nội',
|
value: 'Hà Nội',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Sabah',
|
||||||
|
value: 'Sabah',
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps) => {
|
const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps) => {
|
||||||
const addressRef = useRef<CustomInputCommon>(null)
|
const addressRef = useRef<CustomInputCommon>(null)
|
||||||
const { setOrderShippingAddress, loading } = useSetOrderShippingAddress()
|
const { setOrderShippingAddress, loading } = useSetOrderShippingAddress()
|
||||||
const { showMessageError } = useMessage()
|
const { showMessageError } = useMessage()
|
||||||
|
const { countries } = useAvailableCountries()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -74,9 +82,9 @@ const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps)
|
|||||||
{
|
{
|
||||||
streetLine1: '',
|
streetLine1: '',
|
||||||
city: '',
|
city: '',
|
||||||
province: '',
|
province: DEFAULT_PROVINCE,
|
||||||
postalCode: '',
|
postalCode: '',
|
||||||
countryCode: '',
|
countryCode: DEFAULT_COUNTRY_CODE,
|
||||||
phoneNumber: '',
|
phoneNumber: '',
|
||||||
}}
|
}}
|
||||||
validationSchema={displayingErrorMessagesSchema}
|
validationSchema={displayingErrorMessagesSchema}
|
||||||
@@ -141,12 +149,11 @@ const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps)
|
|||||||
</div>
|
</div>
|
||||||
<div className={s.input}>
|
<div className={s.input}>
|
||||||
<SelectFieldInForm
|
<SelectFieldInForm
|
||||||
options={COUNTRY_CODE}
|
options={countries || []}
|
||||||
keyNameOption={['name', 'alpha-2']}
|
keyNameOption={['name']}
|
||||||
keyValueOption="alpha-2"
|
keyValueOption="code"
|
||||||
name="countryCode"
|
name="countryCode"
|
||||||
placeholder="Country"
|
placeholder="Country"
|
||||||
nameSeperator=" - "
|
|
||||||
error={
|
error={
|
||||||
touched.countryCode && errors.countryCode
|
touched.countryCode && errors.countryCode
|
||||||
? errors.countryCode.toString()
|
? errors.countryCode.toString()
|
||||||
@@ -179,6 +186,7 @@ const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps)
|
|||||||
</Form>
|
</Form>
|
||||||
)}
|
)}
|
||||||
</Formik>
|
</Formik>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user