adminforth 1.3.55-next.0 → 1.3.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/spa/.eslintrc.cjs +14 -0
  2. package/dist/spa/README.md +39 -0
  3. package/dist/spa/env.d.ts +1 -0
  4. package/dist/spa/index.html +23 -0
  5. package/dist/spa/package-lock.json +4659 -0
  6. package/dist/spa/package.json +52 -0
  7. package/dist/spa/postcss.config.js +6 -0
  8. package/dist/spa/public/assets/favicon.png +0 -0
  9. package/dist/spa/src/App.vue +418 -0
  10. package/dist/spa/src/assets/base.css +2 -0
  11. package/dist/spa/src/assets/logo.svg +19 -0
  12. package/dist/spa/src/components/AcceptModal.vue +45 -0
  13. package/dist/spa/src/components/Breadcrumbs.vue +41 -0
  14. package/dist/spa/src/components/BreadcrumbsWithButtons.vue +26 -0
  15. package/dist/spa/src/components/CustomDatePicker.vue +176 -0
  16. package/dist/spa/src/components/CustomDateRangePicker.vue +218 -0
  17. package/dist/spa/src/components/CustomRangePicker.vue +156 -0
  18. package/dist/spa/src/components/Dropdown.vue +168 -0
  19. package/dist/spa/src/components/Filters.vue +222 -0
  20. package/dist/spa/src/components/HelloWorld.vue +17 -0
  21. package/dist/spa/src/components/MenuLink.vue +27 -0
  22. package/dist/spa/src/components/ResourceForm.vue +325 -0
  23. package/dist/spa/src/components/ResourceListTable.vue +466 -0
  24. package/dist/spa/src/components/SingleSkeletLoader.vue +13 -0
  25. package/dist/spa/src/components/SkeleteLoader.vue +23 -0
  26. package/dist/spa/src/components/ThreeDotsMenu.vue +43 -0
  27. package/dist/spa/src/components/Toast.vue +78 -0
  28. package/dist/spa/src/components/ValueRenderer.vue +141 -0
  29. package/dist/spa/src/components/icons/IconCalendar.vue +5 -0
  30. package/dist/spa/src/components/icons/IconCommunity.vue +7 -0
  31. package/dist/spa/src/components/icons/IconDocumentation.vue +7 -0
  32. package/dist/spa/src/components/icons/IconEcosystem.vue +7 -0
  33. package/dist/spa/src/components/icons/IconSupport.vue +7 -0
  34. package/dist/spa/src/components/icons/IconTime.vue +5 -0
  35. package/dist/spa/src/components/icons/IconTooling.vue +19 -0
  36. package/dist/spa/src/composables/useFrontendApi.ts +26 -0
  37. package/dist/spa/src/composables/useStores.ts +131 -0
  38. package/dist/spa/src/index.scss +31 -0
  39. package/dist/spa/src/main.ts +18 -0
  40. package/dist/spa/src/renderers/CompactUUID.vue +48 -0
  41. package/dist/spa/src/renderers/CountryFlag.vue +69 -0
  42. package/dist/spa/src/router/index.ts +59 -0
  43. package/dist/spa/src/spa_types/core.ts +53 -0
  44. package/dist/spa/src/stores/core.ts +148 -0
  45. package/dist/spa/src/stores/filters.ts +27 -0
  46. package/dist/spa/src/stores/modal.ts +48 -0
  47. package/dist/spa/src/stores/toast.ts +31 -0
  48. package/dist/spa/src/stores/user.ts +72 -0
  49. package/dist/spa/src/types/AdminForthConfig.ts +1762 -0
  50. package/dist/spa/src/types/FrontendAPI.ts +143 -0
  51. package/dist/spa/src/utils.ts +160 -0
  52. package/dist/spa/src/views/CreateView.vue +167 -0
  53. package/dist/spa/src/views/EditView.vue +170 -0
  54. package/dist/spa/src/views/ListView.vue +352 -0
  55. package/dist/spa/src/views/LoginView.vue +192 -0
  56. package/dist/spa/src/views/ResourceParent.vue +17 -0
  57. package/dist/spa/src/views/ShowView.vue +194 -0
  58. package/dist/spa/tailwind.config.js +17 -0
  59. package/dist/spa/tsconfig.app.json +14 -0
  60. package/dist/spa/tsconfig.json +11 -0
  61. package/dist/spa/tsconfig.node.json +19 -0
  62. package/dist/spa/vite.config.ts +56 -0
  63. package/package.json +2 -2
@@ -0,0 +1,143 @@
1
+
2
+ export interface FrontendAPIInterface {
3
+
4
+ /**
5
+ * Show a confirmation dialog
6
+ *
7
+ * The dialog will be displayed to the user
8
+ *
9
+ * Example:
10
+ *
11
+ * ```ts
12
+ * const isConfirmed = await window.adminforth.confirm({message: 'Are you sure?', yes: 'Yes', no: 'No'})
13
+ * if (isConfirmed) {
14
+ * your code...
15
+ * }
16
+ * ```
17
+ *
18
+ * @param params - The parameters of the dialog
19
+ * @returns A promise that resolves when the user confirms the dialog
20
+ */
21
+ confirm(params:ConfirmParams ): Promise<void>;
22
+
23
+ /**
24
+ * Show an alert
25
+ *
26
+ * The alert will be displayed to the user
27
+ *
28
+ * Example:
29
+ *
30
+ * ```ts
31
+ * window.adminforth.alert({message: 'Hello', variant: 'success'})
32
+ * ```
33
+ *
34
+ * @param params - The parameters of the alert
35
+ */
36
+ alert(params:AlertParams): void;
37
+
38
+
39
+ list: {
40
+
41
+ /**
42
+ * Refresh the list
43
+ */
44
+ refresh(): void;
45
+
46
+ /**
47
+ * Close the three dots dropdown
48
+ */
49
+ closeThreeDotsDropdown(): void;
50
+
51
+ /**
52
+ * Close the user menu dropdown
53
+ */
54
+ closeUserMenuDropdown(): void;
55
+
56
+ /**
57
+ * Set a filter in the list
58
+ * Works only when user located on the list page.
59
+ * Can be used to set filter from charts or other components in pageInjections.
60
+ *
61
+ * Example:
62
+ *
63
+ * ```ts
64
+ * window.adminforth.list.setFilter({field: 'name', operator: 'ilike', value: 'john'})
65
+ * ```
66
+ *
67
+ * @param filter - The filter to set
68
+ */
69
+ setFilter(filter: any): void;
70
+
71
+ /**
72
+ * Update a filter in the list
73
+ *
74
+ * Example:
75
+ *
76
+ * ```ts
77
+ * window.adminforth.list.updateFilter({field: 'name', operator: 'ilike', value: 'john'})
78
+ * ```
79
+ *
80
+ * @param filter - The filter to update
81
+ */
82
+ updateFilter(filter: any): void;
83
+
84
+ /**
85
+ * Clear all filters from the list
86
+ */
87
+ clearFilters(): void;
88
+ }
89
+ }
90
+
91
+ export type ConfirmParams = {
92
+ /**
93
+ * The message to display in the dialog
94
+ */
95
+ message?: string;
96
+ /**
97
+ * The text to display in the "accept" button
98
+ */
99
+ yes?: string;
100
+ /**
101
+ * The text to display in the "cancel" button
102
+ */
103
+ no?: string;
104
+
105
+ }
106
+
107
+ export type AlertParams = {
108
+ /**
109
+ * The message to display in the alert
110
+ */
111
+ message?: string;
112
+
113
+ /**
114
+ * The message to display in the alert as HTML (can be used instead of message)
115
+ */
116
+ messageHtml?: string;
117
+
118
+ /**
119
+ * The variant of the alert
120
+ */
121
+ variant?: AlertVariant | keyof typeof AlertVariant;
122
+
123
+ /**
124
+ * The timeout of the alert in seconds or 'unlimited' to keep the alert open until the user closes it.
125
+ * Default is 10 seconds;
126
+ */
127
+ timeout?: number | 'unlimited';
128
+
129
+ }
130
+
131
+
132
+
133
+ export enum AlertVariant {
134
+ danger = 'danger',
135
+ success = 'success',
136
+ warning = 'warning',
137
+ info = 'info'
138
+ }
139
+
140
+
141
+
142
+
143
+
@@ -0,0 +1,160 @@
1
+ import { onMounted, ref, resolveComponent } from 'vue';
2
+ import type { CoreConfig } from './spa_types/core';
3
+ import type { ValidationObject } from './types/AdminForthConfig';
4
+
5
+
6
+ import router from "./router";
7
+ import { useCoreStore } from './stores/core';
8
+ import { useUserStore } from './stores/user';
9
+ import { Dropdown } from 'flowbite';
10
+
11
+
12
+ export async function callApi({path, method, body=undefined}: {
13
+ path: string, method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
14
+ body?: any
15
+ }): Promise<any> {
16
+ const options = {
17
+ method,
18
+ headers: {
19
+ 'Content-Type': 'application/json',
20
+ },
21
+ body: JSON.stringify(body),
22
+ };
23
+ const fullPath = `${import.meta.env.VITE_ADMINFORTH_PUBLIC_PATH || ''}${path}`;
24
+ const r = await fetch(fullPath, options);
25
+ if (r.status == 401 ) {
26
+ useUserStore().unauthorize();
27
+ router.push({ name: 'login' });
28
+ return null;
29
+ }
30
+ return await r.json();
31
+ }
32
+
33
+ export async function callAdminForthApi({ path, method, body=undefined }: {
34
+ path: string,
35
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE',
36
+ body?: any
37
+ }) {
38
+ try {
39
+ return callApi({path: `/adminapi/v1${path}`, method, body} );
40
+ } catch (e) {
41
+ console.error('error', e);
42
+ return { error: `Unexpected error: ${e}` };
43
+ }
44
+ }
45
+
46
+ export function getCustomComponent({ file, meta }: { file: string, meta: any }) {
47
+ const name = file.replace(/@/g, '').replace(/\./g, '').replace(/\//g, '');
48
+ return resolveComponent(name);
49
+ }
50
+
51
+ export function getIcon(icon: string) {
52
+ // icon format is "feather:icon-name". We need to get IconName in pascal case
53
+ if (!icon.includes(':')) {
54
+ throw new Error('Icon name should be in format "icon-set:icon-name"');
55
+ }
56
+ const [iconSet, iconName] = icon.split(':');
57
+ const compName = 'Icon' + iconName.split('-').map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join('');
58
+ return resolveComponent(compName);
59
+ }
60
+
61
+ export const loadFile = (file: string) => {
62
+ if (file.startsWith('http')) {
63
+ return file;
64
+ }
65
+ let path;
66
+ let baseUrl = '';
67
+ if (file.startsWith('@/')) {
68
+ path = file.replace('@/', '');
69
+ baseUrl = new URL(`./${path}`, import.meta.url).href;
70
+ } else if (file.startsWith('@@/')) {
71
+ path = file.replace('@@/', '');
72
+ baseUrl = new URL(`./custom/${path}`, import.meta.url).href;
73
+ } else {
74
+ baseUrl = new URL(`./${file}`, import.meta.url).href;
75
+ }
76
+ return baseUrl;
77
+ }
78
+
79
+ export function checkEmptyValues(value: any, viewType: 'show' | 'list' ) {
80
+ const config: CoreConfig | {} = useCoreStore().config;
81
+ let emptyFieldPlaceholder = '';
82
+ if (config.emptyFieldPlaceholder) {
83
+ if(typeof config.emptyFieldPlaceholder === 'string') {
84
+ emptyFieldPlaceholder = config.emptyFieldPlaceholder;
85
+ } else {
86
+ emptyFieldPlaceholder = config.emptyFieldPlaceholder?.[viewType] || '';
87
+ }
88
+ if (value === null || value === undefined || value === '') {
89
+ return emptyFieldPlaceholder;
90
+ }
91
+ }
92
+ return value;
93
+ }
94
+
95
+ export function checkAcessByAllowedActions(allowedActions:any, action:any ) {
96
+ if (!allowedActions) {
97
+ console.warn('allowedActions not set');
98
+ return;
99
+ }
100
+ if(allowedActions[action] === false) {
101
+ console.warn(`Action ${action} is not allowed`);
102
+ router.back();
103
+ }
104
+ }
105
+
106
+ export function initThreeDotsDropdown() {
107
+ const threeDotsDropdown: HTMLElement | null = document.querySelector('#listThreeDotsDropdown');
108
+ if (threeDotsDropdown) {
109
+ // this resource has three dots dropdown
110
+ const dd = new Dropdown(
111
+ threeDotsDropdown,
112
+ document.querySelector('[data-dropdown-toggle="listThreeDotsDropdown"]') as HTMLElement,
113
+ );
114
+ window.adminforth.list.closeThreeDotsDropdown = () => {
115
+ dd.hide();
116
+ }
117
+ }
118
+ }
119
+
120
+ export function applyRegexValidation(value: any, validation: ValidationObject[] | undefined) {
121
+
122
+ if ( validation?.length ) {
123
+ const validationArray = validation;
124
+ for (let i = 0; i < validationArray.length; i++) {
125
+ if (validationArray[i].regExp) {
126
+ let flags = '';
127
+ if (validationArray[i].caseSensitive) {
128
+ flags += 'i';
129
+ }
130
+ if (validationArray[i].multiline) {
131
+ flags += 'm';
132
+ }
133
+ if (validationArray[i].global) {
134
+ flags += 'g';
135
+ }
136
+
137
+ const regExp = new RegExp(validationArray[i].regExp, flags);
138
+ if (value === undefined || value === null) {
139
+ value = '';
140
+ }
141
+ let valueS = `${value}`;
142
+
143
+ if (!regExp.test(valueS)) {
144
+ return validationArray[i].message;
145
+ }
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ export function currentQuery() {
152
+ return router.currentRoute.value.query;
153
+ }
154
+
155
+ export function setQuery(query: any) {
156
+ const currentQuery = { ...router.currentRoute.value.query, ...query };
157
+ router.replace({
158
+ query: currentQuery,
159
+ });
160
+ }
@@ -0,0 +1,167 @@
1
+ <template>
2
+ <div class="relative">
3
+
4
+ <component
5
+ v-for="c in coreStore?.resourceOptions?.pageInjections?.create?.beforeBreadcrumbs || []"
6
+ :is="getCustomComponent(c)"
7
+ :meta="c.meta"
8
+ :record="coreStore.record"
9
+ :resource="coreStore.resource"
10
+ :adminUser="coreStore.adminUser"
11
+ />
12
+
13
+ <BreadcrumbsWithButtons>
14
+ <!-- save and cancle -->
15
+ <button @click="$router.back()"
16
+ class="flex items-center py-1 px-3 me-2 text-sm font-medium rounded-default text-gray-900 focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700"
17
+ >
18
+ Cancel
19
+ </button>
20
+
21
+ <button
22
+ @click="saveRecord"
23
+ class="flex items-center py-1 px-3 text-sm font-medium rounded-default text-red-600 focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-red-700 focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-red-500 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 disabled:opacity-50"
24
+ :disabled="saving || (validating && !isValid)"
25
+ >
26
+ <svg v-if="saving" x
27
+ aria-hidden="true" class="w-4 h-4 mr-1 text-gray-200 animate-spin dark:text-gray-600 fill-red-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
28
+
29
+ <IconFloppyDiskSolid v-else class="w-4 h-4 mr-1" />
30
+ Save
31
+ </button>
32
+
33
+ <ThreeDotsMenu
34
+ :threeDotsDropdownItems="coreStore.resourceOptions?.pageInjections?.create?.threeDotsDropdownItems"
35
+ ></ThreeDotsMenu>
36
+
37
+ </BreadcrumbsWithButtons>
38
+
39
+ <component
40
+ v-for="c in coreStore?.resourceOptions?.pageInjections?.create?.afterBreadcrumbs || []"
41
+ :is="getCustomComponent(c)"
42
+ :meta="c.meta"
43
+ :record="coreStore.record"
44
+ :resource="coreStore.resource"
45
+ :adminUser="coreStore.adminUser"
46
+ />
47
+
48
+ <SingleSkeletLoader v-if="loading"></SingleSkeletLoader>
49
+
50
+
51
+ <ResourceForm
52
+ v-else
53
+ :record="record"
54
+ :resource="coreStore.resource"
55
+ @update:record="onUpdateRecord"
56
+ @update:isValid="isValid = $event"
57
+ :validating="validating"
58
+ :source="'create'"
59
+ >
60
+ </ResourceForm>
61
+
62
+ <component
63
+ v-for="c in coreStore?.resourceOptions?.pageInjections?.create?.bottom || []"
64
+ :is="getCustomComponent(c)"
65
+ :meta="c.meta"
66
+ :record="coreStore.record"
67
+ :resource="coreStore.resource"
68
+ :adminUser="coreStore.adminUser"
69
+ />
70
+
71
+ </div>
72
+ </template>
73
+
74
+
75
+ <script setup>
76
+
77
+ import BreadcrumbsWithButtons from '@/components/BreadcrumbsWithButtons.vue';
78
+ import ResourceForm from '@/components/ResourceForm.vue';
79
+ import SingleSkeletLoader from '@/components/SingleSkeletLoader.vue';
80
+ import { useCoreStore } from '@/stores/core';
81
+ import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown } from '@/utils';
82
+ import { IconFloppyDiskSolid } from '@iconify-prerendered/vue-flowbite';
83
+ import { onMounted, ref, watch } from 'vue';
84
+ import { useRoute, useRouter } from 'vue-router';
85
+ import { computed } from 'vue';
86
+ import { showErrorTost } from '@/composables/useFrontendApi';
87
+ import ThreeDotsMenu from '@/components/ThreeDotsMenu.vue';
88
+
89
+
90
+ const isValid = ref(false);
91
+ const validating = ref(false);
92
+
93
+ const loading = ref(true);
94
+ const saving = ref(false);
95
+
96
+ const route = useRoute();
97
+ const router = useRouter();
98
+
99
+ const record = ref({});
100
+
101
+ const coreStore = useCoreStore();
102
+
103
+ const initalValues = computed(() => {
104
+ if (!route.query.values) {
105
+ return {};
106
+ }
107
+ return JSON.parse(decodeURIComponent(route.query.values));
108
+ });
109
+
110
+
111
+
112
+ async function onUpdateRecord(newRecord) {
113
+ console.log('newRecord', newRecord);
114
+ record.value = newRecord;
115
+ }
116
+
117
+ onMounted(async () => {
118
+ loading.value = true;
119
+ record.value = initalValues.value;
120
+ await coreStore.fetchResourceFull({
121
+ resourceId: route.params.resourceId
122
+ });
123
+ loading.value = false;
124
+ checkAcessByAllowedActions(coreStore.resourceOptions.allowedActions,'create');
125
+ initThreeDotsDropdown();
126
+ });
127
+
128
+ async function saveRecord() {
129
+ console.log('saveRecord isValid', isValid.value);
130
+ if (!isValid.value) {
131
+ validating.value = true;
132
+ return;
133
+ } else {
134
+ validating.value = false;
135
+ }
136
+ saving.value = true;
137
+ const response = await callAdminForthApi({
138
+ method: 'POST',
139
+ path: `/create_record`,
140
+ body: {
141
+ resourceId: route.params.resourceId,
142
+ record: record.value,
143
+ },
144
+ });
145
+ if (response.error) {
146
+ showErrorTost(response.error);
147
+ }
148
+ saving.value = false;
149
+ if (route.query.returnTo) {
150
+ router.push(route.query.returnTo);
151
+ } else {
152
+ router.push({
153
+ name: 'resource-show',
154
+ params: {
155
+ resourceId: route.params.resourceId,
156
+ primaryKey: response.newRecordId
157
+ }
158
+ });
159
+ window.adminforth.alert({
160
+ message: 'Record created successfully',
161
+ variant: 'success'
162
+ });
163
+ }
164
+ }
165
+
166
+
167
+ </script>
@@ -0,0 +1,170 @@
1
+ <template>
2
+ <div class="relative">
3
+ <component
4
+ v-for="c in coreStore?.resourceOptions?.pageInjections?.edit?.beforeBreadcrumbs || []"
5
+ :is="getCustomComponent(c)"
6
+ :meta="c.meta"
7
+ :record="editableRecord"
8
+ :resource="coreStore.resource"
9
+ :adminUser="coreStore.adminUser"
10
+ />
11
+
12
+ <BreadcrumbsWithButtons>
13
+ <!-- save and cancle -->
14
+ <button @click="$router.back()"
15
+ class="flex items-center py-1 px-3 me-2 text-sm font-medium text-gray-900 rounded-default focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700"
16
+ >
17
+ Cancel
18
+ </button>
19
+
20
+ <button
21
+ @click="saveRecord"
22
+ class="flex items-center py-1 px-3 text-sm font-medium rounded-default text-red-600 focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-red-700 focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-red-500 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 disabled:opacity-50"
23
+ :disabled="saving || (validating && !isValid)"
24
+ >
25
+ <IconFloppyDiskSolid class="w-4 h-4" />
26
+ Save
27
+ </button>
28
+
29
+ <ThreeDotsMenu
30
+ :threeDotsDropdownItems="coreStore.resourceOptions?.pageInjections?.edit?.threeDotsDropdownItems"
31
+ ></ThreeDotsMenu>
32
+
33
+ </BreadcrumbsWithButtons>
34
+
35
+ <component
36
+ v-for="c in coreStore?.resourceOptions?.pageInjections?.edit?.afterBreadcrumbs || []"
37
+ :is="getCustomComponent(c)"
38
+ :meta="c.meta"
39
+ :record="coreStore.record"
40
+ :resource="coreStore.resource"
41
+ :adminUser="coreStore.adminUser"
42
+ />
43
+
44
+ <SingleSkeletLoader v-if="loading"></SingleSkeletLoader>
45
+
46
+ <ResourceForm
47
+ v-else
48
+ :record="editableRecord"
49
+ :resource="coreStore.resource"
50
+ :adminUser="coreStore.adminUser"
51
+ @update:record="onUpdateRecord"
52
+ @update:isValid="isValid = $event"
53
+ :validating="validating"
54
+ :source="'edit'"
55
+ >
56
+ </ResourceForm>
57
+
58
+ <component
59
+ v-for="c in coreStore?.resourceOptions?.pageInjections?.edit?.bottom || []"
60
+ :is="getCustomComponent(c)"
61
+ :meta="c.meta"
62
+ :record="coreStore.record"
63
+ :resource="coreStore.resource"
64
+ :adminUser="coreStore.adminUser"
65
+ />
66
+
67
+ </div>
68
+ </template>
69
+
70
+
71
+ <script setup>
72
+
73
+ import BreadcrumbsWithButtons from '@/components/BreadcrumbsWithButtons.vue';
74
+ import ResourceForm from '@/components/ResourceForm.vue';
75
+ import SingleSkeletLoader from '@/components/SingleSkeletLoader.vue';
76
+ import { useCoreStore } from '@/stores/core';
77
+ import { callAdminForthApi, getCustomComponent,checkAcessByAllowedActions, initThreeDotsDropdown } from '@/utils';
78
+ import { IconFloppyDiskSolid } from '@iconify-prerendered/vue-flowbite';
79
+ import { computed, onMounted, ref } from 'vue';
80
+ import { useRoute, useRouter } from 'vue-router';
81
+ import { showErrorTost } from '@/composables/useFrontendApi';
82
+ import ThreeDotsMenu from '@/components/ThreeDotsMenu.vue';
83
+
84
+
85
+ const coreStore = useCoreStore();
86
+
87
+ const isValid = ref(false);
88
+ const validating = ref(false);
89
+
90
+ const route = useRoute();
91
+ const router = useRouter();
92
+
93
+ const loading = ref(true);
94
+
95
+ const saving = ref(false);
96
+
97
+ const record = ref({});
98
+
99
+ async function onUpdateRecord(newRecord) {
100
+ record.value = newRecord;
101
+ }
102
+
103
+ const editableRecord = computed(() => {
104
+ const newRecord = { ...coreStore.record };
105
+ if (!coreStore.resource) {
106
+ return {};
107
+ }
108
+ coreStore.resource.columns.forEach(column => {
109
+ if (column.foreignResource) {
110
+ newRecord[column.name] = newRecord[column.name]?.pk
111
+ }
112
+ });
113
+ return newRecord;
114
+ })
115
+
116
+ onMounted(async () => {
117
+ loading.value = true;
118
+
119
+ await coreStore.fetchResourceFull({
120
+ resourceId: route.params.resourceId
121
+ });
122
+ initThreeDotsDropdown();
123
+
124
+ await coreStore.fetchRecord({
125
+ resourceId: route.params.resourceId,
126
+ primaryKey: route.params.primaryKey,
127
+ });
128
+ checkAcessByAllowedActions(coreStore.resourceOptions.allowedActions,'edit');
129
+ loading.value = false;
130
+ });
131
+
132
+ async function saveRecord() {
133
+ if (!isValid.value) {
134
+ validating.value = true;
135
+ return;
136
+ } else {
137
+ validating.value = false;
138
+ }
139
+
140
+ saving.value = true;
141
+ const updates = {};
142
+ for (const key in record.value) {
143
+ if (record.value[key] !== coreStore.record[key]) {
144
+ updates[key] = record.value[key];
145
+ }
146
+ }
147
+
148
+ const resp = await callAdminForthApi({
149
+ method: 'POST',
150
+ path: `/update_record`,
151
+ body: {
152
+ resourceId: route.params.resourceId,
153
+ recordId: route.params.primaryKey,
154
+ record: updates,
155
+ },
156
+ });
157
+ if (resp.error) {
158
+ showErrorTost(resp.error);
159
+ } else {
160
+ window.adminforth.alert({
161
+ message: 'Record updated successfully',
162
+ variant: 'success',
163
+ timeout: 400000
164
+ });
165
+ }
166
+ saving.value = false;
167
+ router.push({ name: 'resource-show', params: { resourceId: route.params.resourceId, primaryKey: coreStore.record[coreStore.primaryKey] } });
168
+ }
169
+
170
+ </script>