Fix issue that would cause sort order of fields to be corrupted on field changes (#6174)

Fixes #6091
This commit is contained in:
Rijk van Zanten
2021-06-09 20:13:21 -04:00
committed by GitHub
parent 1f2bce9fd2
commit 11ea192368
2 changed files with 10 additions and 20 deletions

View File

@@ -5,12 +5,14 @@ import { getInterfaces } from '@/interfaces';
import { InterfaceConfig } from '@/interfaces/types';
import { Field } from '@/types';
import { getDefaultInterfaceForType } from '@/utils/get-default-interface-for-type';
import { clone } from 'lodash';
import { clone, orderBy } from 'lodash';
import { computed, ComputedRef, Ref } from 'vue';
export default function useFormFields(fields: Ref<Field[]>): { formFields: ComputedRef<Field[]> } {
const { interfaces } = getInterfaces();
const systemFieldsCount = computed(() => fields.value.filter((field) => field.meta?.system === true).length);
const formFields = computed(() => {
let formFields = clone(fields.value);
@@ -42,6 +44,10 @@ export default function useFormFields(fields: Ref<Field[]>): { formFields: Compu
}
}
if (field.meta?.sort && field.meta?.system !== true) {
field.meta.sort = field.meta.sort + systemFieldsCount.value;
}
return field;
});
@@ -52,6 +58,8 @@ export default function useFormFields(fields: Ref<Field[]>): { formFields: Compu
return hidden !== true && systemFake === false;
});
formFields = orderBy(formFields, 'meta.sort');
return formFields;
});

View File

@@ -61,7 +61,7 @@ export const useFieldsStore = defineStore({
const fields: FieldRaw[] = fieldsResponse.data.data;
this.fields = this.adjustSortForSystem([...fields.map(this.parseField), fakeFilesField]);
this.fields = [...fields.map(this.parseField), fakeFilesField];
this.translateFields();
},
@@ -106,24 +106,6 @@ export const useFieldsStore = defineStore({
};
});
},
/**
* System collections have system fields. We'll have to adjust all custom fields to have their
* sort values incremented by the amount of system fields, to ensure the fields are sorted
* correctly after the system fields. (#5520)
*/
adjustSortForSystem(fields: FieldRaw[]) {
const systemFields = fields.filter((field) => field.meta?.system === true);
if (systemFields.length === 0) {
return systemFields;
}
return fields.map((field) => {
if (field.meta?.system === true) return field;
if (field.meta?.sort) field.meta.sort += systemFields.length;
return field;
});
},
async createField(collectionKey: string, newField: Field) {
const stateClone = [...this.fields];