Skip to content

Commit 3145143

Browse files
committed
chore: fix typescript errors in ResourceForm
1 parent 09cfc48 commit 3145143

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

adminforth/spa/src/components/ResourceForm.vue

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@
6565
6666
import { applyRegexValidation, callAdminForthApi, loadMoreForeignOptions, searchForeignOptions, createSearchInputHandlers} from '@/utils';
6767
import { computedAsync } from '@vueuse/core';
68-
import { computed, onMounted, reactive, ref, watch, provide } from 'vue';
68+
import { computed, onMounted, reactive, ref, watch, provide, type Ref } from 'vue';
6969
import { useRouter, useRoute } from 'vue-router';
7070
import { useCoreStore } from "@/stores/core";
7171
import GroupsTable from '@/components/GroupsTable.vue';
7272
import { useI18n } from 'vue-i18n';
73-
import { type AdminForthResourceCommon } from '@/types/Common';
73+
import { type AdminForthResourceColumnCommon, type AdminForthResourceCommon } from '@/types/Common';
7474
7575
const { t } = useI18n();
7676
@@ -91,16 +91,16 @@ const mode = computed(() => route.name === 'resource-create' ? 'create' : 'edit'
9191
9292
const emit = defineEmits(['update:record', 'update:isValid']);
9393
94-
const currentValues = ref(null);
95-
const customComponentsInValidity = ref({});
96-
const customComponentsEmptiness = ref({});
94+
const currentValues: Ref<Record<string, any>> = ref({});
95+
const customComponentsInValidity: Ref<Record<string, AdminForthResourceColumnCommon>> = ref({});
96+
const customComponentsEmptiness: Ref<Record<string, AdminForthResourceColumnCommon>> = ref({});
9797
9898
const columnOptions = ref<Record<string, any[]>>({});
9999
const columnLoadingState = reactive<Record<string, { loading: boolean; hasMore: boolean }>>({});
100100
const columnOffsets = reactive<Record<string, number>>({});
101101
const columnEmptyResultsCount = reactive<Record<string, number>>({});
102102
103-
const columnError = (column) => {
103+
const columnError = (column: AdminForthResourceColumnCommon) => {
104104
const val = computed(() => {
105105
if (!currentValues.value) {
106106
return null;
@@ -109,7 +109,7 @@ const columnError = (column) => {
109109
return customComponentsInValidity.value?.[column.name];
110110
}
111111
112-
if ( column.required[mode.value] ) {
112+
if ( column.required?.[mode.value] ) {
113113
const naturalEmptiness = currentValues.value[column.name] === undefined ||
114114
currentValues.value[column.name] === null ||
115115
currentValues.value[column.name] === '' ||
@@ -136,33 +136,39 @@ const columnError = (column) => {
136136
}
137137
} else if (column.isArray?.enabled) {
138138
if (!column.isArray.allowDuplicateItems) {
139-
if (currentValues.value[column.name].filter((value, index, self) => self.indexOf(value) !== index).length > 0) {
139+
if (currentValues.value[column.name].filter((value: any, index: any, self: any) => self.indexOf(value) !== index).length > 0) {
140140
return t('Array cannot contain duplicate items');
141141
}
142142
}
143143
144-
return currentValues.value[column.name] && currentValues.value[column.name].reduce((error, item) => {
145-
return error || validateValue(column.isArray.itemType, item, column) ||
146-
(item === null || !item.toString() ? t('Array cannot contain empty items') : null);
144+
return currentValues.value[column.name] && currentValues.value[column.name].reduce((error: any, item: any) => {
145+
if (column.isArray) {
146+
return error || validateValue(column.isArray.itemType, item, column) ||
147+
(item === null || !item.toString() ? t('Array cannot contain empty items') : null);
148+
} else {
149+
return error || false;
150+
}
147151
}, null);
148152
} else {
149-
return validateValue(column.type, currentValues.value[column.name], column);
153+
if (column.type) {
154+
return validateValue(column.type, currentValues.value[column.name], column);
155+
}
150156
}
151157
152158
return null;
153159
});
154160
return val.value;
155161
};
156162
157-
const validateValue = (type, value, column) => {
163+
const validateValue = (type: string, value: any, column: AdminForthResourceColumnCommon) => {
158164
if (type === 'string' || type === 'text') {
159165
if (column.maxLength && value?.length > column.maxLength) {
160166
return t('This field must be shorter than {maxLength} characters', { maxLength: column.maxLength });
161167
}
162168
163169
if (column.minLength && value?.length < column.minLength) {
164170
// if column.required[mode.value] is false, then we check if the field is empty
165-
let needToCheckEmpty = column.required[mode.value] || value?.length > 0;
171+
let needToCheckEmpty = column.required?.[mode.value] || value?.length > 0;
166172
if (!needToCheckEmpty) {
167173
return null;
168174
}
@@ -191,10 +197,10 @@ const validateValue = (type, value, column) => {
191197
};
192198
193199
194-
const setCurrentValue = (key, value, index = null) => {
200+
const setCurrentValue = (key: any, value: any, index = null) => {
195201
const col = props.resource.columns.find((column) => column.name === key);
196202
// if field is an array, we need to update the array or individual element
197-
if (col.type === 'json' && col.isArray?.enabled) {
203+
if (((col?.type && col.type === 'json') && (col?.type && col.isArray?.enabled))) {
198204
if (index === null) {
199205
currentValues.value[key] = value;
200206
} else if (index === currentValues.value[key].length) {
@@ -209,12 +215,12 @@ const setCurrentValue = (key, value, index = null) => {
209215
} else {
210216
currentValues.value[key][index] = value;
211217
}
212-
if (['text', 'richtext', 'string'].includes(col.isArray.itemType) && col.enforceLowerCase) {
218+
if (col?.isArray && ['text', 'richtext', 'string'].includes(col.isArray.itemType) && col.enforceLowerCase) {
213219
currentValues.value[key][index] = currentValues.value[key][index].toLowerCase();
214220
}
215221
}
216222
} else {
217-
if (['integer', 'float', 'decimal'].includes(col.type)) {
223+
if (col?.type && ['integer', 'float', 'decimal'].includes(col.type)) {
218224
if (value || value === 0) {
219225
currentValues.value[key] = +value;
220226
} else {
@@ -223,7 +229,7 @@ const setCurrentValue = (key, value, index = null) => {
223229
} else {
224230
currentValues.value[key] = value;
225231
}
226-
if (['text', 'richtext', 'string'].includes(col.type) && col.enforceLowerCase) {
232+
if (col?.type && ['text', 'richtext', 'string'].includes(col?.type) && col.enforceLowerCase) {
227233
currentValues.value[key] = currentValues.value[key].toLowerCase();
228234
}
229235
}
@@ -265,7 +271,7 @@ onMounted(() => {
265271
currentValues.value = Object.assign({}, props.record);
266272
// json values should transform to string
267273
props.resource.columns.forEach((column) => {
268-
if (column.type === 'json') {
274+
if (column.type === 'json' && currentValues.value) {
269275
if (column.isArray?.enabled) {
270276
// if value is null or undefined, we should set it to empty array
271277
if (!currentValues.value[column.name]) {
@@ -316,7 +322,7 @@ async function searchOptions(columnName: string, searchTerm: string) {
316322
317323
318324
const editableColumns = computed(() => {
319-
return props.resource?.columns?.filter(column => column.showIn[mode.value]);
325+
return props.resource?.columns?.filter(column => column.showIn?.[mode.value]);
320326
});
321327
322328
const isValid = computed(() => {
@@ -326,12 +332,14 @@ const isValid = computed(() => {
326332
327333
const groups = computed(() => {
328334
let fieldGroupType;
329-
if (mode.value === 'edit' && coreStore.resource.options?.editFieldGroups !== undefined) {
330-
fieldGroupType = coreStore.resource.options.editFieldGroups;
331-
} else if (mode.value === 'create' && coreStore.resource.options?.createFieldGroups !== undefined) {
332-
fieldGroupType = coreStore.resource.options.createFieldGroups;
333-
} else {
334-
fieldGroupType = coreStore.resource.options?.fieldGroups;
335+
if(coreStore.resource){
336+
if (mode.value === 'edit' && coreStore.resource.options?.editFieldGroups !== undefined) {
337+
fieldGroupType = coreStore.resource.options.editFieldGroups;
338+
} else if (mode.value === 'create' && coreStore.resource.options?.createFieldGroups !== undefined) {
339+
fieldGroupType = coreStore.resource.options.createFieldGroups;
340+
} else {
341+
fieldGroupType = coreStore.resource.options?.fieldGroups;
342+
}
335343
}
336344
return fieldGroupType ?? [];
337345
});

0 commit comments

Comments
 (0)