set locale in saveUrl api request on web

This commit is contained in:
Hongbo Wu
2023-07-13 16:00:49 +08:00
parent b56598c20b
commit df58198161
4 changed files with 14 additions and 14 deletions

View File

@ -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)
}}
>

View File

@ -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, {

View File

@ -21,7 +21,8 @@ export type SaveUrlData = {
export async function saveUrlMutation(
url: string,
timezone?: string
timezone?: string,
locale?: string
): Promise<SaveLinkOutput | undefined> {
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

View File

@ -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())}`)