diff --git a/framework/vendure/schema.d.ts b/framework/vendure/schema.d.ts index b2e0f198e..e2a6ab600 100644 --- a/framework/vendure/schema.d.ts +++ b/framework/vendure/schema.d.ts @@ -3052,10 +3052,10 @@ export type CartFragment = { __typename?: 'Order' } & Pick< | 'totalWithTax' | 'currencyCode' > & { - shippingAddress?: Maybe<{ __typename?: 'OrderAddress' } & Pick> + shippingAddress?: Maybe<{ __typename?: 'OrderAddress' } & Pick> discounts: Array< - { __typename?: 'Discount' } & Pick - > + { __typename?: 'Discount' } & Pick + > customer?: Maybe<{ __typename?: 'Customer' } & Pick> lines: Array< { __typename?: 'OrderLine' } & Pick< @@ -3166,10 +3166,10 @@ export type ApplyCouponCodeMutationVariables = Exact<{ export type ApplyCouponCodeMutation = { applyCouponCode: - | TestOrderFragmentFragment - | Pick - | Pick - | Pick; + | TestOrderFragmentFragment + | Pick + | Pick + | Pick; }; export type ApplyCouponCodeMutation = { __typename?: 'Mutation' } & { @@ -3344,6 +3344,25 @@ type Favorite = Node & { customer: Customer! } +export type GetAvailableCountriesQueryVariables = Exact<{ [key: string]: never; }>; + + +// export type GetAvailableCountriesQuery = { countries: ( +// { __typename?: 'CountryList' } +// & { items: Array<( +// { __typename?: 'Country' } +// & Pick +// )> } +// ) }; + +export type GetAvailableCountriesQuery = { + availableCountries: + { __typename?: 'CountryList' } + & Array<( + { __typename?: 'Country' } + & Pick + )> +}; type FavouriteOption = Customer & { diff --git a/framework/vendure/utils/queries/available-countries-query.ts b/framework/vendure/utils/queries/available-countries-query.ts new file mode 100644 index 000000000..f1da8c408 --- /dev/null +++ b/framework/vendure/utils/queries/available-countries-query.ts @@ -0,0 +1,16 @@ +export const availableCountriesQuery = /* GraphQL */ ` +query availableCountriesQuery { + availableCountries { + ...Country + __typename + } +} + +fragment Country on Country { + id + code + name + enabled + __typename +} +` diff --git a/src/components/hooks/order/index.ts b/src/components/hooks/order/index.ts index bf6383122..7ec92f670 100644 --- a/src/components/hooks/order/index.ts +++ b/src/components/hooks/order/index.ts @@ -1,4 +1,5 @@ export { default as useSetCustomerForOrder } from './useSetCustomerForOrder' export { default as useSetOrderShippingAddress } from './useSetOrderShippingAddress' export { default as useApplyCouponCode } from './useApplyCouponCode' +export { default as useAvailableCountries } from './useAvailableCountries' diff --git a/src/components/hooks/order/useAvailableCountries.tsx b/src/components/hooks/order/useAvailableCountries.tsx new file mode 100644 index 000000000..51099965a --- /dev/null +++ b/src/components/hooks/order/useAvailableCountries.tsx @@ -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([availableCountriesQuery], gglFetcher) + return { + countries: data?.availableCountries, + loading: isValidating, + } +} + +export default useAvailableCountries diff --git a/src/components/modules/checkout/CheckoutInfo/components/ShippingInfoForm/ShippingInfoForm.tsx b/src/components/modules/checkout/CheckoutInfo/components/ShippingInfoForm/ShippingInfoForm.tsx index f1931d7fb..6331daf6d 100644 --- a/src/components/modules/checkout/CheckoutInfo/components/ShippingInfoForm/ShippingInfoForm.tsx +++ b/src/components/modules/checkout/CheckoutInfo/components/ShippingInfoForm/ShippingInfoForm.tsx @@ -2,7 +2,7 @@ import { Form, Formik } from 'formik' import React, { useEffect, useRef } from 'react' import { ButtonCommon, InputFiledInForm, SelectFieldInForm } from 'src/components/common' 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 { LANGUAGE } from 'src/utils/language.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 = [ { name: 'Hồ Chí Minh', @@ -37,12 +40,17 @@ const provinceOptions = [ name: 'Hà Nội', value: 'Hà Nội', }, + { + name: 'Sabah', + value: 'Sabah', + }, ] const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps) => { const addressRef = useRef(null) const { setOrderShippingAddress, loading } = useSetOrderShippingAddress() const { showMessageError } = useMessage() + const { countries } = useAvailableCountries() useEffect(() => { setTimeout(() => { @@ -74,9 +82,9 @@ const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps) { streetLine1: '', city: '', - province: '', + province: DEFAULT_PROVINCE, postalCode: '', - countryCode: '', + countryCode: DEFAULT_COUNTRY_CODE, phoneNumber: '', }} validationSchema={displayingErrorMessagesSchema} @@ -141,12 +149,11 @@ const ShippingInfoForm = ({ onConfirm, id, activeStep }: ShippingInfoFormProps)
)} +
)