- added 'duplicate' functionality;
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect, useRef } from 'react';
|
||||||
import { __ } from '@wordpress/i18n';
|
import { __ } from '@wordpress/i18n';
|
||||||
import { useParams, useNavigate } from 'react-router-dom';
|
import { useParams, useNavigate } from 'react-router-dom';
|
||||||
import { is, isEmpty, isNil } from 'ramda';
|
import { isEmpty } from 'ramda';
|
||||||
import { classNames } from 'primereact/utils';
|
import { classNames } from 'primereact/utils';
|
||||||
|
import { klona } from 'klona';
|
||||||
|
|
||||||
// components
|
// components
|
||||||
import { Button } from 'primereact/button';
|
import { Button } from 'primereact/button';
|
||||||
@@ -15,6 +16,8 @@ import FormsService from '../../service/forms-service';
|
|||||||
import { storeSet } from '../../store';
|
import { storeSet } from '../../store';
|
||||||
import set404FromErrorResponse from '../../helpers/set404FromErrorResponse';
|
import set404FromErrorResponse from '../../helpers/set404FromErrorResponse';
|
||||||
import BandoService from '../../service/bando-service';
|
import BandoService from '../../service/bando-service';
|
||||||
|
import uniqid from '../../helpers/uniqid';
|
||||||
|
import { Toast } from 'primereact/toast';
|
||||||
|
|
||||||
const BandoForms = () => {
|
const BandoForms = () => {
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
@@ -22,8 +25,10 @@ const BandoForms = () => {
|
|||||||
const [templates, setTemplates] = useState(null);
|
const [templates, setTemplates] = useState(null);
|
||||||
const [selectedTemplate, setSelectedTemplate] = useState(null);
|
const [selectedTemplate, setSelectedTemplate] = useState(null);
|
||||||
const [selectedForm, setSelectedForm] = useState(null);
|
const [selectedForm, setSelectedForm] = useState(null);
|
||||||
|
const [selectedForDuplicateForm, setSelectedForDuplicateForm] = useState(null);
|
||||||
const [forms, setForms] = useState([]);
|
const [forms, setForms] = useState([]);
|
||||||
const [bandoStatus, setBandoStatus] = useState('');
|
const [bandoStatus, setBandoStatus] = useState('');
|
||||||
|
const toast = useRef(null);
|
||||||
|
|
||||||
const doCreateNewForm = () => {
|
const doCreateNewForm = () => {
|
||||||
navigate(`/bandi/${id}/forms/new`);
|
navigate(`/bandi/${id}/forms/new`);
|
||||||
@@ -43,10 +48,73 @@ const BandoForms = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const goToEditFormFromTemplate = () => {
|
const doDuplicateForm = () => {
|
||||||
console.log('goToEditFormFromTemplate', selectedTemplate)
|
const selectedFormArr = forms.filter(o => o.value === selectedForDuplicateForm);
|
||||||
//navigate(`/bandi/${id}`);
|
|
||||||
|
if (!isEmpty(selectedFormArr)) {
|
||||||
|
storeSet.main.setAsyncRequest();
|
||||||
|
FormsService.getFormById(selectedForDuplicateForm, getFormCallback, errGetFormCallbacks);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getFormCallback = (data) => {
|
||||||
|
if (data.status === 'SUCCESS') {
|
||||||
|
const newLabel = `${data.data.label} (copy)`;
|
||||||
|
|
||||||
|
const formData = {
|
||||||
|
label: newLabel,
|
||||||
|
content: data.data.content.map(o => ({
|
||||||
|
...o,
|
||||||
|
id: uniqid()
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
FormsService.createFormForCall(
|
||||||
|
id,
|
||||||
|
formData,
|
||||||
|
formCreateCallback,
|
||||||
|
errFormCreateCallback
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const errGetFormCallbacks = (data) => {
|
||||||
|
set404FromErrorResponse(data);
|
||||||
|
storeSet.main.unsetAsyncRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
const formCreateCallback = (data) => {
|
||||||
|
if (data.status === 'SUCCESS') {
|
||||||
|
storeSet.main.unsetAsyncRequest();
|
||||||
|
|
||||||
|
if (toast.current) {
|
||||||
|
toast.current.show({
|
||||||
|
severity: 'success',
|
||||||
|
summary: '',
|
||||||
|
detail: __('Il form è stato aggiornato corretamente!', 'gepafin')
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
navigate(`/bandi/${id}/forms/${data.data.id}`);
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const errFormCreateCallback = (data) => {
|
||||||
|
storeSet.main.unsetAsyncRequest();
|
||||||
|
if (toast.current) {
|
||||||
|
toast.current.show({
|
||||||
|
severity: 'error',
|
||||||
|
summary: '',
|
||||||
|
detail: data.message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*const goToEditFormFromTemplate = () => {
|
||||||
|
console.log('goToEditFormFromTemplate', selectedTemplate)
|
||||||
|
}*/
|
||||||
|
|
||||||
const getCallback = (data) => {
|
const getCallback = (data) => {
|
||||||
if (data.status === 'SUCCESS') {
|
if (data.status === 'SUCCESS') {
|
||||||
@@ -62,7 +130,7 @@ const BandoForms = () => {
|
|||||||
|
|
||||||
const getFormsCallback = (data) => {
|
const getFormsCallback = (data) => {
|
||||||
if (data.status === 'SUCCESS') {
|
if (data.status === 'SUCCESS') {
|
||||||
const forms = data.data.map(o => ({label: o.label, value: o.id}))
|
const forms = data.data.map(o => ({ label: o.label, value: o.id }))
|
||||||
setForms(forms);
|
setForms(forms);
|
||||||
}
|
}
|
||||||
storeSet.main.unsetAsyncRequest();
|
storeSet.main.unsetAsyncRequest();
|
||||||
@@ -78,7 +146,7 @@ const BandoForms = () => {
|
|||||||
const bandoId = !isNaN(parsed) ? parsed : 0;
|
const bandoId = !isNaN(parsed) ? parsed : 0;
|
||||||
|
|
||||||
setTemplates([
|
setTemplates([
|
||||||
{label: "Form template", value: 11}
|
{ label: 'Form template', value: 11 }
|
||||||
])
|
])
|
||||||
|
|
||||||
storeSet.main.setAsyncRequest();
|
storeSet.main.setAsyncRequest();
|
||||||
@@ -96,6 +164,7 @@ const BandoForms = () => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="appPage__spacer"></div>
|
<div className="appPage__spacer"></div>
|
||||||
|
<Toast ref={toast} />
|
||||||
|
|
||||||
<div className="appPage__content">
|
<div className="appPage__content">
|
||||||
|
|
||||||
@@ -124,16 +193,32 @@ const BandoForms = () => {
|
|||||||
<div className="appPageSection__withBorder">
|
<div className="appPageSection__withBorder">
|
||||||
<h2>{__('Crea un nuovo Form da Zero', 'gepafin')}</h2>
|
<h2>{__('Crea un nuovo Form da Zero', 'gepafin')}</h2>
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<p>{__('Inizia con un form completamente vuoto e personalizzabil', 'gepafin')}</p>
|
<p>{__('Inizia con un form completamente vuoto e personalizzabile', 'gepafin')}</p>
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
disabled={'PUBLISH' === bandoStatus}
|
disabled={'PUBLISH' === bandoStatus}
|
||||||
onClick={doCreateNewForm}
|
onClick={doCreateNewForm}
|
||||||
label={__('Crea form', 'gepafin')}/>
|
label={__('Crea form', 'gepafin')}/>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="row">
|
||||||
|
<p>{__('Duplica il form creato in precedenza', 'gepafin')}</p>
|
||||||
|
<Dropdown
|
||||||
|
id="form"
|
||||||
|
disabled={isEmpty(forms)}
|
||||||
|
value={selectedForDuplicateForm}
|
||||||
|
onChange={(e) => setSelectedForDuplicateForm(e.value)}
|
||||||
|
options={forms}
|
||||||
|
optionLabel="label"
|
||||||
|
placeholder={__('Seleziona form', 'gepafin')}/>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
disabled={'PUBLISH' === bandoStatus || isEmpty(forms)}
|
||||||
|
onClick={doDuplicateForm}
|
||||||
|
label={__('Duplicare', 'gepafin')}/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className={classNames(["appPageSection__withBorder", (isEmpty(forms) ? 'disabled' : '')])}>
|
<div className={classNames(['appPageSection__withBorder', (isEmpty(forms) ? 'disabled' : '')])}>
|
||||||
<h2>{__('Modifica form esistente', 'gepafin')}</h2>
|
<h2>{__('Modifica form esistente', 'gepafin')}</h2>
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<p>{__('Continua a lavorare su un form precedentemente salvato', 'gepafin')}</p>
|
<p>{__('Continua a lavorare su un form precedentemente salvato', 'gepafin')}</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user