diff --git a/src/app/favicon.ico b/src/app/favicon.ico index a00646c..27e861b 100644 Binary files a/src/app/favicon.ico and b/src/app/favicon.ico differ diff --git a/src/modules/environment/components/environment.tsx b/src/modules/environment/components/environment.tsx index 132053c..febaf1b 100644 --- a/src/modules/environment/components/environment.tsx +++ b/src/modules/environment/components/environment.tsx @@ -17,7 +17,6 @@ import { } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { toast } from "@/components/ui/use-toast"; -import { env } from "@/env"; import { type FragmentOf } from "@/graphql/gql"; import { envUrlSchema } from "@/lib/env-url"; import { clearIdempotencyKey } from "@/lib/idempotency-key"; @@ -25,6 +24,12 @@ import { clearIdempotencyKey } from "@/lib/idempotency-key"; import { fetchProduct } from "../actions/fetch-product"; import { ProductFragment } from "../fragments"; import { Cart } from "./cart"; +import { + resolveChannelSlug, + resolveEnvUrl, + saveChannelSlugToLocalStorage, + saveEnvUrlToLocalStorage, +} from "./storage-helpers"; const EnvironmentConfigSchema = z.object({ url: envUrlSchema, @@ -33,22 +38,6 @@ const EnvironmentConfigSchema = z.object({ type EnvironmentConfigSchemaType = z.infer; -const lsKey = "saleorApiUrl"; - -const getEnvFromLs = () => { - try { - return localStorage.getItem(lsKey) ?? ""; - } catch (e) { - return ""; - } -}; - -const saveEnvToLs = (env: string) => { - try { - localStorage.setItem(lsKey, env); - } catch (e) {} -}; - export const Environment = () => { const [products, setProducts] = useState< FragmentOf[] @@ -57,14 +46,14 @@ export const Environment = () => { const form = useForm({ resolver: zodResolver(EnvironmentConfigSchema), defaultValues: { - url: getEnvFromLs(), - channelSlug: env.NEXT_PUBLIC_INITIAL_CHANNEL_SLUG, + url: resolveEnvUrl(), + channelSlug: resolveChannelSlug(), }, }); const onSubmit = async (data: EnvironmentConfigSchemaType) => { - saveEnvToLs(data.url); - + saveEnvUrlToLocalStorage(data.url); + saveChannelSlugToLocalStorage(data.channelSlug); const response = await fetchProduct({ channelSlug: data.channelSlug, envUrl: data.url, diff --git a/src/modules/environment/components/storage-helpers.ts b/src/modules/environment/components/storage-helpers.ts new file mode 100644 index 0000000..03e91a3 --- /dev/null +++ b/src/modules/environment/components/storage-helpers.ts @@ -0,0 +1,39 @@ +import { env } from "@/env"; + +const localStorageEnvKey = "saleorApiUrl"; + +export const resolveEnvUrl = () => { + try { + return ( + localStorage.getItem(localStorageEnvKey) ?? + env.NEXT_PUBLIC_INITIAL_ENV_URL + ); + } catch (e) { + return env.NEXT_PUBLIC_INITIAL_ENV_URL; + } +}; + +export const saveEnvUrlToLocalStorage = (env: string) => { + try { + localStorage.setItem(localStorageEnvKey, env); + } catch (e) {} +}; + +const localStorageChannelKey = "saleorChannelSlug"; + +export const resolveChannelSlug = () => { + try { + return ( + localStorage.getItem(localStorageChannelKey) ?? + env.NEXT_PUBLIC_INITIAL_CHANNEL_SLUG + ); + } catch (e) { + return env.NEXT_PUBLIC_INITIAL_CHANNEL_SLUG; + } +}; + +export const saveChannelSlugToLocalStorage = (channelSlug: string) => { + try { + localStorage.setItem(localStorageChannelKey, channelSlug); + } catch (e) {} +}; diff --git a/src/modules/stripe/components/stripe-checkout-form.tsx b/src/modules/stripe/components/stripe-checkout-form.tsx index 8ab2642..dd4fb05 100644 --- a/src/modules/stripe/components/stripe-checkout-form.tsx +++ b/src/modules/stripe/components/stripe-checkout-form.tsx @@ -54,7 +54,6 @@ export const StripeCheckoutFormWrapped = (props: { const stripe = useStripe(); const elements = useElements(); const [loading, setLoading] = useState(false); - const [paymentMethod, setPaymentMethod] = useState(null); const handleSubmit = async (event: any) => { if (!stripe) { @@ -68,7 +67,8 @@ export const StripeCheckoutFormWrapped = (props: { } // Trigger form validation and wallet collection - const { error: submitError } = await elements.submit(); + const { error: submitError, selectedPaymentMethod } = + await elements.submit(); if (submitError) { toast({ @@ -81,7 +81,7 @@ export const StripeCheckoutFormWrapped = (props: { setLoading(true); - if (!paymentMethod) { + if (!selectedPaymentMethod) { setLoading(false); toast({ variant: "destructive", @@ -99,7 +99,7 @@ export const StripeCheckoutFormWrapped = (props: { idempotencyKey: getIdempotencyKey(), data: { paymentIntent: { - paymentMethod, + paymentMethod: selectedPaymentMethod, }, }, }); @@ -168,11 +168,7 @@ export const StripeCheckoutFormWrapped = (props: { return (
- { - setPaymentMethod(event.value.type); - }} - /> +