Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set def field value for visible fields #4716

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
</template>

<script>
import _ from 'lodash';
import { klona } from 'klona';
import { mapActions } from 'pinia';
import AposModifiedMixin from 'Modules/@apostrophecms/ui/mixins/AposModifiedMixin';
Expand Down Expand Up @@ -528,10 +529,13 @@ export default {
this.docType = docData.type;
}
this.original = klona(docData);
this.docFields.data = {
...this.getDefault(),
...docData
};
console.log('docData');
console.dir(docData, { depth: 9 });
console.log('this.getDefault()');
console.dir(this.getDefault(docData), { depth: 9 });
this.docFields.data = _.merge(this.getDefault(docData), docData);
console.log('this.docFields.data');
console.dir(this.docFields.data, { depth: 9 });
// TODO: Is this block even useful since published is fetched after loadDoc?
if (this.published) {
this.changed = detectDocChange(
Expand All @@ -546,20 +550,57 @@ export default {
}
}
},
getDefault() {
getDefault(docData) {
const doc = {};
this.schema.forEach(field => {
if (field.name.startsWith('_')) {
return;
}
// Using `hasOwn` here, not simply checking if `field.def` is truthy
// so that `false`, `null`, `''` or `0` are taken into account:
const hasDefaultValue = Object.hasOwn(field, 'def');
doc[field.name] = hasDefaultValue
? klona(field.def)
: null;
});

getSchemaDefault(doc, this.schema, docData);

return doc;

function getSchemaDefault(doc, schema, docData, isArray = false) {
schema.forEach(field => {
if (field.name.startsWith('_')) {
return;
}

// Using `hasOwn` here, not simply checking if `field.def` is truthy
// so that `false`, `null`, `''` or `0` are taken into account:
const hasDefaultValue = Object.hasOwn(field, 'def');

if (isArray && hasDefaultValue) {
console.log('isArray && hasDefaultValue');
console.log(doc);

docData.forEach((item, index) => {
if (!doc[index]) {
doc[index] = {};
}

doc[index][field.name] = klona(field.def);

// if (field.type === 'object') {
// getSchemaDefault(doc[index][field.name], field.schema);
// }

// if (field.type === 'array') {
// getSchemaDefault(doc[index][field.name], field.schema, true, docData[index][field.name]);
// }
});
} else {
doc[field.name] = hasDefaultValue
? klona(field.def)
: null;
}

if (field.type === 'object') {
getSchemaDefault(doc[field.name], field.schema, docData[field.name]);
}

if (field.type === 'array') {
getSchemaDefault(doc[field.name], field.schema, docData[field.name], true);
}
});
}
},
async preview() {
if (!await this.confirmAndCancel()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default {
},
data () {
const next = this.getNext();
// console.log('🚀 ~ data ~ next:', next);
return {
schemaInput: {
data: next
Expand Down Expand Up @@ -117,6 +118,8 @@ export default {
},
// Return next at mount or when generation changes
getNext() {
// console.log('this.modelValue', this.modelValue);
// console.log('{ ...this.modelValue }', { ...this.modelValue });
return this.modelValue?.data ? this.modelValue.data : (this.field.def || {});
}
}
Expand Down
4 changes: 4 additions & 0 deletions modules/@apostrophecms/schema/ui/apos/logic/AposSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ export default {
};
next.data[field.name] = fieldState[field.name].data;
});
// console.log('fieldState');
// console.dir(fieldState, { depth: 9 });
// console.log('next');
// console.dir(next, { depth: 9 });
this.next = next;
this.fieldState = fieldState;

Expand Down
10 changes: 10 additions & 0 deletions modules/@apostrophecms/schema/ui/apos/mixins/AposInputMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ export default {
validateAndEmit () {
// If the field is conditional and isn't shown, disregard any errors.
const error = this.conditionMet === false ? false : this.validate(this.next);
// console.log('🚀 ~ validateAndEmit ~ error:', error);

// console.log('this.next', this.next);
// console.log('this.convert(this.next)', this.convert(this.next));

// const isObject = x => typeof x === 'object' && !Array.isArray(x) && x !== null;
// if (isObject(this.next)) {
// console.log('this.next is an object');
// this.next
// }

this.$emit('update:modelValue', {
data: error ? this.next : this.convert(this.next),
Expand Down
Loading