-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
072dd18
commit 4a5c4d2
Showing
15 changed files
with
216 additions
and
16 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
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
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
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
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
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
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
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
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,31 @@ | ||
'use server'; | ||
|
||
import { SubmissionResult } from '@conform-to/react'; | ||
import { parseWithZod } from '@conform-to/zod'; | ||
import { revalidatePath } from 'next/cache'; | ||
import { getTranslations } from 'next-intl/server'; | ||
import { z } from 'zod'; | ||
|
||
import { setPreferredCurrencyCode } from '~/lib/currency'; | ||
|
||
import { currencyCodeSchema } from '../schema'; | ||
|
||
const currencySwitchSchema = z.object({ | ||
code: currencyCodeSchema, | ||
}); | ||
|
||
export const switchCurrency = async (_prevState: SubmissionResult | null, payload: FormData) => { | ||
const t = await getTranslations('Components.Header.Currency'); | ||
|
||
const submission = parseWithZod(payload, { schema: currencySwitchSchema }); | ||
|
||
if (submission.status !== 'success') { | ||
return submission.reply({ formErrors: [t('invalidCurrency')] }); | ||
} | ||
|
||
await setPreferredCurrencyCode(submission.value.code); | ||
|
||
revalidatePath('/'); | ||
|
||
return submission.reply({ resetForm: true }); | ||
}; |
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
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
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,11 @@ | ||
import { z } from 'zod'; | ||
|
||
import type { CurrencyCode } from './fragment'; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const currencyCodeSchema = z | ||
.string() | ||
.length(3) | ||
.toUpperCase() | ||
.refine((val) => /^[A-Z]{3}$/.test(val), { | ||
message: 'Must be a valid currency code', | ||
}) as z.ZodType<CurrencyCode>; |
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,30 @@ | ||
'use server'; | ||
|
||
import { cookies } from 'next/headers'; | ||
|
||
import type { CurrencyCode } from '~/components/header/fragment'; | ||
import { currencyCodeSchema } from '~/components/header/schema'; | ||
|
||
export async function getPreferredCurrencyCode(): Promise<CurrencyCode | undefined> { | ||
const cookieStore = await cookies(); | ||
const currencyCode = cookieStore.get('currencyCode')?.value; | ||
|
||
if (!currencyCode) { | ||
return undefined; | ||
} | ||
|
||
const result = currencyCodeSchema.safeParse(currencyCode); | ||
|
||
return result.success ? result.data : undefined; | ||
} | ||
|
||
export async function setPreferredCurrencyCode(currencyCode: CurrencyCode): Promise<void> { | ||
const cookieStore = await cookies(); | ||
|
||
cookieStore.set('currencyCode', currencyCode, { | ||
httpOnly: true, | ||
secure: process.env.NODE_ENV === 'production', | ||
sameSite: 'lax', | ||
path: '/', | ||
}); | ||
} |
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
Oops, something went wrong.