From df58198161a2f9ade01df5a34eae19efeb84a931 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 13 Jul 2023 16:00:49 +0800 Subject: [PATCH] set locale in saveUrl api request on web --- .../web/components/templates/homeFeed/AddLinkModal.tsx | 10 ++++------ packages/web/lib/dateFormatting.ts | 4 ++-- .../web/lib/networking/mutations/saveUrlMutation.ts | 4 +++- packages/web/pages/api/save.ts | 10 +++++----- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/web/components/templates/homeFeed/AddLinkModal.tsx b/packages/web/components/templates/homeFeed/AddLinkModal.tsx index 084e2ac43..20b8f300c 100644 --- a/packages/web/components/templates/homeFeed/AddLinkModal.tsx +++ b/packages/web/components/templates/homeFeed/AddLinkModal.tsx @@ -1,5 +1,6 @@ import { useCallback, useState } from 'react' import toast from 'react-hot-toast' +import { locale, timeZone } from '../../../lib/dateFormatting' import { saveUrlMutation } from '../../../lib/networking/mutations/saveUrlMutation' import { showErrorToast } from '../../../lib/toastHelpers' import { Button } from '../../elements/Button' @@ -20,12 +21,9 @@ type AddLinkModalProps = { export function AddLinkModal(props: AddLinkModalProps): JSX.Element { const [link, setLink] = useState('') - // get timezone from browser - const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone - const handleLinkSubmission = useCallback( - async (link: string, timezone: string) => { - const result = await saveUrlMutation(link, timezone) + async (link: string, timezone: string, locale: string) => { + const result = await saveUrlMutation(link, timezone, locale) if (result) { toast( () => ( @@ -98,7 +96,7 @@ export function AddLinkModal(props: AddLinkModalProps): JSX.Element { setLink(newLink) submitLink = newLink } - handleLinkSubmission(submitLink, timezone) + handleLinkSubmission(submitLink, timeZone, locale) props.onOpenChange(false) }} > diff --git a/packages/web/lib/dateFormatting.ts b/packages/web/lib/dateFormatting.ts index 64e91d917..e31384041 100644 --- a/packages/web/lib/dateFormatting.ts +++ b/packages/web/lib/dateFormatting.ts @@ -1,8 +1,8 @@ //https://github.com/you-dont-need/You-Dont-Need-Momentjs -const locale = Intl.DateTimeFormat().resolvedOptions().locale || 'en-US' +export const locale = Intl.DateTimeFormat().resolvedOptions().locale || 'en-US' // get the user's time zone -const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone +export const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone export function formattedLongDate(rawDate: string): string { return new Intl.DateTimeFormat(locale, { diff --git a/packages/web/lib/networking/mutations/saveUrlMutation.ts b/packages/web/lib/networking/mutations/saveUrlMutation.ts index 299d65c80..0f3880d15 100644 --- a/packages/web/lib/networking/mutations/saveUrlMutation.ts +++ b/packages/web/lib/networking/mutations/saveUrlMutation.ts @@ -21,7 +21,8 @@ export type SaveUrlData = { export async function saveUrlMutation( url: string, - timezone?: string + timezone?: string, + locale?: string ): Promise { const clientRequestId = uuidv4() const mutation = gql` @@ -46,6 +47,7 @@ export async function saveUrlMutation( clientRequestId, source: 'add-link', timezone, + locale, }, }) const output = data as SaveResponseData | undefined diff --git a/packages/web/pages/api/save.ts b/packages/web/pages/api/save.ts index 789a66b12..556b674d5 100644 --- a/packages/web/pages/api/save.ts +++ b/packages/web/pages/api/save.ts @@ -1,5 +1,6 @@ import type { NextApiRequest, NextApiResponse } from 'next' import { v4 as uuidv4 } from 'uuid' +import { locale, timeZone } from '../../lib/dateFormatting' import { SaveResponseData } from '../../lib/networking/mutations/saveUrlMutation' import { ssrFetcher } from '../../lib/networking/networkHelpers' @@ -8,7 +9,8 @@ const saveUrl = async ( url: URL, labels: string[] | undefined, state: string | undefined, - timezone?: string + timezone?: string, + locale?: string ) => { const clientRequestId = uuidv4() const mutation = ` @@ -35,6 +37,7 @@ const saveUrl = async ( labels: labels?.map((label) => ({ name: label })), state, timezone, + locale, }, }) @@ -62,10 +65,7 @@ export default async ( const labels = req.query['labels'] as string[] | undefined const state = req.query['state'] as string | undefined const url = new URL(urlStr as string) - // get timezone from browser - const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone - - const saveResult = await saveUrl(req, url, labels, state, timezone) + const saveResult = await saveUrl(req, url, labels, state, timeZone, locale) console.log('saveResult: ', saveResult) if (saveResult) { res.redirect(`/article?url=${encodeURIComponent(url.toString())}`)