|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import Modal from "react-bootstrap/Modal"; |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import { CloseIcon, CustomSearch, CustomButton } from "@formsflow/components"; |
| 5 | +import { Form } from "@aot-technologies/formio-react"; |
| 6 | +import { fetchAllForms, fetchFormById } from "../api/services/filterServices"; |
| 7 | +interface FormSelectionModalProps { |
| 8 | + showModal: boolean; |
| 9 | + onClose: () => void; |
| 10 | + onSelectForm: ({formId,formName}) => void; |
| 11 | +} |
| 12 | + |
| 13 | +export const FormSelectionModal: React.FC<FormSelectionModalProps> = React.memo( |
| 14 | + ({ showModal, onClose, onSelectForm }) => { |
| 15 | + |
| 16 | + const { t } = useTranslation(); |
| 17 | + const [searchFormName, setSearchFormName] = useState<string>(""); |
| 18 | + const [loadingForm, setLoadingForm] = useState<boolean>(false); |
| 19 | + const [selectedForm, setSelectedForm] = useState({ formId: "", formName: "" }); |
| 20 | + const [loading, setLoading] = useState(false); |
| 21 | + const [form, setForm] = useState<any>(null); |
| 22 | + const [formNames, setFormNames] = useState({ data: [], isLoading: true }); |
| 23 | + const [filteredFormNames, setFilteredFormNames] = useState<any[]>( |
| 24 | + formNames.data |
| 25 | + ); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + handleFormNameSearch(); |
| 29 | + }, [searchFormName]); |
| 30 | + useEffect(() => { |
| 31 | + fetchAllForms().then((res) => { |
| 32 | + const data = res.data?.forms ?? []; |
| 33 | + setFormNames({ |
| 34 | + data: data.map((i) => ({ formName: i.formName, formId: i.formId })), |
| 35 | + isLoading: false, |
| 36 | + }); |
| 37 | + }); |
| 38 | + }, []); |
| 39 | + useEffect (()=>{ |
| 40 | + if(formNames.data.length > 0) { |
| 41 | + setSelectedForm( ({ formId: formNames.data[0].formId, formName: formNames.data[0].formName })); |
| 42 | + } |
| 43 | + },[formNames.data]) |
| 44 | + const handleFormNameSearch = () => { |
| 45 | + setLoadingForm(true); |
| 46 | + if (searchFormName?.trim()) { |
| 47 | + setFilteredFormNames( |
| 48 | + formNames?.data.filter((i) => |
| 49 | + i.formName |
| 50 | + .toLowerCase() |
| 51 | + ?.includes(searchFormName?.trim()?.toLowerCase()) |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
| 55 | + setLoadingForm(false); |
| 56 | + }; |
| 57 | + const handleClearSearch = () => { |
| 58 | + setSearchFormName(""); |
| 59 | + setFilteredFormNames(formNames.data); |
| 60 | + }; |
| 61 | + const getFormOptions = () => { |
| 62 | + return searchFormName ? filteredFormNames : formNames.data; |
| 63 | + }; |
| 64 | + useEffect(() => { |
| 65 | + if (selectedForm.formId) { |
| 66 | + setLoading(true); |
| 67 | + // Fetch form data by ID |
| 68 | + fetchFormById(selectedForm.formId) |
| 69 | + .then((res) => { |
| 70 | + if (res.data) { |
| 71 | + const { data } = res; |
| 72 | + setForm(data); |
| 73 | + } |
| 74 | + }) |
| 75 | + .catch((err) => { |
| 76 | + console.error( |
| 77 | + "Error fetching form data:", |
| 78 | + err.response?.data ?? err.message |
| 79 | + ); |
| 80 | + }) |
| 81 | + .finally(() => { |
| 82 | + setLoading(false); |
| 83 | + }); |
| 84 | + } |
| 85 | + }, [selectedForm]); |
| 86 | + |
| 87 | + |
| 88 | + function renderFormList() { |
| 89 | + if (loadingForm || formNames.isLoading) { |
| 90 | + return <div className="form-selection-spinner"></div>; |
| 91 | + } |
| 92 | + |
| 93 | + const formOptions = getFormOptions(); |
| 94 | + if (formOptions.length > 0) { |
| 95 | + return formOptions.map((item) => ( |
| 96 | + <button |
| 97 | + className={`form-list-item button-as-div ${ |
| 98 | + selectedForm.formId === item.formId ? "active-form" : "" |
| 99 | + }`} |
| 100 | + onClick={() => setSelectedForm({ formId: item.formId, formName: item.formName })} |
| 101 | + key={item.formId} |
| 102 | + data-testid="form-selection-modal-form-item" |
| 103 | + > |
| 104 | + {item.formName} |
| 105 | + </button> |
| 106 | + )); |
| 107 | + } |
| 108 | + |
| 109 | + return <span className="nothing-found-text">{t("Nothing is found. Please try again.")}</span>; |
| 110 | + } |
| 111 | + |
| 112 | + return ( |
| 113 | + <Modal |
| 114 | + show={showModal} |
| 115 | + centered |
| 116 | + size="lg" |
| 117 | + className="form-selection-modal"> |
| 118 | + <Modal.Header className="form-selection-header"> |
| 119 | + <Modal.Title> {t("Select a Form")} </Modal.Title> |
| 120 | + <div className="d-flex align-items-center"> |
| 121 | + <CloseIcon |
| 122 | + onClick={onClose} |
| 123 | + data-testid="form-selection-modal-close-icon" |
| 124 | + /> |
| 125 | + </div> |
| 126 | + </Modal.Header> |
| 127 | + <Modal.Body className="form-selection-modal-body"> |
| 128 | + <div className="form-selection-left"> |
| 129 | + <div className="search-form"> |
| 130 | + <CustomSearch |
| 131 | + placeholder={t("Search ...")} |
| 132 | + search={searchFormName} |
| 133 | + setSearch={setSearchFormName} |
| 134 | + handleClearSearch={handleClearSearch} |
| 135 | + handleSearch={handleFormNameSearch} |
| 136 | + dataTestId="form-custom-search" |
| 137 | + /> |
| 138 | + </div> |
| 139 | + <div className="form-list"> |
| 140 | + {renderFormList()} |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + <div className="form-selection-right"> |
| 144 | + <div className="form-selection-preview custom-scroll"> |
| 145 | + {loading ? ( |
| 146 | + <div className="form-selection-spinner"></div> |
| 147 | + ) : ( |
| 148 | + <Form |
| 149 | + form={form} |
| 150 | + options={{ |
| 151 | + noAlerts: true, |
| 152 | + viewAsHtml: true, |
| 153 | + readOnly: true, |
| 154 | + showHiddenFields:true |
| 155 | + } as any} |
| 156 | + |
| 157 | + /> |
| 158 | + )} |
| 159 | + </div> |
| 160 | + <div className="form-select-btn"> |
| 161 | + <CustomButton |
| 162 | + onClick={() => { |
| 163 | + onSelectForm(selectedForm); |
| 164 | + }} |
| 165 | + variant="primary" |
| 166 | + label={t("Select This Form")} |
| 167 | + size="md" |
| 168 | + dataTestid="select-form-btn" |
| 169 | + /> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </Modal.Body> |
| 173 | + </Modal> |
| 174 | + ); |
| 175 | + } |
| 176 | +); |
0 commit comments