chore: further refinements

This commit is contained in:
James
2022-09-11 20:57:30 -07:00
parent ad4f7a5fff
commit 21eb19edd1
10 changed files with 100 additions and 40 deletions

View File

@@ -10,12 +10,19 @@ import { User } from '../../auth';
import { Payload } from '../..';
export type FieldHookArgs<T extends TypeWithID = any, P = any, S = any> = {
/** The data passed to update the document within create and update operations, and the full document itself in the afterRead hook. */
data?: Partial<T>,
/** Boolean to denote if this hook is running against finding one, or finding many within the afterRead hook. */
findMany?: boolean
/** The full original document in `update` operations. In the `afterChange` hook, this is the resulting document of the operation. */
originalDoc?: T,
/** A string relating to which operation the field type is currently executing within. Useful within beforeValidate, beforeChange, and afterChange hooks to differentiate between create and update operations. */
operation?: 'create' | 'read' | 'update' | 'delete',
/** The Express request object. It is mocked for Local API operations. */
req: PayloadRequest
/** The sibling data passed to a field that the hook is running against. */
siblingData: Partial<S>
/** The value of the field. */
value?: P,
}
@@ -203,6 +210,7 @@ export type TabsField = Omit<FieldBase, 'admin' | 'name' | 'localized'> & {
export type TabAsField = Tab & {
type: 'tab'
name?: string
};
export type UIField = {
@@ -389,6 +397,7 @@ export type FieldAffectingData =
| UploadField
| CodeField
| PointField
| TabAsField
export type NonPresentationalField =
TextField

View File

@@ -126,14 +126,12 @@ export const promise = async ({
}
case 'tab': {
let tabSiblingData;
let tabSiblingDoc;
let tabSiblingData = siblingData;
let tabSiblingDoc = siblingDoc;
if (tabHasName(field)) {
tabSiblingData = siblingData[field.name] || {};
tabSiblingDoc = siblingDoc[field.name];
} else {
tabSiblingData = siblingData || {};
tabSiblingDoc = { ...siblingDoc };
tabSiblingData = siblingData[field.name] as Record<string, unknown>;
tabSiblingDoc = siblingDoc[field.name] as Record<string, unknown>;
}
await traverseFields({

View File

@@ -49,14 +49,13 @@ export const promise = async ({
const hasLocalizedValue = flattenLocales
&& fieldAffectsData(field)
&& (typeof siblingDoc[field.name] === 'object' && siblingDoc[field.name] !== null)
&& field.name
&& 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 (typeof localizedValue === 'undefined' && field.type === 'group') localizedValue = {};
if (typeof localizedValue === 'undefined' && (field.type === 'group' || field.type === 'tab')) localizedValue = {};
if (typeof localizedValue === 'undefined') localizedValue = null;
siblingDoc[field.name] = localizedValue;
}

View File

@@ -286,11 +286,16 @@ export const promise = async ({
let tabSiblingData = siblingData;
let tabSiblingDoc = siblingDoc;
let tabSiblingDocWithLocales = siblingDocWithLocales;
if (tabHasName(field)) {
tabPath = `${path}${field.name}.`;
tabSiblingData = typeof siblingData[field.name] === 'object' ? siblingData[field.name] as Record<string, unknown> : {};
tabSiblingDoc = typeof siblingDoc[field.name] === 'object' ? siblingDoc[field.name] as Record<string, unknown> : {};
tabSiblingDocWithLocales = typeof siblingDocWithLocales[field.name] === 'object' ? siblingDocWithLocales[field.name] as Record<string, unknown> : {};
if (typeof siblingData[field.name] !== 'object') siblingData[field.name] = {};
if (typeof siblingDoc[field.name] !== 'object') siblingDoc[field.name] = {};
if (typeof siblingDocWithLocales[field.name] !== 'object') siblingDocWithLocales[field.name] = {};
tabSiblingData = siblingData[field.name] as Record<string, unknown>;
tabSiblingDoc = siblingDoc[field.name] as Record<string, unknown>;
tabSiblingDocWithLocales = siblingDocWithLocales[field.name] as Record<string, unknown>;
}
await traverseFields({