- 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 { useParams, useNavigate } from 'react-router-dom';
|
||||
import { is, isEmpty, isNil } from 'ramda';
|
||||
import { isEmpty } from 'ramda';
|
||||
import { classNames } from 'primereact/utils';
|
||||
import { klona } from 'klona';
|
||||
|
||||
// components
|
||||
import { Button } from 'primereact/button';
|
||||
@@ -15,6 +16,8 @@ import FormsService from '../../service/forms-service';
|
||||
import { storeSet } from '../../store';
|
||||
import set404FromErrorResponse from '../../helpers/set404FromErrorResponse';
|
||||
import BandoService from '../../service/bando-service';
|
||||
import uniqid from '../../helpers/uniqid';
|
||||
import { Toast } from 'primereact/toast';
|
||||
|
||||
const BandoForms = () => {
|
||||
const { id } = useParams();
|
||||
@@ -22,8 +25,10 @@ const BandoForms = () => {
|
||||
const [templates, setTemplates] = useState(null);
|
||||
const [selectedTemplate, setSelectedTemplate] = useState(null);
|
||||
const [selectedForm, setSelectedForm] = useState(null);
|
||||
const [selectedForDuplicateForm, setSelectedForDuplicateForm] = useState(null);
|
||||
const [forms, setForms] = useState([]);
|
||||
const [bandoStatus, setBandoStatus] = useState('');
|
||||
const toast = useRef(null);
|
||||
|
||||
const doCreateNewForm = () => {
|
||||
navigate(`/bandi/${id}/forms/new`);
|
||||
@@ -43,11 +48,74 @@ const BandoForms = () => {
|
||||
}
|
||||
}
|
||||
|
||||
const goToEditFormFromTemplate = () => {
|
||||
console.log('goToEditFormFromTemplate', selectedTemplate)
|
||||
//navigate(`/bandi/${id}`);
|
||||
const doDuplicateForm = () => {
|
||||
const selectedFormArr = forms.filter(o => o.value === selectedForDuplicateForm);
|
||||
|
||||
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) => {
|
||||
if (data.status === 'SUCCESS') {
|
||||
setBandoStatus(data.data.status);
|
||||
@@ -62,7 +130,7 @@ const BandoForms = () => {
|
||||
|
||||
const getFormsCallback = (data) => {
|
||||
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);
|
||||
}
|
||||
storeSet.main.unsetAsyncRequest();
|
||||
@@ -78,7 +146,7 @@ const BandoForms = () => {
|
||||
const bandoId = !isNaN(parsed) ? parsed : 0;
|
||||
|
||||
setTemplates([
|
||||
{label: "Form template", value: 11}
|
||||
{ label: 'Form template', value: 11 }
|
||||
])
|
||||
|
||||
storeSet.main.setAsyncRequest();
|
||||
@@ -96,6 +164,7 @@ const BandoForms = () => {
|
||||
</div>
|
||||
|
||||
<div className="appPage__spacer"></div>
|
||||
<Toast ref={toast} />
|
||||
|
||||
<div className="appPage__content">
|
||||
|
||||
@@ -124,16 +193,32 @@ const BandoForms = () => {
|
||||
<div className="appPageSection__withBorder">
|
||||
<h2>{__('Crea un nuovo Form da Zero', 'gepafin')}</h2>
|
||||
<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
|
||||
type="button"
|
||||
disabled={'PUBLISH' === bandoStatus}
|
||||
onClick={doCreateNewForm}
|
||||
label={__('Crea form', 'gepafin')}/>
|
||||
</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 className={classNames(["appPageSection__withBorder", (isEmpty(forms) ? 'disabled' : '')])}>
|
||||
<div className={classNames(['appPageSection__withBorder', (isEmpty(forms) ? 'disabled' : '')])}>
|
||||
<h2>{__('Modifica form esistente', 'gepafin')}</h2>
|
||||
<div className="row">
|
||||
<p>{__('Continua a lavorare su un form precedentemente salvato', 'gepafin')}</p>
|
||||
|
||||
Reference in New Issue
Block a user