-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcreateEditUtils.ts
More file actions
65 lines (62 loc) · 2.11 KB
/
Copy pathcreateEditUtils.ts
File metadata and controls
65 lines (62 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { AdminForthResourceColumn } from '@/types/Back';
import { useAdminforth } from '@/adminforth';
import { type Ref, nextTick } from 'vue';
export function scrollToInvalidField(resourceFormRef: any, t: (key: string) => string) {
const { alert } = useAdminforth();
let columnsWithErrors: {column: AdminForthResourceColumn, error: string}[] = [];
for (const column of resourceFormRef.value?.editableColumns || []) {
if (resourceFormRef.value?.columnsWithErrors[column.name]) {
columnsWithErrors.push({
column,
error: resourceFormRef.value?.columnsWithErrors[column.name]
});
}
}
const errorMessage = t('Failed to save. Please fix errors for the following fields:') + '<ul class="mt-2 list-disc list-inside">' + columnsWithErrors.map(c => `<li><strong>${c.column.label || c.column.name}</strong>: ${c.error}</li>`).join('') + '</ul>';
alert({
messageHtml: errorMessage,
variant: 'danger'
});
const firstInvalidElement = document.querySelector('.af-invalid-field-message');
if (firstInvalidElement) {
firstInvalidElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
export async function saveRecordPreparations(
viewMode: 'create' | 'edit',
validatingMode: Ref<boolean>,
resourceFormRef: Ref<any>,
isValid: Ref<boolean>,
t: (key: string) => string,
saving: Ref<boolean>,
runSaveInterceptors: any,
record: Ref<Record<string, any>>,
coreStore: any,
route: any
) {
validatingMode.value = true;
await nextTick();
//wait for response for the user validation function if it exists
while (1) {
if (resourceFormRef.value?.isValidating) {
await new Promise(resolve => setTimeout(resolve, 100));
} else {
break;
}
}
if (!isValid.value) {
await nextTick();
scrollToInvalidField(resourceFormRef, t);
return;
} else {
validatingMode.value = false;
}
saving.value = true;
const interceptorsResult = await runSaveInterceptors({
action: viewMode,
values: record.value,
resource: coreStore.resource,
resourceId: route.params.resourceId as string,
});
return interceptorsResult;
}