pgo-ui 1.1.21 → 1.1.22

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pgo-ui",
3
- "version": "1.1.21",
3
+ "version": "1.1.22",
4
4
  "description": "A Vue 3 component library with PGO design system",
5
5
  "private": false,
6
6
  "type": "module",
@@ -94,29 +94,28 @@
94
94
  </Form>
95
95
  <template #footer>
96
96
  <div class="flex justify-end gap-2 ">
97
- <Button v-if="mode == 'edit'"
98
- label="buttons.update"
97
+ <Button
98
+ v-if="formMode === 'create'"
99
+ label="buttons.draft"
99
100
  color="primary"
100
- :loading="isSubmitting"
101
- @click="handleSubmit('update')"
101
+ :loading="isSubmitting && clickedButton === 'draft'"
102
+ :disabled="isSubmitting || !valid"
103
+ @click="handleSubmit('draft')"
102
104
  />
103
105
  <template v-else>
104
- <Button
105
- v-if="items || draftSubmitted"
106
- label="buttons.submit"
107
- color="primary"
108
- :loading="isSubmitting"
109
- :disabled="!valid || isSubmitting"
110
-
111
- @click="handleSubmit('submitted')"
106
+ <Button
107
+ label="buttons.update"
108
+ color="warning"
109
+ :loading="isSubmitting && clickedButton === 'update'"
110
+ :disabled="isSubmitting || !isFormDirty || !valid"
111
+ @click="handleSubmit('update')"
112
112
  />
113
- <Button
114
- label="buttons.draft"
115
- color="primary"
116
- :loading="isSubmitting"
117
- :disabled="!valid || isSubmitting || draftSubmitted"
118
-
119
- @click="handleSubmit('draft')"
113
+ <Button
114
+ label="buttons.submit"
115
+ color="primary"
116
+ :loading="isSubmitting && clickedButton === 'submit'"
117
+ :disabled="isSubmitting || !valid"
118
+ @click="handleSubmit('submit')"
120
119
  />
121
120
  <!-- <Button v-if="items" label="buttons.submit" color="primary" @click="handleSubmit('submit')"/> -->
122
121
 
@@ -149,7 +148,11 @@
149
148
  const formRef = ref(null);
150
149
  const isSubmitting = ref(false);
151
150
  const draftSubmitted = ref(false);
151
+ const relationId = ref(null);
152
152
  const clickedButton = ref('');
153
+ const isPreFilled = ref(false)
154
+ const originalFormData = ref({})
155
+
153
156
  const emit = defineEmits([
154
157
  'update:modelValue',
155
158
  'submit:success',
@@ -185,16 +188,21 @@
185
188
  const compiledFunctions = ref({});
186
189
  // const showUploads = ref(false)
187
190
 
191
+ const formMode = ref(props.mode)
192
+
188
193
  // Enhanced evaluation context that includes props and mode
189
194
  const createEvaluationContext = () => {
190
195
  return {
196
+ formData,
197
+ formVariables,
198
+ variables: formVariables,
191
199
  ...formData,
192
200
  ...formVariables,
193
- mode: props.mode,
201
+ mode: formMode.value,
194
202
  formType: formType.value,
195
203
  props: props,
196
- isEdit: props.mode === 'edit',
197
- isCreate: props.mode === 'create'
204
+ isEdit: formMode.value === 'edit',
205
+ isCreate: formMode.value === 'create',
198
206
  };
199
207
  };
200
208
 
@@ -242,6 +250,23 @@
242
250
  }
243
251
  };
244
252
 
253
+ const isFormDirty = computed(() => {
254
+ if (!isPreFilled.value) return false
255
+ return Object.keys(originalFormData.value).some(key => {
256
+ const original = originalFormData.value[key]
257
+ const current = formData[key]
258
+ // Handle null/undefined equality
259
+ if (original === null || original === undefined) {
260
+ return current !== null && current !== undefined && current !== ''
261
+ }
262
+ // Handle objects/arrays
263
+ if (typeof original === 'object') {
264
+ return JSON.stringify(original) !== JSON.stringify(current)
265
+ }
266
+ return original !== current
267
+ })
268
+ })
269
+
245
270
  // Initialize form data with default values or items data
246
271
  const initializeFormData = () => {
247
272
  // Add safety check for form.fields
@@ -252,8 +277,18 @@
252
277
  props.form.fields.forEach(field => {
253
278
  if (!field || !field.key) return;
254
279
 
255
- if (props.items && props.items[field.key] !== undefined) {
256
- formData[field.key] = props.items[field.key];
280
+ const fieldKey = field.dataKey ?? field.key
281
+
282
+ // 1. Try to get value from props.items (supports dot notation)
283
+ if (props.items) {
284
+ const val = fieldKey.includes('.')
285
+ ? getNestedValue(props.items, fieldKey)
286
+ : props.items[fieldKey]
287
+
288
+ if (val !== undefined) {
289
+ formData[field.key] = val
290
+ return
291
+ }
257
292
  } else if (field.default !== undefined) {
258
293
  formData[field.key] = field.default;
259
294
  } else if (field.value !== undefined) {
@@ -423,6 +458,14 @@
423
458
  return true; // Show group by default if evaluation fails
424
459
  }
425
460
  };
461
+ // Helper to get nested value by dot notation key e.g. "submissionDetails.form_ref_number"
462
+ const getNestedValue = (obj, path) => {
463
+ if (!path || !obj) return undefined
464
+ return path.split('.').reduce((acc, key) => {
465
+ if (acc === null || acc === undefined) return undefined
466
+ return acc[key]
467
+ }, obj)
468
+ }
426
469
 
427
470
  // Function to get ungrouped fields
428
471
  const getUngroupedFields = () => {
@@ -633,7 +676,7 @@
633
676
 
634
677
  // Add relationId for file upload components
635
678
  if (field.inputType === 'file' || field.inputType === 'filefield') {
636
- cleanProps.relationId = formId.value || props.editItemId;
679
+ cleanProps.relationId = relationId.value;
637
680
  // cleanProps.onuploadSuccess = fetchData();
638
681
  // cleanProps.onviewPdf = emit('view-pdf');
639
682
  // cleanProps.fileList = FormDataList.value?.[ field.dataKey ?? field.key ];
@@ -802,55 +845,55 @@
802
845
  try {
803
846
  // Filter out non-database fields before submission
804
847
  const submitData = getDbFieldsOnly(formData);
805
- if (clickedButton.value === 'draft') {
848
+ const currentId = formId.value || props.editItemId
849
+ if (clickedButton.value === 'draft' || clickedButton.value === 'update') {
806
850
  submitData.status = 'draft';
807
851
  }else {
808
852
  submitData.status = 'submitted';
809
853
  }
810
854
  if (formId.value) {
811
- submitData.id = formId.value; // Ensure ID is included for updates
855
+ // submitData.id = formId.value; // Ensure ID is included for updates
812
856
  }
813
857
  // const isFullUrl = /^https?:\/\//i.test(props.form?.crudLink)
814
858
  // let url = isFullUrl ? props.form?.crudLink : baseUrl + '/' + props.form?.crudLink
815
859
  let completeUrl = ''
816
-
817
- if ((props.mode === 'edit' && props.editItemId) || clickedButton.value === 'submitted') {
818
- const editLink = props.form?.updateLink || props.form?.crudLink;
819
- // const isFullUrl = /^https?:\/\//i.test(editLink)
820
- // let url = isFullUrl ? editLink : baseUrl + '/' + editLink
821
- let url = editLink
822
- url += `/${submitData.id}`;
823
- completeUrl = api.put(url, submitData); // Use filtered data
824
- } else {
825
-
826
- const createLink = props.form?.createLink || props.form?.crudLink;
827
- // const isFullUrl = /^https?:\/\//i.test(createLink)
828
- // let url = isFullUrl ? createLink : baseUrl + '/' + createLink
829
- let url = createLink
830
- completeUrl = api.post(url, submitData); // Use filtered data
831
- }
832
- const response = await completeUrl;
860
+ let response = null;
861
+
862
+ if (clickedButton.value=== 'draft' && currentId) {
863
+ const createLink = props.form?.createLink || props.form?.crudLink
864
+ response = await api.post(createLink, submitData)
865
+
866
+ // Transition to edit mode
867
+ formId.value = (response?.data?.uuid || response?.uuid) ?? (response?.data?.id || response?.id)
868
+ formData.id = response?.data?.id || response?.id
869
+ relationId.value = response?.data?.id || response?.id
870
+ formMode.value = 'edit' // trigger button swap
871
+
872
+ await fetchData() // load saved data back
873
+
874
+ // snackbar?.show({ message: response?.data?.message || 'Draft saved', variant: 'success' })
875
+ // Do NOT close form
876
+ emit('submit:success', response?.message)
877
+ } else{
878
+ submitData.status = 'draft'
879
+ //submitData.id = currentId
880
+ const updateLink = props.form?.updateLink || props.form?.crudLink
881
+ response = await api.put(`${updateLink}/${currentId}`, submitData)
882
+
883
+ emit('submit:success', response?.message)
884
+ if (buttonType === 'submit') {
885
+ handleClose()
886
+ formRef.value?.reset()
887
+ }else {
888
+ await fetchData()
889
+ }
833
890
 
834
- const successMessage = response.data?.message || response.message || 'Created successfully';
835
- snackbar?.show({ message: successMessage, variant: 'success' });
836
-
837
- formId.value = response?.data?.id || formData.id || props.editItemId || null; // Update formId with response ID if available
838
- // submitData.id = formId.value; // Ensure submitData has the correct ID for any subsequent operations
839
- console.log('Form submission response:', response);
840
-
841
- if (clickedButton.value === 'draft') {
842
- draftSubmitted.value = true;
843
- }
844
- if (clickedButton.value !== 'draft') {
845
- emit('submit:success', response.message);
846
- handleClose();
847
- formRef.value?.reset();
848
891
  }
849
892
 
850
893
  } catch (error) {
851
894
  console.error('Form submission error:', error);
852
895
  const errorMessage = error.response?.data?.message || 'Form submission failed';
853
- snackbar?.show({ message: errorMessage, variant: 'error' });
896
+ // snackbar?.show({ message: errorMessage, variant: 'error' });
854
897
  if (error.response?.status === 422 || error.response?.data?.errors) {
855
898
  const backendErrors = error.response.data.errors;
856
899
  snackbar?.show({ message: backendErrors, variant: 'error' });
@@ -921,14 +964,34 @@
921
964
 
922
965
  if (response) {
923
966
  const fetchedData = response
924
- FormDataList.value = response || response.data || {};
925
- formData.id = props.editItemId
967
+ FormDataList.value = response || response.data || {}
968
+ formData.id = response?.data?.id || response?.id
969
+ relationId.value = response?.data?.id || response?.id
926
970
 
927
971
  props.form.fields.forEach(field => {
928
- if (fetchedData[field.key] !== undefined) {
929
- formData[field.key] = fetchedData[field.key]
972
+ if (!field || !field.key) return
973
+
974
+ const fieldKey = field.key
975
+
976
+ if (fieldKey.includes('.')) {
977
+ const nestedVal = getNestedValue(fetchedData, fieldKey)
978
+ if (nestedVal !== undefined) {
979
+ formData[field.key] = nestedVal
980
+ return
981
+ }
982
+ }
983
+
984
+ if (fetchedData[fieldKey] !== undefined) {
985
+ formData[field.key] = fetchedData[fieldKey]
930
986
  }
931
987
  })
988
+ await nextTick()
989
+ if (formRef.value?.validate) {
990
+ const result = await formRef.value.validate()
991
+ valid.value = result?.valid ?? false
992
+ }
993
+ originalFormData.value = JSON.parse(JSON.stringify({ ...formData }))
994
+ isPreFilled.value = true
932
995
  }
933
996
  } catch (error) {
934
997
  snackbar?.show({ message: 'Error fetching data', variant: 'error' })
@@ -940,7 +1003,11 @@
940
1003
  open.value = false;
941
1004
  isSubmitting.value = false;
942
1005
  formRef.value?.reset();
1006
+ isPreFilled.value = false
1007
+ originalFormData.value = {}
943
1008
  valid.value = false;
1009
+ formMode.value = props.mode
1010
+ formId.value = null
944
1011
  emit('close', false);
945
1012
  };
946
1013
 
@@ -990,6 +1057,7 @@
990
1057
 
991
1058
  // Also watch for mode changes
992
1059
  watch(() => props.mode, (newMode) => {
1060
+ formMode.value = newMode
993
1061
  if (newMode === 'edit' && props.editItemId) {
994
1062
  fetchData()
995
1063
  } else if (newMode === 'create') {
@@ -1,4 +0,0 @@
1
- import { _ as f } from "./index-BYtCMgbN.js";
2
- export {
3
- f as default
4
- };