This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reapply defaults on discard (#864)
- Loading branch information
1 parent
5602812
commit 6bcf451
Showing
6 changed files
with
105 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import isEqual from 'lodash/isEqual'; | ||
|
||
import { isNotNullish } from './null.util'; | ||
import { | ||
I18N_FIELD_DUPLICATE, | ||
I18N_FIELD_TRANSLATE, | ||
duplicateDefaultI18nFields, | ||
hasI18n, | ||
} from '../i18n'; | ||
|
||
import type { Collection, EntryData, Field, ObjectValue } from '@staticcms/core/interface'; | ||
|
||
export function applyDefaultsToDraftData( | ||
fields: Field[], | ||
skipField: (field: Field) => boolean = () => false, | ||
initialValue?: ObjectValue | null, | ||
) { | ||
const emptyDraftData = fields.reduce((acc, item) => { | ||
const name = item.name; | ||
|
||
if (skipField(item) || isNotNullish(acc[name])) { | ||
return acc; | ||
} | ||
|
||
const subfields = 'fields' in item && item.fields; | ||
const list = item.widget === 'list'; | ||
const defaultValue = (('default' in item ? item.default : null) ?? null) as EntryData; | ||
|
||
function isEmptyDefaultValue(val: EntryData | EntryData[]) { | ||
return [[{}], {}].some(e => isEqual(val, e)); | ||
} | ||
|
||
if (subfields) { | ||
if (list && Array.isArray(defaultValue)) { | ||
acc[name] = defaultValue; | ||
} else { | ||
const asList = Array.isArray(subfields) ? subfields : [subfields]; | ||
|
||
const subDefaultValue = list | ||
? [applyDefaultsToDraftData(asList, skipField)] | ||
: applyDefaultsToDraftData(asList, skipField); | ||
|
||
if (!isEmptyDefaultValue(subDefaultValue)) { | ||
acc[name] = subDefaultValue; | ||
} | ||
} | ||
return acc; | ||
} | ||
|
||
if (defaultValue !== null) { | ||
acc[name] = defaultValue; | ||
} | ||
|
||
return acc; | ||
}, (initialValue ?? {}) as ObjectValue); | ||
|
||
return emptyDraftData; | ||
} | ||
|
||
export function createEmptyDraftData(fields: Field[], skipField?: (field: Field) => boolean) { | ||
return applyDefaultsToDraftData(fields, skipField); | ||
} | ||
|
||
export function createEmptyDraftI18nData(collection: Collection, dataFields: Field[]) { | ||
if (!hasI18n(collection)) { | ||
return {}; | ||
} | ||
|
||
function skipField(field: Field) { | ||
return field.i18n !== I18N_FIELD_DUPLICATE && field.i18n !== I18N_FIELD_TRANSLATE; | ||
} | ||
|
||
const i18nData = createEmptyDraftData(dataFields, skipField); | ||
return duplicateDefaultI18nFields(collection, i18nData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters