fix: sync localization data after switching locale (#5277)
Co-authored-by: James <james@trbl.design>
This commit is contained in:
@@ -13,7 +13,7 @@ import React, { Fragment, useEffect } from 'react'
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
export const EditViewClient: React.FC = () => {
|
export const EditViewClient: React.FC = () => {
|
||||||
const { collectionSlug, getDocPermissions, getVersions, globalSlug, isEditing, setDocumentInfo } =
|
const { collectionSlug, getDocPermissions, getVersions, globalSlug, isEditing, setOnSave } =
|
||||||
useDocumentInfo()
|
useDocumentInfo()
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -58,10 +58,8 @@ export const EditViewClient: React.FC = () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setDocumentInfo({
|
setOnSave(onSave)
|
||||||
onSave,
|
}, [setOnSave, onSave])
|
||||||
})
|
|
||||||
}, [setDocumentInfo, onSave])
|
|
||||||
|
|
||||||
// Allow the `DocumentInfoProvider` to hydrate
|
// Allow the `DocumentInfoProvider` to hydrate
|
||||||
if (!Edit || (!collectionSlug && !globalSlug)) {
|
if (!Edit || (!collectionSlug && !globalSlug)) {
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ const Localizer: React.FC<{
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
router.refresh()
|
||||||
close()
|
close()
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -98,6 +98,8 @@ export const PublishMany: React.FC<Props> = (props) => {
|
|||||||
slug,
|
slug,
|
||||||
t,
|
t,
|
||||||
toggleModal,
|
toggleModal,
|
||||||
|
router,
|
||||||
|
stringifyParams,
|
||||||
])
|
])
|
||||||
|
|
||||||
if (!versions?.drafts || selectAll === SelectAllStatus.None || !hasPermission) {
|
if (!versions?.drafts || selectAll === SelectAllStatus.None || !hasPermission) {
|
||||||
|
|||||||
@@ -94,6 +94,8 @@ export const UnpublishMany: React.FC<Props> = (props) => {
|
|||||||
slug,
|
slug,
|
||||||
t,
|
t,
|
||||||
toggleModal,
|
toggleModal,
|
||||||
|
router,
|
||||||
|
stringifyParams,
|
||||||
])
|
])
|
||||||
|
|
||||||
if (!versions?.drafts || selectAll === SelectAllStatus.None || !hasPermission) {
|
if (!versions?.drafts || selectAll === SelectAllStatus.None || !hasPermission) {
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import { useConfig } from '../Config/index.js'
|
|||||||
import { useLocale } from '../Locale/index.js'
|
import { useLocale } from '../Locale/index.js'
|
||||||
import { usePreferences } from '../Preferences/index.js'
|
import { usePreferences } from '../Preferences/index.js'
|
||||||
import { useTranslation } from '../Translation/index.js'
|
import { useTranslation } from '../Translation/index.js'
|
||||||
import { documentInfoReducer } from './reducer.js'
|
|
||||||
|
|
||||||
const Context = createContext({} as DocumentInfoContext)
|
const Context = createContext({} as DocumentInfoContext)
|
||||||
|
|
||||||
@@ -32,20 +31,11 @@ export const DocumentInfoProvider: React.FC<
|
|||||||
DocumentInfoProps & {
|
DocumentInfoProps & {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}
|
}
|
||||||
> = ({ children, ...rest }) => {
|
> = ({ children, ...props }) => {
|
||||||
const [documentInfo, dispatchDocumentInfo] = useReducer(documentInfoReducer, rest)
|
const [documentTitle, setDocumentTitle] = useState(props.title)
|
||||||
|
const [onSave, setOnSave] = useState(() => props.onSave)
|
||||||
|
|
||||||
const setDocumentInfo = useCallback(
|
const { id, collectionSlug, globalSlug } = props
|
||||||
(newInfo: DocumentInfo) => {
|
|
||||||
dispatchDocumentInfo({
|
|
||||||
type: 'SET_DOC_INFO',
|
|
||||||
payload: newInfo,
|
|
||||||
})
|
|
||||||
},
|
|
||||||
[dispatchDocumentInfo],
|
|
||||||
)
|
|
||||||
|
|
||||||
const { id, collectionSlug, globalSlug } = documentInfo
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
collections,
|
collections,
|
||||||
@@ -69,13 +59,6 @@ export const DocumentInfoProvider: React.FC<
|
|||||||
const [unpublishedVersions, setUnpublishedVersions] =
|
const [unpublishedVersions, setUnpublishedVersions] =
|
||||||
useState<PaginatedDocs<TypeWithVersion<any>>>(null)
|
useState<PaginatedDocs<TypeWithVersion<any>>>(null)
|
||||||
|
|
||||||
const setDocumentTitle = useCallback((title: string) => {
|
|
||||||
dispatchDocumentInfo({
|
|
||||||
type: 'SET_DOC_TITLE',
|
|
||||||
payload: title,
|
|
||||||
})
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const baseURL = `${serverURL}${api}`
|
const baseURL = `${serverURL}${api}`
|
||||||
let slug: string
|
let slug: string
|
||||||
let pluralType: 'collections' | 'globals'
|
let pluralType: 'collections' | 'globals'
|
||||||
@@ -285,9 +268,13 @@ export const DocumentInfoProvider: React.FC<
|
|||||||
void getVersions()
|
void getVersions()
|
||||||
}, [getVersions])
|
}, [getVersions])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setDocumentTitle(props.title)
|
||||||
|
}, [props.title])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadDocPermissions = async () => {
|
const loadDocPermissions = async () => {
|
||||||
const docPermissions: DocumentPermissions = rest.docPermissions
|
const docPermissions: DocumentPermissions = props.docPermissions
|
||||||
if (!docPermissions) await getDocPermissions()
|
if (!docPermissions) await getDocPermissions()
|
||||||
else setDocPermissions(docPermissions)
|
else setDocPermissions(docPermissions)
|
||||||
}
|
}
|
||||||
@@ -295,37 +282,21 @@ export const DocumentInfoProvider: React.FC<
|
|||||||
if (collectionSlug || globalSlug) {
|
if (collectionSlug || globalSlug) {
|
||||||
void loadDocPermissions()
|
void loadDocPermissions()
|
||||||
}
|
}
|
||||||
}, [getDocPermissions, rest.docPermissions, setDocPermissions, collectionSlug, globalSlug])
|
}, [getDocPermissions, props.docPermissions, setDocPermissions, collectionSlug, globalSlug])
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const loadDocPreferences = async () => {
|
|
||||||
let docPreferences: DocumentPreferences = rest.docPreferences
|
|
||||||
if (!docPreferences) docPreferences = await getDocPreferences()
|
|
||||||
|
|
||||||
dispatchDocumentInfo({
|
|
||||||
type: 'SET_DOC_INFO',
|
|
||||||
payload: {
|
|
||||||
docPreferences,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (id) {
|
|
||||||
void loadDocPreferences()
|
|
||||||
}
|
|
||||||
}, [getDocPreferences, preferencesKey, rest.docPreferences, id])
|
|
||||||
|
|
||||||
const value: DocumentInfoContext = {
|
const value: DocumentInfoContext = {
|
||||||
...documentInfo,
|
...props,
|
||||||
docConfig,
|
docConfig,
|
||||||
docPermissions,
|
docPermissions,
|
||||||
getDocPermissions,
|
getDocPermissions,
|
||||||
getDocPreferences,
|
getDocPreferences,
|
||||||
getVersions,
|
getVersions,
|
||||||
|
onSave,
|
||||||
publishedDoc,
|
publishedDoc,
|
||||||
setDocFieldPreferences,
|
setDocFieldPreferences,
|
||||||
setDocumentInfo,
|
|
||||||
setDocumentTitle,
|
setDocumentTitle,
|
||||||
|
setOnSave,
|
||||||
|
title: documentTitle,
|
||||||
unpublishedVersions,
|
unpublishedVersions,
|
||||||
versions,
|
versions,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
import type { DocumentInfo } from './index.js'
|
|
||||||
|
|
||||||
type SET = {
|
|
||||||
payload: Partial<DocumentInfo>
|
|
||||||
type: 'SET_DOC_INFO'
|
|
||||||
}
|
|
||||||
|
|
||||||
type SET_TITLE = {
|
|
||||||
payload: string
|
|
||||||
type: 'SET_DOC_TITLE'
|
|
||||||
}
|
|
||||||
|
|
||||||
type RESET = {
|
|
||||||
payload: DocumentInfo
|
|
||||||
type: 'RESET_DOC_INFO'
|
|
||||||
}
|
|
||||||
|
|
||||||
type Action = RESET | SET | SET_TITLE
|
|
||||||
|
|
||||||
export const documentInfoReducer = (state: DocumentInfo, action: Action): DocumentInfo => {
|
|
||||||
switch (action.type) {
|
|
||||||
case 'SET_DOC_INFO':
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
...action.payload,
|
|
||||||
}
|
|
||||||
case 'SET_DOC_TITLE':
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
title: action.payload,
|
|
||||||
}
|
|
||||||
case 'RESET_DOC_INFO':
|
|
||||||
return {
|
|
||||||
...action.payload,
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -48,6 +48,6 @@ export type DocumentInfoContext = Omit<DocumentInfo, 'docPreferences'> & {
|
|||||||
getDocPreferences: () => Promise<{ [key: string]: unknown }>
|
getDocPreferences: () => Promise<{ [key: string]: unknown }>
|
||||||
getVersions: () => Promise<void>
|
getVersions: () => Promise<void>
|
||||||
setDocFieldPreferences: (field: string, fieldPreferences: { [key: string]: unknown }) => void
|
setDocFieldPreferences: (field: string, fieldPreferences: { [key: string]: unknown }) => void
|
||||||
setDocumentInfo?: React.Dispatch<React.SetStateAction<Partial<DocumentInfo>>>
|
|
||||||
setDocumentTitle: (title: string) => void
|
setDocumentTitle: (title: string) => void
|
||||||
|
setOnSave: (data: Data) => Promise<void> | void
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ export default buildConfigWithDefaults({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onInit: async (payload) => {
|
onInit: async (payload) => {
|
||||||
console.log('onInit')
|
|
||||||
await payload.create({
|
await payload.create({
|
||||||
collection: 'users',
|
collection: 'users',
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user