|
| 1 | +import { useState, useEffect, useMemo } from 'react'; |
| 2 | +import { Form, FormGroup, Label, Input, Button } from 'reactstrap'; |
| 3 | +import { toast } from 'react-toastify'; |
| 4 | +import { useDispatch, useSelector } from 'react-redux'; |
| 5 | + |
| 6 | +import { fetchAllEquipments } from '../../../actions/bmdashboard/equipmentActions'; |
| 7 | +import { fetchTools } from '../../../actions/bmdashboard/toolActions'; |
| 8 | +import { fetchBMProjects } from '../../../actions/bmdashboard/projectActions'; |
| 9 | + |
| 10 | +const initialFormState = { |
| 11 | + project: '', |
| 12 | + toolOrEquipment: '', |
| 13 | + name: '', |
| 14 | + number: '', |
| 15 | +}; |
| 16 | + |
| 17 | +export default function EquipmentUpdateForm() { |
| 18 | + const [formData, setFormData] = useState(initialFormState); |
| 19 | + const [isFormValid, setIsFormValid] = useState(false); |
| 20 | + const dispatch = useDispatch(); |
| 21 | + |
| 22 | + // Fetch dropdown data |
| 23 | + const projects = useSelector(state => state.bmProjects || []); |
| 24 | + const tools = useSelector(state => state.bmTools.toolslist || []); |
| 25 | + const equipments = useSelector(state => state.bmEquipments.equipmentslist || []); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + dispatch(fetchBMProjects()); |
| 29 | + dispatch(fetchTools()); |
| 30 | + dispatch(fetchAllEquipments()); |
| 31 | + }, [dispatch]); |
| 32 | + |
| 33 | + // Extract list of valid tools and equipment (only where name is present) |
| 34 | + const toolList = useMemo( |
| 35 | + () => |
| 36 | + tools |
| 37 | + .filter(tool => tool.itemType && tool.itemType.name) |
| 38 | + .map(tool => ({ id: tool.itemType._id, name: tool.itemType.name })), |
| 39 | + [tools], |
| 40 | + ); |
| 41 | + |
| 42 | + const equipmentList = useMemo( |
| 43 | + () => |
| 44 | + equipments |
| 45 | + .filter(equip => equip.itemType && equip.itemType.name) |
| 46 | + .map(equip => ({ id: equip.itemType._id, name: equip.itemType.name })), |
| 47 | + [equipments], |
| 48 | + ); |
| 49 | + |
| 50 | + // Update form validity |
| 51 | + useEffect(() => { |
| 52 | + const { project, toolOrEquipment, name, number } = formData; |
| 53 | + setIsFormValid(!!(project && toolOrEquipment && name && number)); |
| 54 | + }, [formData]); |
| 55 | + |
| 56 | + // Handle input change |
| 57 | + // Handle input change |
| 58 | + const handleChange = e => { |
| 59 | + const { name, value } = e.target; |
| 60 | + |
| 61 | + // Allow only numbers in the "number" field |
| 62 | + if (name === 'number' && value !== '' && !/^\d+$/.test(value)) { |
| 63 | + return; // Ignore invalid input (non-numeric values) |
| 64 | + } |
| 65 | + |
| 66 | + setFormData(prev => ({ |
| 67 | + ...prev, |
| 68 | + [name]: value, |
| 69 | + ...(name === 'toolOrEquipment' ? { name: '', number: '' } : {}), // Reset name and number on change |
| 70 | + })); |
| 71 | + }; |
| 72 | + |
| 73 | + // Reset form |
| 74 | + const handleCancel = () => { |
| 75 | + setFormData(initialFormState); |
| 76 | + }; |
| 77 | + |
| 78 | + // Submit form |
| 79 | + const handleSubmit = e => { |
| 80 | + e.preventDefault(); |
| 81 | + |
| 82 | + if (!isFormValid) { |
| 83 | + toast.error('Please complete all fields before updating.'); |
| 84 | + return; |
| 85 | + } |
| 86 | + setFormData(initialFormState); |
| 87 | + toast.success('Tool/Equipment updated successfully!'); |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div className="add-tool-form"> |
| 92 | + <Form onSubmit={handleSubmit}> |
| 93 | + <FormGroup> |
| 94 | + <Label for="project"> |
| 95 | + Project <span className="field-required">*</span> |
| 96 | + </Label> |
| 97 | + <Input |
| 98 | + type="select" |
| 99 | + name="project" |
| 100 | + id="project" |
| 101 | + value={formData.project} |
| 102 | + onChange={handleChange} |
| 103 | + > |
| 104 | + <option value="">Select Project</option> |
| 105 | + {projects.map(proj => ( |
| 106 | + <option key={proj.id} value={proj.id}> |
| 107 | + {proj.name} |
| 108 | + </option> |
| 109 | + ))} |
| 110 | + </Input> |
| 111 | + {!formData.project && <div className="toolFormError">Project is required</div>} |
| 112 | + </FormGroup> |
| 113 | + |
| 114 | + <FormGroup> |
| 115 | + <Label for="toolOrEquipment"> |
| 116 | + Tool or Equipment <span className="field-required">*</span> |
| 117 | + </Label> |
| 118 | + <Input |
| 119 | + type="select" |
| 120 | + name="toolOrEquipment" |
| 121 | + id="toolOrEquipment" |
| 122 | + value={formData.toolOrEquipment} |
| 123 | + onChange={handleChange} |
| 124 | + > |
| 125 | + <option value="">Select Tool/Equipment</option> |
| 126 | + <option value="Tool">Tool</option> |
| 127 | + <option value="Equipment">Equipment</option> |
| 128 | + </Input> |
| 129 | + {!formData.toolOrEquipment && <div className="toolFormError">This field is required</div>} |
| 130 | + </FormGroup> |
| 131 | + |
| 132 | + <FormGroup> |
| 133 | + <Label for="name"> |
| 134 | + Name <span className="field-required">*</span> |
| 135 | + </Label> |
| 136 | + <Input |
| 137 | + type="select" |
| 138 | + name="name" |
| 139 | + id="name" |
| 140 | + value={formData.name} |
| 141 | + onChange={handleChange} |
| 142 | + disabled={!formData.toolOrEquipment} |
| 143 | + > |
| 144 | + <option value="">Select Name</option> |
| 145 | + {formData.toolOrEquipment === 'Tool' && |
| 146 | + toolList.map(item => ( |
| 147 | + <option key={item.id} value={item.name}> |
| 148 | + {item.name} |
| 149 | + </option> |
| 150 | + ))} |
| 151 | + {formData.toolOrEquipment === 'Equipment' && |
| 152 | + equipmentList.map(item => ( |
| 153 | + <option key={item.id} value={item.name}> |
| 154 | + {item.name} |
| 155 | + </option> |
| 156 | + ))} |
| 157 | + </Input> |
| 158 | + {!formData.name && <div className="toolFormError">Please select a name</div>} |
| 159 | + </FormGroup> |
| 160 | + |
| 161 | + <FormGroup> |
| 162 | + <Label for="number"> |
| 163 | + Number <span className="field-required">*</span> |
| 164 | + </Label> |
| 165 | + <Input |
| 166 | + type="text" |
| 167 | + name="number" |
| 168 | + id="number" |
| 169 | + value={formData.number} |
| 170 | + onChange={handleChange} |
| 171 | + placeholder="Enter number" |
| 172 | + /> |
| 173 | + {!formData.number && <div className="toolFormError">Number is required</div>} |
| 174 | + </FormGroup> |
| 175 | + <div className="add-tool-buttons"> |
| 176 | + <Button color="primary" type="submit" disabled={!isFormValid}> |
| 177 | + Update |
| 178 | + </Button> |
| 179 | + <Button color="secondary" type="button" onClick={handleCancel}> |
| 180 | + Cancel |
| 181 | + </Button> |
| 182 | + </div> |
| 183 | + </Form> |
| 184 | + </div> |
| 185 | + ); |
| 186 | +} |
0 commit comments