chore: use href for locale switching, warns user before leaving (#7215)
Opts to use links instead of router.replace when switching locales. The main benefit is now the user will be warned if they have changes and want to switch locales. Before it would switch locales and they would lose any unsaved changes in the locale they came from.
This commit is contained in:
@@ -43,7 +43,7 @@ export const Autosave: React.FC<Props> = ({
|
|||||||
} = useConfig()
|
} = useConfig()
|
||||||
const { docConfig, getVersions, versions } = useDocumentInfo()
|
const { docConfig, getVersions, versions } = useDocumentInfo()
|
||||||
const { reportUpdate } = useDocumentEvents()
|
const { reportUpdate } = useDocumentEvents()
|
||||||
const { dispatchFields, setSubmitted } = useForm()
|
const { dispatchFields, setModified, setSubmitted } = useForm()
|
||||||
const submitted = useFormSubmitted()
|
const submitted = useFormSubmitted()
|
||||||
const versionsConfig = docConfig?.versions
|
const versionsConfig = docConfig?.versions
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ export const Autosave: React.FC<Props> = ({
|
|||||||
// Store locale in ref so the autosave func
|
// Store locale in ref so the autosave func
|
||||||
// can always retrieve the most to date locale
|
// can always retrieve the most to date locale
|
||||||
localeRef.current = locale
|
localeRef.current = locale
|
||||||
|
console.log(modifiedRef.current, modified)
|
||||||
// When debounced fields change, autosave
|
// When debounced fields change, autosave
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const abortController = new AbortController()
|
const abortController = new AbortController()
|
||||||
@@ -131,6 +131,7 @@ export const Autosave: React.FC<Props> = ({
|
|||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
const newDate = new Date()
|
const newDate = new Date()
|
||||||
setLastSaved(newDate.getTime())
|
setLastSaved(newDate.getTime())
|
||||||
|
setModified(false)
|
||||||
reportUpdate({
|
reportUpdate({
|
||||||
id,
|
id,
|
||||||
entitySlug,
|
entitySlug,
|
||||||
@@ -216,6 +217,7 @@ export const Autosave: React.FC<Props> = ({
|
|||||||
reportUpdate,
|
reportUpdate,
|
||||||
serverURL,
|
serverURL,
|
||||||
setSubmitted,
|
setSubmitted,
|
||||||
|
setModified,
|
||||||
versionsConfig?.drafts,
|
versionsConfig?.drafts,
|
||||||
debouncedFields,
|
debouncedFields,
|
||||||
submitted,
|
submitted,
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import { getTranslation } from '@payloadcms/translations'
|
import { getTranslation } from '@payloadcms/translations'
|
||||||
import { useRouter } from 'next/navigation.js'
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
import { useConfig } from '../../providers/Config/index.js'
|
import { useConfig } from '../../providers/Config/index.js'
|
||||||
import { useLocale } from '../../providers/Locale/index.js'
|
import { useLocale } from '../../providers/Locale/index.js'
|
||||||
import { useRouteCache } from '../../providers/RouteCache/index.js'
|
|
||||||
import { useSearchParams } from '../../providers/SearchParams/index.js'
|
import { useSearchParams } from '../../providers/SearchParams/index.js'
|
||||||
import { useTranslation } from '../../providers/Translation/index.js'
|
import { useTranslation } from '../../providers/Translation/index.js'
|
||||||
import { Popup, PopupList } from '../Popup/index.js'
|
import { Popup, PopupList } from '../Popup/index.js'
|
||||||
@@ -24,8 +22,6 @@ export const Localizer: React.FC<{
|
|||||||
const { i18n } = useTranslation()
|
const { i18n } = useTranslation()
|
||||||
const locale = useLocale()
|
const locale = useLocale()
|
||||||
const { stringifyParams } = useSearchParams()
|
const { stringifyParams } = useSearchParams()
|
||||||
const router = useRouter()
|
|
||||||
const { clearRouteCache } = useRouteCache()
|
|
||||||
|
|
||||||
if (localization) {
|
if (localization) {
|
||||||
const { locales } = localization
|
const { locales } = localization
|
||||||
@@ -43,18 +39,13 @@ export const Localizer: React.FC<{
|
|||||||
return (
|
return (
|
||||||
<PopupList.Button
|
<PopupList.Button
|
||||||
active={locale.code === localeOption.code}
|
active={locale.code === localeOption.code}
|
||||||
key={localeOption.code}
|
href={stringifyParams({
|
||||||
onClick={() => {
|
|
||||||
router.replace(
|
|
||||||
stringifyParams({
|
|
||||||
params: {
|
params: {
|
||||||
locale: localeOption.code,
|
locale: localeOption.code,
|
||||||
},
|
},
|
||||||
}),
|
})}
|
||||||
)
|
key={localeOption.code}
|
||||||
clearRouteCache()
|
onClick={close}
|
||||||
close()
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{localeOptionLabel}
|
{localeOptionLabel}
|
||||||
{localeOptionLabel !== localeOption.code && ` (${localeOption.code})`}
|
{localeOptionLabel !== localeOption.code && ` (${localeOption.code})`}
|
||||||
|
|||||||
@@ -30,20 +30,20 @@ type LoginArgs = {
|
|||||||
const random = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
|
const random = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
|
||||||
|
|
||||||
const networkConditions = {
|
const networkConditions = {
|
||||||
'Slow 3G': {
|
|
||||||
download: ((500 * 1000) / 8) * 0.8,
|
|
||||||
upload: ((500 * 1000) / 8) * 0.8,
|
|
||||||
latency: 400 * 5,
|
|
||||||
},
|
|
||||||
'Fast 3G': {
|
'Fast 3G': {
|
||||||
download: ((1.6 * 1000 * 1000) / 8) * 0.9,
|
download: ((1.6 * 1000 * 1000) / 8) * 0.9,
|
||||||
upload: ((750 * 1000) / 8) * 0.9,
|
|
||||||
latency: 1000,
|
latency: 1000,
|
||||||
|
upload: ((750 * 1000) / 8) * 0.9,
|
||||||
|
},
|
||||||
|
'Slow 3G': {
|
||||||
|
download: ((500 * 1000) / 8) * 0.8,
|
||||||
|
latency: 400 * 5,
|
||||||
|
upload: ((500 * 1000) / 8) * 0.8,
|
||||||
},
|
},
|
||||||
'Slow 4G': {
|
'Slow 4G': {
|
||||||
download: ((4 * 1000 * 1000) / 8) * 0.8,
|
download: ((4 * 1000 * 1000) / 8) * 0.8,
|
||||||
upload: ((3 * 1000 * 1000) / 8) * 0.8,
|
|
||||||
latency: 1000,
|
latency: 1000,
|
||||||
|
upload: ((3 * 1000 * 1000) / 8) * 0.8,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,10 +53,10 @@ const networkConditions = {
|
|||||||
* @param serverURL
|
* @param serverURL
|
||||||
*/
|
*/
|
||||||
export async function ensureAutoLoginAndCompilationIsDone({
|
export async function ensureAutoLoginAndCompilationIsDone({
|
||||||
page,
|
|
||||||
serverURL,
|
|
||||||
customAdminRoutes,
|
customAdminRoutes,
|
||||||
customRoutes,
|
customRoutes,
|
||||||
|
page,
|
||||||
|
serverURL,
|
||||||
}: {
|
}: {
|
||||||
customAdminRoutes?: Config['admin']['routes']
|
customAdminRoutes?: Config['admin']['routes']
|
||||||
customRoutes?: Config['routes']
|
customRoutes?: Config['routes']
|
||||||
@@ -65,7 +65,7 @@ export async function ensureAutoLoginAndCompilationIsDone({
|
|||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
const {
|
const {
|
||||||
admin: {
|
admin: {
|
||||||
routes: { login: loginRoute, createFirstUser: createFirstUserRoute },
|
routes: { createFirstUser: createFirstUserRoute, login: loginRoute },
|
||||||
},
|
},
|
||||||
routes: { admin: adminRoute },
|
routes: { admin: adminRoute },
|
||||||
} = getAdminRoutes({ customAdminRoutes, customRoutes })
|
} = getAdminRoutes({ customAdminRoutes, customRoutes })
|
||||||
@@ -97,8 +97,8 @@ export async function ensureAutoLoginAndCompilationIsDone({
|
|||||||
*/
|
*/
|
||||||
export async function throttleTest({
|
export async function throttleTest({
|
||||||
context,
|
context,
|
||||||
page,
|
|
||||||
delay,
|
delay,
|
||||||
|
page,
|
||||||
}: {
|
}: {
|
||||||
context: BrowserContext
|
context: BrowserContext
|
||||||
delay: 'Fast 3G' | 'Slow 3G' | 'Slow 4G'
|
delay: 'Fast 3G' | 'Slow 3G' | 'Slow 4G'
|
||||||
@@ -108,9 +108,9 @@ export async function throttleTest({
|
|||||||
|
|
||||||
await cdpSession.send('Network.emulateNetworkConditions', {
|
await cdpSession.send('Network.emulateNetworkConditions', {
|
||||||
downloadThroughput: networkConditions[delay].download,
|
downloadThroughput: networkConditions[delay].download,
|
||||||
uploadThroughput: networkConditions[delay].upload,
|
|
||||||
latency: networkConditions[delay].latency,
|
latency: networkConditions[delay].latency,
|
||||||
offline: false,
|
offline: false,
|
||||||
|
uploadThroughput: networkConditions[delay].upload,
|
||||||
})
|
})
|
||||||
|
|
||||||
await page.route('**/*', async (route) => {
|
await page.route('**/*', async (route) => {
|
||||||
@@ -123,7 +123,7 @@ export async function throttleTest({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function firstRegister(args: FirstRegisterArgs): Promise<void> {
|
export async function firstRegister(args: FirstRegisterArgs): Promise<void> {
|
||||||
const { page, serverURL, customAdminRoutes, customRoutes } = args
|
const { customAdminRoutes, customRoutes, page, serverURL } = args
|
||||||
|
|
||||||
const {
|
const {
|
||||||
routes: { admin: adminRoute },
|
routes: { admin: adminRoute },
|
||||||
@@ -139,11 +139,11 @@ export async function firstRegister(args: FirstRegisterArgs): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function login(args: LoginArgs): Promise<void> {
|
export async function login(args: LoginArgs): Promise<void> {
|
||||||
const { page, serverURL, data = devUser, customAdminRoutes, customRoutes } = args
|
const { customAdminRoutes, customRoutes, data = devUser, page, serverURL } = args
|
||||||
|
|
||||||
const {
|
const {
|
||||||
admin: {
|
admin: {
|
||||||
routes: { login: loginRoute, createFirstUser: createFirstUserRoute },
|
routes: { createFirstUser: createFirstUserRoute, login: loginRoute },
|
||||||
},
|
},
|
||||||
routes: { admin: adminRoute },
|
routes: { admin: adminRoute },
|
||||||
} = getAdminRoutes({ customAdminRoutes, customRoutes })
|
} = getAdminRoutes({ customAdminRoutes, customRoutes })
|
||||||
@@ -236,7 +236,7 @@ export async function openDocControls(page: Page): Promise<void> {
|
|||||||
export async function changeLocale(page: Page, newLocale: string) {
|
export async function changeLocale(page: Page, newLocale: string) {
|
||||||
await page.locator('.localizer >> button').first().click()
|
await page.locator('.localizer >> button').first().click()
|
||||||
await page
|
await page
|
||||||
.locator(`.localizer .popup.popup--active .popup-button-list button`, {
|
.locator(`.localizer .popup.popup--active .popup-button-list__button`, {
|
||||||
hasText: newLocale,
|
hasText: newLocale,
|
||||||
})
|
})
|
||||||
.first()
|
.first()
|
||||||
@@ -349,8 +349,8 @@ export function describeIfInCIOrHasLocalstack(): jest.Describe {
|
|||||||
type AdminRoutes = Config['admin']['routes']
|
type AdminRoutes = Config['admin']['routes']
|
||||||
|
|
||||||
export function getAdminRoutes({
|
export function getAdminRoutes({
|
||||||
customRoutes,
|
|
||||||
customAdminRoutes,
|
customAdminRoutes,
|
||||||
|
customRoutes,
|
||||||
}: {
|
}: {
|
||||||
customAdminRoutes?: AdminRoutes
|
customAdminRoutes?: AdminRoutes
|
||||||
customRoutes?: Config['routes']
|
customRoutes?: Config['routes']
|
||||||
|
|||||||
Reference in New Issue
Block a user