kopparhavet/module/helpers/actor-helper.js

54 lines
1.8 KiB
JavaScript

export default class ActorHelpers {
static async actorUpdate(event, formData) {
formData = expandObject(formData);
// Handle the free-form relationship list
const formAttrs = expandObject(formData)?.data?.relationships || {};
const relationships = Object.values(formAttrs).reduce((obj, v) => {
let k = v["key"].trim();
delete v["key"];
obj[k] = v;
return obj;
}, {});
// Remove modifications which are no longer used
if (this.object.data?.relationships) {
for (let k of Object.keys(this.object.data.relationships)) {
if (!relationships.hasOwnProperty(k)) relationships[`-=${k}`] = null;
}
}
// recombine modifications to formData
if (Object.keys(relationships).length > 0) {
setProperty(formData, `data.relationships`, relationships);
}
// Handle the free-form learnign list
const formAttrsLearning = expandObject(formData)?.data?.learning || {};
const learning = Object.values(formAttrsLearning).reduce((obj, v) => {
let k = v["key"].trim();
delete v["key"];
obj[k] = v;
return obj;
}, {});
// Remove modifications which are no longer used
if (this.object.data?.learning) {
for (let k of Object.keys(this.object.data.learning)) {
if (!learning.hasOwnProperty(k)) learning[`-=${k}`] = null;
}
}
// recombine modifications to formData
if (Object.keys(learning).length > 0) {
setProperty(formData, `data.learning`, learning);
}
// Update the Item
this.actor.data.flags.loaded = false;
this.object.update(formData);
}
}