fix: wrong timezone used when saving item with adding link on web

This commit is contained in:
Hongbo Wu 2023-07-13 15:16:35 +08:00
parent 55a3f00a5e
commit b56598c20b
3 changed files with 16 additions and 6 deletions

View file

@ -20,9 +20,12 @@ 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) => {
const result = await saveUrlMutation(link)
async (link: string, timezone: string) => {
const result = await saveUrlMutation(link, timezone)
if (result) {
toast(
() => (
@ -95,7 +98,7 @@ export function AddLinkModal(props: AddLinkModalProps): JSX.Element {
setLink(newLink)
submitLink = newLink
}
handleLinkSubmission(submitLink)
handleLinkSubmission(submitLink, timezone)
props.onOpenChange(false)
}}
>

View file

@ -20,7 +20,8 @@ export type SaveUrlData = {
}
export async function saveUrlMutation(
url: string
url: string,
timezone?: string
): Promise<SaveLinkOutput | undefined> {
const clientRequestId = uuidv4()
const mutation = gql`
@ -44,6 +45,7 @@ export async function saveUrlMutation(
url,
clientRequestId,
source: 'add-link',
timezone,
},
})
const output = data as SaveResponseData | undefined

View file

@ -7,7 +7,8 @@ const saveUrl = async (
req: NextApiRequest,
url: URL,
labels: string[] | undefined,
state: string | undefined
state: string | undefined,
timezone?: string
) => {
const clientRequestId = uuidv4()
const mutation = `
@ -33,6 +34,7 @@ const saveUrl = async (
source: 'api-save-url',
labels: labels?.map((label) => ({ name: label })),
state,
timezone,
},
})
@ -60,7 +62,10 @@ 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)
const saveResult = await saveUrl(req, url, labels, state)
// get timezone from browser
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
const saveResult = await saveUrl(req, url, labels, state, timezone)
console.log('saveResult: ', saveResult)
if (saveResult) {
res.redirect(`/article?url=${encodeURIComponent(url.toString())}`)