-
Notifications
You must be signed in to change notification settings - Fork 212
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 6e87363
Showing
13 changed files
with
192 additions
and
14 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,37 @@ | ||
'use server'; | ||
|
||
import { SubmissionResult } from '@conform-to/react'; | ||
import { parseWithZod } from '@conform-to/zod'; | ||
import { revalidatePath } from 'next/cache'; | ||
import { cookies } from 'next/headers'; | ||
import { getTranslations } from 'next-intl/server'; | ||
import { z } from 'zod'; | ||
|
||
const currencySchema = z.object({ | ||
code: z.string().length(3).toUpperCase(), | ||
}); | ||
|
||
export const switchCurrency = async (_prevState: SubmissionResult | null, payload: FormData) => { | ||
const t = await getTranslations('Components.Header.Currency'); | ||
|
||
const submission = parseWithZod(payload, { schema: currencySchema }); | ||
|
||
if (submission.status !== 'success') { | ||
return submission.reply({ formErrors: [t('invalidCurrency')] }); | ||
} | ||
|
||
cookies().set({ | ||
name: 'currencyCode', | ||
value: submission.value.code, | ||
httpOnly: true, | ||
secure: process.env.NODE_ENV === 'production', | ||
sameSite: 'lax', | ||
path: '/', | ||
}); | ||
|
||
await Promise.resolve(); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,5 +29,12 @@ export const HeaderFragment = graphql(` | |
} | ||
} | ||
} | ||
currencies(first: 10) { | ||
edges { | ||
node { | ||
code | ||
} | ||
} | ||
} | ||
} | ||
`); |
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,15 @@ | ||
'use server'; | ||
|
||
import { cookies } from "next/headers"; | ||
|
||
export async function getPreferredCurrencyCode(): Promise<string | undefined> { | ||
const cookieStore = await cookies(); | ||
const currencyCode = cookieStore.get('currencyCode')?.value; | ||
|
||
return currencyCode; | ||
} | ||
|
||
export async function setPreferredCurrencyCode(currencyCode: string): Promise<void> { | ||
const cookieStore = await cookies(); | ||
cookieStore.set('currencyCode', currencyCode); | ||
} |
Oops, something went wrong.