feat: i18n support (#387)

This commit is contained in:
Daniel Lautzenheiser
2023-01-18 15:08:40 -05:00
committed by GitHub
parent 7372c3735b
commit a01f30ef69
22 changed files with 580 additions and 425 deletions

View File

@ -181,6 +181,7 @@ function entries(
const payload = action.payload;
const loadedEntries = payload.entries;
const page = payload.page;
const append = payload.append;
const entities = {
...state.entities,
@ -196,7 +197,9 @@ function entries(
pages[payload.collection] = {
page: page ?? undefined,
ids: [...(pages[payload.collection]?.ids ?? []), ...loadedEntries.map(entry => entry.slug)],
ids: append
? [...(pages[payload.collection]?.ids ?? []), ...loadedEntries.map(entry => entry.slug)]
: [...loadedEntries.map(entry => entry.slug)],
isFetching: false,
};

View File

@ -162,10 +162,14 @@ function entryDraftReducer(
entry: set(newState.entry, `${dataPath.join('.')}.${path}`, value),
};
console.log('BEFORE I18N', { ...newState.entry });
if (i18n) {
newState = duplicateI18nFields(newState, field, i18n.locales, i18n.defaultLocale);
}
console.log('AFTER I18N', { ...newState.entry });
const newData = get(newState.entry, dataPath) ?? {};
return {
@ -175,12 +179,16 @@ function entryDraftReducer(
}
case DRAFT_VALIDATION_ERRORS: {
const { path, errors } = action.payload;
const { path, errors, i18n } = action.payload;
const fieldsErrors = { ...state.fieldsErrors };
const dataPath = (i18n && getDataPath(i18n.currentLocale, i18n.defaultLocale)) || ['data'];
const fullPath = `${dataPath.join('.')}.${path}`;
if (errors.length === 0) {
delete fieldsErrors[path];
delete fieldsErrors[fullPath];
} else {
fieldsErrors[path] = action.payload.errors;
fieldsErrors[fullPath] = action.payload.errors;
}
return {
...state,

View File

@ -1,9 +1,14 @@
/* eslint-disable import/prefer-default-export */
import { getDataPath } from '@staticcms/core/lib/i18n';
import type { I18nSettings } from '@staticcms/core/interface';
import type { RootState } from '@staticcms/core/store';
export const selectFieldErrors = (path: string) => (state: RootState) => {
return state.entryDraft.fieldsErrors[path] ?? [];
};
export const selectFieldErrors =
(path: string, i18n: I18nSettings | undefined) => (state: RootState) => {
const dataPath = (i18n && getDataPath(i18n.currentLocale, i18n.defaultLocale)) || ['data'];
const fullPath = `${dataPath.join('.')}.${path}`;
return state.entryDraft.fieldsErrors[fullPath] ?? [];
};
export function selectEditingDraft(state: RootState) {
return state.entryDraft.entry;