From e6438b367c6949d3343183ea09ed7b38e124c88a Mon Sep 17 00:00:00 2001 From: thanhdanh27600 Date: Mon, 28 Aug 2023 22:14:53 +0700 Subject: [PATCH] update og image --- public/locales/en/common.json | 3 +- public/locales/vi/common.json | 3 +- src/api/requests.ts | 8 +++- .../gadgets/AdvancedSettingUrlForm.tsx | 45 ++++++++++++++----- src/components/gadgets/FacebookPreview.tsx | 10 +++-- src/components/gadgets/TwitterPreview.tsx | 8 ++-- .../sections/URLAdvancedSetting.tsx | 4 +- src/controllers/forward.ts | 18 ++++++-- src/controllers/og.ts | 3 +- src/controllers/shorten/update.ts | 10 ++++- src/pages/[...hash].tsx | 21 ++++++--- src/types/constants.ts | 4 +- src/utils/validateMiddleware.ts | 3 ++ 13 files changed, 101 insertions(+), 39 deletions(-) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index f6e0f90a..ad0b9041 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -56,5 +56,6 @@ "advancedView": "Advanced info", "loadMore": "Load more", "editPreview": "More options", - "save": "Save" + "save": "Save", + "maximumCharaters": "Maximum of {{n}} characters" } diff --git a/public/locales/vi/common.json b/public/locales/vi/common.json index 417bd51d..2e88e9d9 100644 --- a/public/locales/vi/common.json +++ b/public/locales/vi/common.json @@ -56,5 +56,6 @@ "advancedView": "Xem nâng cao", "loadMore": "Xem thêm", "editPreview": "Chỉnh sửa", - "save": "Lưu thay đổi" + "save": "Lưu thay đổi", + "maximumCharaters": "Tối đa {{n}} ký tự" } diff --git a/src/api/requests.ts b/src/api/requests.ts index 890fe744..69325252 100644 --- a/src/api/requests.ts +++ b/src/api/requests.ts @@ -1,6 +1,7 @@ import memoize from 'fast-memoize'; import { stringify } from 'querystring'; import { Forward } from 'types/forward'; +import { Locale } from 'types/locale'; import { QR } from 'types/qr'; import { ShortenUrl } from 'types/shorten'; import { Stats } from 'types/stats'; @@ -16,12 +17,15 @@ export const updateShortenUrlRequest = async ({ hash, ogTitle, ogDescription, + locale, }: { hash: string; ogDescription?: string; ogTitle?: string; + locale: Locale; }) => { const rs = await API.put(`/api/shorten/update`, { + locale, hash, ogTitle, ogDescription, @@ -32,16 +36,18 @@ export const updateShortenUrlRequest = async ({ export const getForwardUrl = async ({ hash, + locale, userAgent, ip, fromClientSide, }: { hash: string; + locale: string; userAgent?: string; ip?: string | null; fromClientSide?: boolean; }) => { - const rs = await API.post(`/api/forward`, { hash, userAgent, ip, fromClientSide }); + const rs = await API.post(`/api/forward`, { hash, locale, userAgent, ip, fromClientSide }); const data = rs.data; return data as Forward; }; diff --git a/src/components/gadgets/AdvancedSettingUrlForm.tsx b/src/components/gadgets/AdvancedSettingUrlForm.tsx index cce2e486..dcc11727 100644 --- a/src/components/gadgets/AdvancedSettingUrlForm.tsx +++ b/src/components/gadgets/AdvancedSettingUrlForm.tsx @@ -10,23 +10,26 @@ import { SubmitHandler, useForm } from 'react-hook-form'; import { toast } from 'react-hot-toast'; import { useMutation } from 'react-query'; import { LIMIT_OG_DESCRIPTION_LENGTH, LIMIT_OG_TITLE_LENGTH, brandUrlShortDomain } from 'types/constants'; +import { Locale } from 'types/locale'; import { MIXPANEL_EVENT, MIXPANEL_STATUS } from 'types/utils'; import { debounce } from 'utils/data'; import { useTrans } from 'utils/i18next'; import { logger } from 'utils/logger'; import { QueryKey } from 'utils/requests'; +type ShortenSettingPayload = Partial & { locale?: Locale }; + export const AdvancedSettingUrlForm = () => { - const { t } = useTrans(); + const { t, locale } = useTrans(); const { shortenSlice } = useBearStore(); const [shortenHistory, setShortenHistoryForm] = shortenSlice((state) => [ state.shortenHistory, state.setShortenHistoryForm, ]); const defaultValues = { - ogTitle: shortenHistory?.ogTitle ?? t('ogTitle', { hash: shortenHistory?.hash ?? 'XXX' }), - ogDescription: shortenHistory?.ogDescription ?? t('ogDescription'), - ogDomain: shortenHistory?.ogDomain ?? brandUrlShortDomain, + ogTitle: shortenHistory?.ogTitle || t('ogTitle', { hash: shortenHistory?.hash ?? 'XXX' }), + ogDescription: shortenHistory?.ogDescription || t('ogDescription'), + ogDomain: shortenHistory?.ogDomain || brandUrlShortDomain, ogImgSrc: shortenHistory?.ogImgSrc, }; @@ -35,11 +38,11 @@ export const AdvancedSettingUrlForm = () => { handleSubmit, formState: { errors }, watch, - } = useForm>({ + } = useForm({ defaultValues, }); - const handleUpdate = useCallback((history: Partial) => { + const handleUpdate = useCallback((history: ShortenSettingPayload) => { setShortenHistoryForm(history); }, []); @@ -49,11 +52,18 @@ export const AdvancedSettingUrlForm = () => { useEffect(() => { debouncedUpdate({ - ...shortenHistory, ogTitle, ogDescription, }); - }, [ogTitle, ogDescription, shortenHistory]); + }, [ogTitle, ogDescription]); + + useEffect(() => { + if (shortenHistory) + debouncedUpdate({ + ogTitle: shortenHistory.ogDescription || t('ogTitle', { hash: shortenHistory.hash ?? 'XXX' }), + ogDescription: shortenHistory.ogDescription || t('ogDescription'), + }); + }, [shortenHistory]); const updateShortenUrl = useMutation(QueryKey.SHORTEN_UPDATE, { mutationFn: updateShortenUrlRequest, @@ -72,10 +82,11 @@ export const AdvancedSettingUrlForm = () => { }, }); - const onSubmit: SubmitHandler> = async (values) => { + const onSubmit: SubmitHandler = async (values) => { if (!shortenHistory?.hash) return; updateShortenUrl.mutate({ hash: shortenHistory.hash, + locale, ogDescription: values.ogDescription || undefined, ogTitle: values.ogTitle || undefined, }); @@ -92,14 +103,15 @@ export const AdvancedSettingUrlForm = () => { btnSize="md" {...register('ogTitle', { required: { message: t('errorNoInput'), value: true }, - maxLength: { message: `Maximum of ${LIMIT_OG_TITLE_LENGTH} charactors`, value: LIMIT_OG_TITLE_LENGTH }, + maxLength: { message: t('maximumCharaters', { n: LIMIT_OG_TITLE_LENGTH }), value: LIMIT_OG_TITLE_LENGTH }, })} + maxLength={LIMIT_OG_TITLE_LENGTH} disabled={updateShortenUrl.isLoading} />

{error.message}

} + render={(error) =>

{error.message}

} />
@@ -107,11 +119,20 @@ export const AdvancedSettingUrlForm = () => {