diff --git a/src/components/UnsavedChangesDetector/index.js b/src/components/UnsavedChangesDetector/index.js
index 71c967e..642c619 100644
--- a/src/components/UnsavedChangesDetector/index.js
+++ b/src/components/UnsavedChangesDetector/index.js
@@ -1,18 +1,63 @@
import { useEffect } from 'react';
import { __ } from '@wordpress/i18n';
import equal from 'fast-deep-equal';
-//import { diff } from 'deep-object-diff';
+import { diff } from 'deep-object-diff';
+import { klona } from 'klona';
+import { TZDate } from '@date-fns/tz';
+import { wrap } from 'object-path-immutable';
+import { is, isNil } from 'ramda';
// store
import { storeGet } from '../../store';
const UnsavedChangesDetector = ({ getValuesFn }) => {
const warnIfUnsavedChanges = (event) => {
- const formData = getValuesFn();
+ let formData = klona(getValuesFn());
+ formData.dates = [];
+
+ if (formData.startDate) {
+ let starDate;
+
+ if (is(String, formData.startDate)) {
+ starDate = formData.startDate;
+ } else {
+ const tzAwareDate = new TZDate(formData.startDate, 'Europe/Berlin');
+ starDate = tzAwareDate.toISOString().substring(0, 19);
+ }
+
+ formData = wrap(formData).insert(['dates'], starDate, 0).value();
+ }
+ if (formData.endDate) {
+ let endDate;
+
+ if (is(String, formData.endDate)) {
+ endDate = formData.endDate;
+ } else {
+ const tzAwareDate = new TZDate(formData.endDate, 'Europe/Berlin');
+ endDate = tzAwareDate.toISOString().substring(0, 19);
+ }
+
+ formData = wrap(formData).insert(['dates'], endDate, 1).value();
+ }
+ if (!isNil(formData.startTime)) {
+ if (!is(String, formData.startTime)) {
+ const tzAwareDate = new TZDate(formData.startTime, 'Europe/Berlin');
+ formData.startTime = tzAwareDate.toISOString().substring(11, 16);
+ }
+ }
+ if (!isNil(formData.endTime)) {
+ if (!is(String, formData.endTime)) {
+ const tzAwareDate = new TZDate(formData.endTime, 'Europe/Berlin');
+ formData.endTime = tzAwareDate.toISOString().substring(11, 16);
+ }
+ }
const initial = storeGet.main.formInitialData();
+
const isEqual = equal(initial, formData);
// TODO
- //console.log('isEqual', isEqual, initial, formData, diff(initial, formData))
+ /*console.log('isEqual', isEqual,
+ initial, formData,
+ diff(initial, formData))*/
if (!isEqual) {
event.returnValue = __('You have unsaved changes. If you proceed, they will be lost.', 'gepafin');
}
diff --git a/src/pages/BandoApplication/ApplicationSteps/index.js b/src/pages/BandoApplication/ApplicationSteps/index.js
index 46df2c5..82c0f1d 100644
--- a/src/pages/BandoApplication/ApplicationSteps/index.js
+++ b/src/pages/BandoApplication/ApplicationSteps/index.js
@@ -1,20 +1,13 @@
import React from 'react';
import { range } from 'ramda';
-//import { __ } from '@wordpress/i18n';
+import { __ } from '@wordpress/i18n';
// components
import { Steps } from 'primereact/steps';
const ApplicationSteps = ({ totalSteps = 0, activeStepIndex }) => {
const rangeArr = range(1, totalSteps + 1);
- const items = rangeArr.map(() => ({ label: 'Passo' }));
-
- /*// TODO update to using Steps after primereact is updated
- return(
- 0 !== totalSteps
- ? {__('Passo', 'gepafin')}: {activeStepIndex + 1}
- : null
- )*/
+ const items = rangeArr.map(() => ({ label: __('Passo', 'gepafin') }));
return(
0 !== totalSteps
diff --git a/src/pages/BandoEdit/components/BandoEditFormStep1/index.js b/src/pages/BandoEdit/components/BandoEditFormStep1/index.js
index 9cbb1d5..4117123 100644
--- a/src/pages/BandoEdit/components/BandoEditFormStep1/index.js
+++ b/src/pages/BandoEdit/components/BandoEditFormStep1/index.js
@@ -5,6 +5,7 @@ import { useForm } from 'react-hook-form';
import { isEmpty, isNil, is } from 'ramda';
import { klona } from 'klona';
import { TZDate } from '@date-fns/tz';
+import { wrap } from 'object-path-immutable';
// components
import FormField from '../../../../components/FormField';
@@ -48,38 +49,36 @@ const BandoEditFormStep1 = forwardRef(function ({ initialData, getFormErrors, st
const values = getValues();
const toast = useRef(null);
- const onSubmit = (formData) => {
- /*if (!isNil(formData.dates) && formData.dates.length) {
- formData.dates = formData.dates.map(v => {
- if (is(String, v)) {
- return v;
- } else {
- const tzAwareDate = new TZDate(v, 'Europe/Berlin');
- return tzAwareDate.toISOString().substring(0, 19);
- }
- });
- }
-
- storeSet.main.setAsyncRequest();
- if (!formData.id) {
- BandoService.createBando(formData, createCallback, errCreateCallback);
- } else {
- BandoService.updateBandoStep1(formData.id, formData, createCallback, errCreateCallback);
- }*/
- };
+ const onSubmit = () => {};
const onSaveDraft = () => {
trigger();
- const formData = getValues();
- if (!isNil(formData.dates) && formData.dates.length) {
- formData.dates = formData.dates.map(v => {
- if (is(String, v)) {
- return v;
- } else {
- const tzAwareDate = new TZDate(v, 'Europe/Berlin');
- return tzAwareDate.toISOString().substring(0, 19);
- }
- });
+ let formData = klona(getValues());
+ formData.dates = [];
+
+ if (formData.startDate) {
+ let starDate;
+
+ if (is(String, formData.startDate)) {
+ starDate = formData.startDate;
+ } else {
+ const tzAwareDate = new TZDate(formData.startDate, 'Europe/Berlin');
+ starDate = tzAwareDate.toISOString().substring(0, 19);
+ }
+
+ formData = wrap(formData).insert(['dates'], starDate, 0).value();
+ }
+ if (formData.endDate) {
+ let endDate;
+
+ if (is(String, formData.endDate)) {
+ endDate = formData.endDate;
+ } else {
+ const tzAwareDate = new TZDate(formData.endDate, 'Europe/Berlin');
+ endDate = tzAwareDate.toISOString().substring(0, 19);
+ }
+
+ formData = wrap(formData).insert(['dates'], endDate, 1).value();
}
if (!isNil(formData.startTime)) {
if (!is(String, formData.startTime)) {
@@ -95,6 +94,9 @@ const BandoEditFormStep1 = forwardRef(function ({ initialData, getFormErrors, st
}
}
+ delete formData.startDate;
+ delete formData.endDate;
+
storeSet.main.setAsyncRequest();
if (!formData.id) {
BandoService.createBando(formData, createCallback, errCreateCallback);
@@ -106,11 +108,13 @@ const BandoEditFormStep1 = forwardRef(function ({ initialData, getFormErrors, st
const createCallback = (data) => {
storeSet.main.unsetAsyncRequest();
if (data.status === 'SUCCESS') {
- toast.current.show({
- severity: 'success',
- summary: '',
- detail: __('Il bando è stato aggiornato corretamente!', 'gepafin')
- });
+ if (toast.current) {
+ toast.current.show({
+ severity: 'success',
+ summary: '',
+ detail: __('Il bando è stato aggiornato corretamente!', 'gepafin')
+ });
+ }
const values = getValues();
if (!values.id && data.data.id) {
navigate(`/bandi/${data.data.id}`);
@@ -121,6 +125,13 @@ const BandoEditFormStep1 = forwardRef(function ({ initialData, getFormErrors, st
}
const errCreateCallback = (data) => {
+ if (toast.current && data.message) {
+ toast.current.show({
+ severity: 'error',
+ summary: '',
+ detail: data.message
+ });
+ }
set404FromErrorResponse(data);
storeSet.main.unsetAsyncRequest();
}
@@ -193,7 +204,12 @@ const BandoEditFormStep1 = forwardRef(function ({ initialData, getFormErrors, st
useEffect(() => {
const newFormData = klona(formInitialData);
if (!isNil(formInitialData.dates) && formInitialData.dates.length) {
- newFormData.dates = formInitialData.dates.map(v => is(String, v) ? new Date(v) : (v ? v : ''));
+ if (newFormData.dates[0]) {
+ newFormData.startDate = is(String, newFormData.dates[0]) ? new Date(newFormData.dates[0]) : (newFormData.dates[0] ? newFormData.dates[0] : '');
+ }
+ if (newFormData.dates[1]) {
+ newFormData.endDate = is(String, newFormData.dates[1]) ? new Date(newFormData.dates[1]) : (newFormData.dates[1] ? newFormData.dates[1] : '');
+ }
}
if (!isNil(formInitialData.startTime) && !isEmpty(formInitialData.startTime)) {
newFormData.startTime = is(String, formInitialData.startTime)
@@ -305,7 +321,7 @@ const BandoEditFormStep1 = forwardRef(function ({ initialData, getFormErrors, st
}}
/>
-