* chore: ensures relationship fields react to locale changes in the admin panel - fixes #1870

* chore: patches in default values for fields, and localized fields using fallbacks - fixes #1859

* chore: organizes field localization and sanitizing

* Revert "Feat/1180 loading UI enhancements"

* Feat/1180 loading UI enhancements

* chore: safely sets tab if name field, only sets fallback value if it exists

* chore: adds test to ensure text fields use fallback locale value when empty
This commit is contained in:
Jarrod Flesch
2023-01-19 16:55:03 -05:00
committed by GitHub
parent 5d71d4bf6e
commit c0ac155a71
3 changed files with 68 additions and 15 deletions

View File

@@ -46,22 +46,49 @@ export const promise = async ({
delete siblingDoc[field.name];
}
const hasLocalizedValue = flattenLocales
const shouldHoistLocalizedValue = flattenLocales
&& fieldAffectsData(field)
&& (typeof siblingDoc[field.name] === 'object' && siblingDoc[field.name] !== null)
&& field.localized
&& req.locale !== 'all';
if (hasLocalizedValue) {
let localizedValue = siblingDoc[field.name][req.locale];
if (typeof localizedValue === 'undefined' && req.fallbackLocale) localizedValue = siblingDoc[field.name][req.fallbackLocale];
if (localizedValue === null && (field.type === 'array' || field.type === 'blocks')) localizedValue = siblingDoc[field.name][req.fallbackLocale];
if (typeof localizedValue === 'undefined' && (field.type === 'group' || field.type === 'tab')) localizedValue = {};
if (typeof localizedValue === 'undefined') localizedValue = null;
siblingDoc[field.name] = localizedValue;
if (shouldHoistLocalizedValue) {
// replace actual value with localized value before sanitizing
// { [locale]: fields } -> fields
const { locale } = req;
const value = siblingDoc[field.name][locale];
const fallbackLocale = req.payload.config.localization && req.payload.config.localization?.fallback && req.fallbackLocale;
let hoistedValue = value;
if (fallbackLocale && fallbackLocale !== locale) {
const fallbackValue = siblingDoc[field.name][fallbackLocale];
const isNullOrUndefined = typeof value === 'undefined' || value === null;
if (fallbackValue) {
switch (field.type) {
case 'text':
case 'textarea': {
if (value === '' || isNullOrUndefined) {
hoistedValue = fallbackValue;
}
break;
}
default: {
if (isNullOrUndefined) {
hoistedValue = fallbackValue;
}
break;
}
}
}
}
siblingDoc[field.name] = hoistedValue;
}
// Sanitize outgoing data
// Sanitize outgoing field value
switch (field.type) {
case 'group': {
// Fill groups with empty objects so fields with hooks within groups can populate
@@ -74,7 +101,7 @@ export const promise = async ({
}
case 'tabs': {
field.tabs.forEach((tab) => {
if (tabHasName(tab) && typeof siblingDoc[tab.name] === 'undefined') {
if (tabHasName(tab) && (typeof siblingDoc[tab.name] === 'undefined' || siblingDoc[tab.name] === null)) {
siblingDoc[tab.name] = {};
}
});