adminforth 1.6.2-next.3 → 1.6.2-next.5

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 (93) hide show
  1. package/dist/index.d.ts.map +1 -1
  2. package/dist/index.js +22 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/spa/.eslintrc.cjs +14 -0
  5. package/dist/spa/README.md +39 -0
  6. package/dist/spa/env.d.ts +1 -0
  7. package/dist/spa/index.html +23 -0
  8. package/dist/spa/package-lock.json +5062 -0
  9. package/dist/spa/package.json +58 -0
  10. package/dist/spa/postcss.config.js +6 -0
  11. package/dist/spa/public/assets/favicon.png +0 -0
  12. package/dist/spa/src/App.vue +432 -0
  13. package/dist/spa/src/adminforth.ts +160 -0
  14. package/dist/spa/src/afcl/AreaChart.vue +160 -0
  15. package/dist/spa/src/afcl/BarChart.vue +170 -0
  16. package/dist/spa/src/afcl/Button.vue +27 -0
  17. package/dist/spa/src/afcl/Checkbox.vue +24 -0
  18. package/dist/spa/src/afcl/Dropzone.vue +128 -0
  19. package/dist/spa/src/afcl/Input.vue +41 -0
  20. package/dist/spa/src/afcl/Link.vue +17 -0
  21. package/dist/spa/src/afcl/LinkButton.vue +25 -0
  22. package/dist/spa/src/afcl/PieChart.vue +175 -0
  23. package/dist/spa/src/afcl/ProgressBar.vue +57 -0
  24. package/dist/spa/src/afcl/Select.vue +246 -0
  25. package/dist/spa/src/afcl/Skeleton.vue +26 -0
  26. package/dist/spa/src/afcl/Spinner.vue +9 -0
  27. package/dist/spa/src/afcl/Table.vue +116 -0
  28. package/dist/spa/src/afcl/Tooltip.vue +43 -0
  29. package/dist/spa/src/afcl/VerticalTabs.vue +49 -0
  30. package/dist/spa/src/afcl/index.ts +20 -0
  31. package/dist/spa/src/assets/base.css +2 -0
  32. package/dist/spa/src/assets/logo.svg +19 -0
  33. package/dist/spa/src/components/AcceptModal.vue +44 -0
  34. package/dist/spa/src/components/Breadcrumbs.vue +41 -0
  35. package/dist/spa/src/components/BreadcrumbsWithButtons.vue +25 -0
  36. package/dist/spa/src/components/CustomDatePicker.vue +180 -0
  37. package/dist/spa/src/components/CustomDateRangePicker.vue +218 -0
  38. package/dist/spa/src/components/CustomRangePicker.vue +156 -0
  39. package/dist/spa/src/components/Filters.vue +232 -0
  40. package/dist/spa/src/components/GroupsTable.vue +218 -0
  41. package/dist/spa/src/components/HelloWorld.vue +17 -0
  42. package/dist/spa/src/components/MenuLink.vue +41 -0
  43. package/dist/spa/src/components/ResourceForm.vue +260 -0
  44. package/dist/spa/src/components/ResourceListTable.vue +486 -0
  45. package/dist/spa/src/components/ShowTable.vue +81 -0
  46. package/dist/spa/src/components/SingleSkeletLoader.vue +13 -0
  47. package/dist/spa/src/components/SkeleteLoader.vue +18 -0
  48. package/dist/spa/src/components/ThreeDotsMenu.vue +43 -0
  49. package/dist/spa/src/components/Toast.vue +78 -0
  50. package/dist/spa/src/components/ValueRenderer.vue +141 -0
  51. package/dist/spa/src/components/icons/IconCalendar.vue +5 -0
  52. package/dist/spa/src/components/icons/IconCommunity.vue +7 -0
  53. package/dist/spa/src/components/icons/IconDocumentation.vue +7 -0
  54. package/dist/spa/src/components/icons/IconEcosystem.vue +7 -0
  55. package/dist/spa/src/components/icons/IconSupport.vue +7 -0
  56. package/dist/spa/src/components/icons/IconTime.vue +5 -0
  57. package/dist/spa/src/components/icons/IconTooling.vue +19 -0
  58. package/dist/spa/src/composables/useFrontendApi.ts +28 -0
  59. package/dist/spa/src/i18n.ts +54 -0
  60. package/dist/spa/src/index.scss +34 -0
  61. package/dist/spa/src/main.ts +22 -0
  62. package/dist/spa/src/renderers/CompactField.vue +46 -0
  63. package/dist/spa/src/renderers/CompactUUID.vue +46 -0
  64. package/dist/spa/src/renderers/CountryFlag.vue +65 -0
  65. package/dist/spa/src/renderers/HumanNumber.vue +58 -0
  66. package/dist/spa/src/renderers/RelativeTime.vue +42 -0
  67. package/dist/spa/src/renderers/URL.vue +18 -0
  68. package/dist/spa/src/router/index.ts +70 -0
  69. package/dist/spa/src/spa_types/core.ts +51 -0
  70. package/dist/spa/src/stores/core.ts +228 -0
  71. package/dist/spa/src/stores/filters.ts +27 -0
  72. package/dist/spa/src/stores/modal.ts +48 -0
  73. package/dist/spa/src/stores/toast.ts +30 -0
  74. package/dist/spa/src/stores/user.ts +79 -0
  75. package/dist/spa/src/types/Adapters.ts +26 -0
  76. package/dist/spa/src/types/Back.ts +1344 -0
  77. package/dist/spa/src/types/Common.ts +940 -0
  78. package/dist/spa/src/types/FrontendAPI.ts +189 -0
  79. package/dist/spa/src/utils.ts +184 -0
  80. package/dist/spa/src/views/CreateView.vue +167 -0
  81. package/dist/spa/src/views/EditView.vue +171 -0
  82. package/dist/spa/src/views/ListView.vue +442 -0
  83. package/dist/spa/src/views/LoginView.vue +199 -0
  84. package/dist/spa/src/views/PageNotFound.vue +20 -0
  85. package/dist/spa/src/views/ResourceParent.vue +50 -0
  86. package/dist/spa/src/views/ShowView.vue +209 -0
  87. package/dist/spa/src/websocket.ts +129 -0
  88. package/dist/spa/tailwind.config.js +19 -0
  89. package/dist/spa/tsconfig.app.json +14 -0
  90. package/dist/spa/tsconfig.json +11 -0
  91. package/dist/spa/tsconfig.node.json +19 -0
  92. package/dist/spa/vite.config.ts +52 -0
  93. package/package.json +1 -1
@@ -0,0 +1,189 @@
1
+ import type { AdminForthFilterOperators } from "./Common.js";
2
+
3
+
4
+
5
+ export type FilterParams = {
6
+ /**
7
+ * Field of resource to filter
8
+ */
9
+ field: string;
10
+ /**
11
+ * Operator of filter
12
+ */
13
+ operator: AdminForthFilterOperators;
14
+ /**
15
+ * Value of filter
16
+ */
17
+ value: string | number | boolean ;
18
+ }
19
+
20
+ export interface FrontendAPIInterface {
21
+
22
+ /**
23
+ * Show a confirmation dialog
24
+ *
25
+ * The dialog will be displayed to the user
26
+ *
27
+ * Example:
28
+ *
29
+ * ```ts
30
+ * import adminforth from '@/adminforth'
31
+ *
32
+ * const isConfirmed = await adminforth.confirm({message: 'Are you sure?', yes: 'Yes', no: 'No'})
33
+ * if (isConfirmed) {
34
+ * your code...
35
+ * }
36
+ * ```
37
+ *
38
+ * @param params - The parameters of the dialog
39
+ * @returns A promise that resolves when the user confirms the dialog
40
+ */
41
+ confirm(params:ConfirmParams ): Promise<void>;
42
+
43
+ /**
44
+ * Show an alert
45
+ *
46
+ * The alert will be displayed to the user
47
+ *
48
+ * Example:
49
+ *
50
+ * ```ts
51
+ * import adminforth from '@/adminforth'
52
+ *
53
+ * adminforth.alert({message: 'Hello', variant: 'success'})
54
+ * ```
55
+ *
56
+ * @param params - The parameters of the alert
57
+ */
58
+ alert(params:AlertParams): void;
59
+
60
+
61
+ list: {
62
+
63
+ /**
64
+ * Full refresh the list. Loader will be shown during fetching data. Will fully reload table data from the server. If new data available, it will be shown in the list.
65
+ */
66
+ refresh(): Promise<{ error? : string }>;
67
+
68
+ /**
69
+ * Silently Refresh existing rows in the list (without loader).
70
+ * Should be called when table data already loaded, otherwise method will return an error.
71
+ * If new data available, it will not appear in the list.
72
+ */
73
+ silentRefresh(): Promise<{ error? : string }>;
74
+
75
+ /**
76
+ * Refresh a specific row in the list without loader, row should be already shown in the list, otherwise method will return an error
77
+ */
78
+ silentRefreshRow (pk: any): Promise<{ error? : string }>;
79
+
80
+ /**
81
+ * Close the three dots dropdown
82
+ */
83
+ closeThreeDotsDropdown(): void;
84
+
85
+
86
+ /**
87
+ * Set a filter in the list
88
+ * Works only when user located on the list page.
89
+ * Can be used to set filter from charts or other components in pageInjections.
90
+ *
91
+ * Example:
92
+ *
93
+ * ```ts
94
+ * import adminforth from '@/adminforth'
95
+ *
96
+ * adminforth.list.setFilter({field: 'name', operator: 'ilike', value: 'john'})
97
+ * ```
98
+ *
99
+ * @param filter - The filter to set
100
+ */
101
+ setFilter(filter: FilterParams): void;
102
+
103
+ /**
104
+ * Update a filter in the list
105
+ *
106
+ * Example:
107
+ *
108
+ * ```ts
109
+ * import adminforth from '@/adminforth';
110
+ *
111
+ * adminforth.list.updateFilter({field: 'name', operator: 'ilike', value: 'john'})
112
+ * ```
113
+ *
114
+ * @param filter - The filter to update
115
+ */
116
+ updateFilter(filter: FilterParams): void;
117
+
118
+ /**
119
+ * Clear all filters from the list
120
+ */
121
+ clearFilters(): void;
122
+ }
123
+
124
+ menu: {
125
+ /**
126
+ * Refreshes the badges in the menu, by recalling the badge function for each menu item
127
+ */
128
+ refreshMenuBadges(): void;
129
+ }
130
+
131
+ /**
132
+ * Close the user menu dropdown
133
+ */
134
+ closeUserMenuDropdown(): void;
135
+ }
136
+
137
+ export type ConfirmParams = {
138
+ /**
139
+ * The message to display in the dialog
140
+ */
141
+ message?: string;
142
+ /**
143
+ * The text to display in the "accept" button
144
+ */
145
+ yes?: string;
146
+ /**
147
+ * The text to display in the "cancel" button
148
+ */
149
+ no?: string;
150
+
151
+ }
152
+
153
+ export type AlertParams = {
154
+ /**
155
+ * The message to display in the alert
156
+ */
157
+ message?: string;
158
+
159
+ /**
160
+ * The message to display in the alert as HTML (can be used instead of message)
161
+ */
162
+ messageHtml?: string;
163
+
164
+ /**
165
+ * The variant of the alert
166
+ */
167
+ variant?: AlertVariant | keyof typeof AlertVariant;
168
+
169
+ /**
170
+ * The timeout of the alert in seconds or 'unlimited' to keep the alert open until the user closes it.
171
+ * Default is 10 seconds;
172
+ */
173
+ timeout?: number | 'unlimited';
174
+
175
+ }
176
+
177
+
178
+
179
+ export enum AlertVariant {
180
+ danger = 'danger',
181
+ success = 'success',
182
+ warning = 'warning',
183
+ info = 'info'
184
+ }
185
+
186
+
187
+
188
+
189
+
@@ -0,0 +1,184 @@
1
+ import { onMounted, ref, resolveComponent } from 'vue';
2
+ import type { CoreConfig } from './spa_types/core';
3
+ import type { ValidationObject } from './types/AdminForthConfig';
4
+ import router from "./router";
5
+ import { useCoreStore } from './stores/core';
6
+ import { useUserStore } from './stores/user';
7
+ import { Dropdown } from 'flowbite';
8
+ import adminforth from './adminforth';
9
+
10
+ const LS_LANG_KEY = `afLanguage`;
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
+ 'accept-language': localStorage.getItem(LS_LANG_KEY) || 'en',
21
+ },
22
+ body: JSON.stringify(body),
23
+ };
24
+ const fullPath = `${import.meta.env.VITE_ADMINFORTH_PUBLIC_PATH || ''}${path}`;
25
+ try {
26
+ const r = await fetch(fullPath, options);
27
+ if (r.status == 401 ) {
28
+ useUserStore().unauthorize();
29
+ await router.push({ name: 'login' });
30
+ return null;
31
+ }
32
+ return await r.json();
33
+ } catch(e){
34
+ adminforth.alert({variant:'danger', message: window.i18n?.global?.t('Something went wrong, please try again later'),})
35
+ console.error(`error in callApi ${path}`,e);
36
+ }
37
+ }
38
+
39
+ export async function callAdminForthApi({ path, method, body=undefined, headers=undefined }: {
40
+ path: string,
41
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE',
42
+ body?: any,
43
+ headers?: Record<string, string>
44
+ }): Promise<any> {
45
+ try {
46
+ return callApi({path: `/adminapi/v1${path}`, method, body} );
47
+ } catch (e) {
48
+ console.error('error', e);
49
+ return { error: `Unexpected error: ${e}` };
50
+ }
51
+ }
52
+
53
+ export function getCustomComponent({ file, meta }: { file: string, meta?: any }) {
54
+ const name = file.replace(/@/g, '').replace(/\./g, '').replace(/\//g, '');
55
+ return resolveComponent(name);
56
+ }
57
+
58
+ export function getIcon(icon: string) {
59
+ // icon format is "feather:icon-name". We need to get IconName in pascal case
60
+ if (!icon.includes(':')) {
61
+ throw new Error('Icon name should be in format "icon-set:icon-name"');
62
+ }
63
+ const [iconSet, iconName] = icon.split(':');
64
+ const compName = 'Icon' + iconName.split('-').map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join('');
65
+ return resolveComponent(compName);
66
+ }
67
+
68
+ export const loadFile = (file: string) => {
69
+ if (file.startsWith('http')) {
70
+ return file;
71
+ }
72
+ let path;
73
+ let baseUrl = '';
74
+ if (file.startsWith('@/')) {
75
+ path = file.replace('@/', '');
76
+ baseUrl = new URL(`./${path}`, import.meta.url).href;
77
+ } else if (file.startsWith('@@/')) {
78
+ path = file.replace('@@/', '');
79
+ baseUrl = new URL(`./custom/${path}`, import.meta.url).href;
80
+ } else {
81
+ baseUrl = new URL(`./${file}`, import.meta.url).href;
82
+ }
83
+ return baseUrl;
84
+ }
85
+
86
+ export function checkEmptyValues(value: any, viewType: 'show' | 'list' ) {
87
+ const config: CoreConfig | {} = useCoreStore().config;
88
+ let emptyFieldPlaceholder = '';
89
+ if (config.emptyFieldPlaceholder) {
90
+ if(typeof config.emptyFieldPlaceholder === 'string') {
91
+ emptyFieldPlaceholder = config.emptyFieldPlaceholder;
92
+ } else {
93
+ emptyFieldPlaceholder = config.emptyFieldPlaceholder?.[viewType] || '';
94
+ }
95
+ if (value === null || value === undefined || value === '') {
96
+ return emptyFieldPlaceholder;
97
+ }
98
+ }
99
+ return value;
100
+ }
101
+
102
+ export function checkAcessByAllowedActions(allowedActions:any, action:any ) {
103
+ if (!allowedActions) {
104
+ console.warn('allowedActions not set');
105
+ return;
106
+ }
107
+ if(allowedActions[action] === false) {
108
+ console.warn(`Action ${action} is not allowed`);
109
+ router.back();
110
+ }
111
+ }
112
+
113
+ export function initThreeDotsDropdown() {
114
+ const threeDotsDropdown: HTMLElement | null = document.querySelector('#listThreeDotsDropdown');
115
+ if (threeDotsDropdown) {
116
+ // this resource has three dots dropdown
117
+ const dd = new Dropdown(
118
+ threeDotsDropdown,
119
+ document.querySelector('[data-dropdown-toggle="listThreeDotsDropdown"]') as HTMLElement,
120
+ );
121
+ adminforth.list.closeThreeDotsDropdown = () => {
122
+ dd.hide();
123
+ }
124
+ }
125
+ }
126
+
127
+ export function applyRegexValidation(value: any, validation: ValidationObject[] | undefined) {
128
+
129
+ if ( validation?.length ) {
130
+ const validationArray = validation;
131
+ for (let i = 0; i < validationArray.length; i++) {
132
+ if (validationArray[i].regExp) {
133
+ let flags = '';
134
+ if (validationArray[i].caseSensitive) {
135
+ flags += 'i';
136
+ }
137
+ if (validationArray[i].multiline) {
138
+ flags += 'm';
139
+ }
140
+ if (validationArray[i].global) {
141
+ flags += 'g';
142
+ }
143
+
144
+ const regExp = new RegExp(validationArray[i].regExp, flags);
145
+ if (value === undefined || value === null) {
146
+ value = '';
147
+ }
148
+ let valueS = `${value}`;
149
+
150
+ if (!regExp.test(valueS)) {
151
+ return validationArray[i].message;
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+
158
+ export function currentQuery() {
159
+ return router.currentRoute.value.query;
160
+ }
161
+
162
+ export function setQuery(query: any) {
163
+ const currentQuery = { ...router.currentRoute.value.query, ...query };
164
+ router.replace({
165
+ query: currentQuery,
166
+ });
167
+ }
168
+
169
+ export function verySimpleHash(str: string): string {
170
+ return `${str.split('').reduce((a, b)=>{a=((a<<5)-a)+b.charCodeAt(0);return a&a},0)}`;
171
+ }
172
+
173
+ export function humanifySize(size) {
174
+ if (!size) {
175
+ return '';
176
+ }
177
+ const units = ['B', 'KB', 'MB', 'GB', 'TB']
178
+ let i = 0
179
+ while (size >= 1024 && i < units.length - 1) {
180
+ size /= 1024
181
+ i++
182
+ }
183
+ return `${size.toFixed(1)} ${units[i]}`
184
+ }
@@ -0,0 +1,167 @@
1
+ <template>
2
+ <div class="relative w-full">
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
+ {{ $t('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"
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
+ {{ $t('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 lang="ts">
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
+ import adminforth from '@/adminforth';
89
+
90
+
91
+ const isValid = ref(false);
92
+ const validating = ref(false);
93
+
94
+ const loading = ref(true);
95
+ const saving = ref(false);
96
+
97
+ const route = useRoute();
98
+ const router = useRouter();
99
+
100
+ const record = ref({});
101
+
102
+ const coreStore = useCoreStore();
103
+
104
+ const initalValues = computed(() => {
105
+ if (!route.query.values) {
106
+ return {};
107
+ }
108
+ return JSON.parse(decodeURIComponent(route.query.values));
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
+ adminforth.alert({
160
+ message: 'Record created successfully',
161
+ variant: 'success'
162
+ });
163
+ }
164
+ }
165
+
166
+
167
+ </script>
@@ -0,0 +1,171 @@
1
+ <template>
2
+ <div class="relative w-full">
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
+ {{ $t('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
+ {{ $t('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 lang="ts">
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
+ import adminforth from '@/adminforth';
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
+ source: 'edit',
128
+ });
129
+ checkAcessByAllowedActions(coreStore.resourceOptions.allowedActions,'edit');
130
+ loading.value = false;
131
+ });
132
+
133
+ async function saveRecord() {
134
+ if (!isValid.value) {
135
+ validating.value = true;
136
+ return;
137
+ } else {
138
+ validating.value = false;
139
+ }
140
+
141
+ saving.value = true;
142
+ const updates = {};
143
+ for (const key in record.value) {
144
+ if (record.value[key] !== coreStore.record[key]) {
145
+ updates[key] = record.value[key];
146
+ }
147
+ }
148
+
149
+ const resp = await callAdminForthApi({
150
+ method: 'POST',
151
+ path: `/update_record`,
152
+ body: {
153
+ resourceId: route.params.resourceId,
154
+ recordId: route.params.primaryKey,
155
+ record: updates,
156
+ },
157
+ });
158
+ if (resp.error) {
159
+ showErrorTost(resp.error);
160
+ } else {
161
+ adminforth.alert({
162
+ message: 'Record updated successfully',
163
+ variant: 'success',
164
+ timeout: 400000
165
+ });
166
+ }
167
+ saving.value = false;
168
+ router.push({ name: 'resource-show', params: { resourceId: route.params.resourceId, primaryKey: coreStore.record[coreStore.primaryKey] } });
169
+ }
170
+
171
+ </script>