diff --git a/src/components/form/AddForm.js b/src/components/form/AddForm.js index 0acb7a0ca21611e58a07864dcdc68c03f43b216d..23961101b1c436a6781f38cebe647909fd580f7c 100644 --- a/src/components/form/AddForm.js +++ b/src/components/form/AddForm.js @@ -16,6 +16,7 @@ import Header from "./../common/Header"; import Sortable from "sortablejs"; import { BtnTwo } from "../buttons/BtnTwo"; import { BtnOne } from "../buttons/BtnOne"; +import {ConfirmModal } from "../../components/modal"; let strings = new LocalizedStrings(translations); @@ -27,6 +28,7 @@ class AddForm extends Component { this.resetElements = this.resetElements.bind(this); this.handleChange = this.handleChange.bind(this); this.submit = this.submit.bind(this); + this.saveFormDetails = this.saveFormDetails.bind(this); this.state = { language: "en", formElements: [], @@ -39,6 +41,7 @@ class AddForm extends Component { fields: [], }, breadcrumbData: [], + showDuplicateConfirmModal: false, }; } @@ -276,30 +279,46 @@ class AddForm extends Component { } if (allowSubmission) { - FormService.add(formData).then( - (response) => { - if (response.statusInfo.statusCode === APP.CODE.SUCCESS) { - Notify.success(response.statusInfo.statusMessage); - // this.props.updateParent(response.responseData.id); - setTimeout(() => { - this.props.history.push("/manage?tab=1"); - }, 500); - } else { - Notify.error(response.statusInfo.errorMessage); - } - }, - (error) => { - error.statusInfo - ? Notify.error(error.statusInfo.errorMessage) - : Notify.error(error.message); - } - ); + this.saveFormDetails(formData,false); } else { Notify.error("Kindly fill all the fields"); } }; + saveFormDetails = (formData, isDuplicate) => { + FormService.add(formData).then( + (response) => { + if (response.statusInfo.statusCode === APP.CODE.SUCCESS) { + if(isDuplicate) { + Notify.success("Duplicate form is added successfully"); + this.setState({showDuplicateConfirmModal: false}); + }else { + Notify.success(response.statusInfo.statusMessage); + } + + // this.props.updateParent(response.responseData.id); + setTimeout(() => { + this.props.history.push("/manage?tab=1"); + }, 500); + } else { + Notify.error(response.statusInfo.errorMessage); + } + }, + (error) => { + error.statusInfo + ? Notify.error(error.statusInfo.errorMessage) + : Notify.error(error.message); + } + ); + } + + duplicateForm= () => { + let formData = {...this.state.formDetails, id: "", title: 'Copy of '+ this.state.formDetails.title}; + this.saveFormDetails(formData, true); + } + render() { + const { formDetails, showDuplicateConfirmModal } = this.state; strings.setLanguage( localStorage.getItem("language") || this.state.language ); @@ -339,6 +358,18 @@ class AddForm extends Component { /> </div> )} + {this.state.formDetails.status === + LANG.FORM_STATUS.DRAFT && ( + <div className="mr-3"> + <BtnOne + label="Duplicate" + btnType="button" + isLink={false} + link="" + clickHandler={(e) => this.setState({ showDuplicateConfirmModal: true})} + /> + </div> + )} <div className="mr-0"> <BtnTwo label="Submit" @@ -514,6 +545,15 @@ class AddForm extends Component { </div> </div> </div> + {showDuplicateConfirmModal && ( + <ConfirmModal + title="Duplicate Form" + onConfirm={this.duplicateForm} + onCancel={() => { + this.setState({showDuplicateConfirmModal: false}); + }} + > Do you want to delete {formDetails?.title} ?</ConfirmModal> + )} </Fragment> ); } diff --git a/src/components/form/ListForms.js b/src/components/form/ListForms.js index 4cbd06aee888dfb04c7e4516acc16704feae24b6..900e7382dec54396bb1b5eb61f84c384e966490d 100644 --- a/src/components/form/ListForms.js +++ b/src/components/form/ListForms.js @@ -4,12 +4,17 @@ import { FormService } from "../../services/form.service"; import { APP, LANG } from "../../constants"; import Notify from "../../helpers/notify"; import { BtnTwo } from "../buttons"; +import {ConfirmModal } from "../../components/modal"; +import Helper from "../../helpers/auth"; class ListForms extends Component { constructor(props) { super(props); this.state = { forms: [], + isAdmin: Helper.isAdmin(), + formToDelete: {}, + showConfirmModal: false, }; this.getFormShortCode = this.getFormShortCode.bind(this); } @@ -62,24 +67,7 @@ class ListForms extends Component { } else { formData.status = LANG.FORM_STATUS.UNPUBLISH; } - FormService.add(formData).then( - (response) => { - if (response.statusInfo.statusCode === APP.CODE.SUCCESS) { - Notify.success(response.statusInfo.statusMessage); - // this.props.updateParent(response.responseData.id); - setTimeout(() => { - this.getAllForms(); - }, 500); - } else { - Notify.error(response.statusInfo.errorMessage); - } - }, - (error) => { - error.statusInfo - ? Notify.error(error.statusInfo.errorMessage) - : Notify.error(error.message); - } - ); + this.saveFormDetails(formData, false); }; searchForms = (event) => { @@ -105,7 +93,40 @@ class ListForms extends Component { // history: PropTypes.object.isRequired, // }; + deleteForm = () => { + const formData = {...this.state.formToDelete, status: LANG.FORM_STATUS.DELETED} + this.saveFormDetails(formData, true); + } + + + saveFormDetails = (formData, isDelete) => { + FormService.add(formData).then( + (response) => { + if (response.statusInfo.statusCode === APP.CODE.SUCCESS) { + if(isDelete) { + this.setState({showConfirmModal: false}); + Notify.success("Form deleted successfully") + }else { + Notify.success(response.statusInfo.statusMessage); + } + // this.props.updateParent(response.responseData.id); + setTimeout(() => { + this.getAllForms(); + }, 500); + } else { + Notify.error(response.statusInfo.errorMessage); + } + }, + (error) => { + error.statusInfo + ? Notify.error(error.statusInfo.errorMessage) + : Notify.error(error.message); + } + ); + } + render() { + const { isAdmin, showConfirmModal, formToDelete } = this.state; return ( <Fragment> <div className="row pt-2"> @@ -194,7 +215,18 @@ class ListForms extends Component { </td> <td className="td-preview"> {form.status === LANG.FORM_STATUS.DRAFT && ( + <> <Link to={`/forms/${form.id}/edit`}>Edit</Link> + {isAdmin && ( + <span + className="ml-3 text-danger pointer" + onClick={ + () => { + this.setState({formToDelete: form, showConfirmModal:true}) + } + }>Delete</span> + )} + </> )} {form.status !== LANG.FORM_STATUS.DRAFT && ( <span className="font-weight-bold black-16">Edit</span> @@ -206,6 +238,15 @@ class ListForms extends Component { </table> </div> </div> + {showConfirmModal && ( + <ConfirmModal + title="Delete Form" + onConfirm={this.deleteForm} + onCancel={() => { + this.setState({showConfirmModal: false, formToDelete: {}}); + }} + > Do you want to delete {formToDelete?.title} ?</ConfirmModal> + )} </Fragment> ); } diff --git a/src/constants/LangConstants.ts b/src/constants/LangConstants.ts index bec48d016568fd932876775da5373ef1bb54667a..56a17a863077f945d78462d9a0f58ddc996bf16f 100644 --- a/src/constants/LangConstants.ts +++ b/src/constants/LangConstants.ts @@ -28,7 +28,8 @@ export const LANG = { APPROVED: "APPROVED", REJECTED: "REJECTED", LEAD_INSPECTION_COMPLETED: "LEADINSCOMPLETED", - ASSISTING_INSPECTION_COMPLETED: "INSCOMPLETED" + ASSISTING_INSPECTION_COMPLETED: "INSCOMPLETED", + DELETED: "DELETED" }, COL: { 1: 1, diff --git a/src/helpers/auth.js b/src/helpers/auth.js index 8eda5c8ed8589216720949da33edfaf1d9b2a38e..1c1a028263c0dfc1afe27ba83061e468335504c3 100644 --- a/src/helpers/auth.js +++ b/src/helpers/auth.js @@ -31,6 +31,15 @@ const Auth = { } else { return false; } + }, + + isAdmin() { + const userRole = this.getUserRole(); + if(userRole === APP.ROLE.SUPER_ADMIN || userRole === APP.ROLE.REGULATOR) { + return true; + } else { + return false; + } } };