-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
category and taxonomy extra props editor: code cleanup
- Loading branch information
Showing
3 changed files
with
61 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as Validation from '@core/validation/validation' | ||
import { validateExtraPropDef } from './extraPropDefValidator' | ||
|
||
import { ExtraPropDef } from './extraPropDef' | ||
|
||
const updateOrDeleteExtraDef = async ({ extraPropDefs, propName, extraPropDef, deleted = false }) => { | ||
const extraPropDefsArray = ExtraPropDef.extraDefsToArray(extraPropDefs) | ||
const itemExtraDefsArrayUpdated = [...extraPropDefsArray] | ||
const oldItemIndex = itemExtraDefsArrayUpdated.findIndex((item) => ExtraPropDef.getName(item) === propName) | ||
|
||
if (deleted) { | ||
// remove old item | ||
delete itemExtraDefsArrayUpdated[oldItemIndex] | ||
} else { | ||
if (oldItemIndex < 0) { | ||
// add new extra def item | ||
itemExtraDefsArrayUpdated.push(extraPropDef) | ||
} else { | ||
// update existing item | ||
itemExtraDefsArrayUpdated[oldItemIndex] = extraPropDef | ||
} | ||
const validation = await validateExtraPropDef({ | ||
extraPropDef, | ||
extraPropDefsArray: itemExtraDefsArrayUpdated, | ||
}) | ||
if (!Validation.isValid(validation)) { | ||
throw new Error('Invalid item extra def') | ||
} | ||
} | ||
// prepare itemExtraDefs for storage | ||
// - remove unnecessary information (uuid, name) | ||
// - index stored object by extra def name | ||
return itemExtraDefsArrayUpdated.reduce((acc, item, index) => { | ||
const name = ExtraPropDef.getName(item) | ||
const dataType = ExtraPropDef.getDataType(item) | ||
acc[name] = ExtraPropDef.newItem({ dataType, index }) | ||
return acc | ||
}, {}) | ||
} | ||
|
||
export const ExtraPropDefsUpdater = { | ||
updateOrDeleteExtraDef, | ||
} |
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