Skip to content

Commit abf2fe5

Browse files
committed
add tenant validation to match backend requirements
Special handling for blank values in: unit number, occupants. Backend validation requires `null` instead of empty string Custom form validation: property ID is required ONLY when lease dates are supplied - use Formik's "validate" hook to drive new state for the error messages on propertyID The form resets all fields after submission - `preSelectedChoices` was the trick to get previously selected "chips" to disappear from SearchPanel components indentation fixes
1 parent eac3022 commit abf2fe5

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

src/views/AddTenant/addTenant.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const validationSchema = Yup.object().shape({
2323
.min(7, "*Phone Number must have at least 7 characters")
2424
.max(20, "*Phone Number can't be longer than 20 characters")
2525
.required("*Phone Number is required"),
26-
unitNum: Yup.number(),
26+
unitNum: Yup.string(),
2727
occupants: Yup.number(),
2828
});
2929

@@ -39,12 +39,16 @@ export const AddTenant = () => {
3939
const [propertyOptions, setPropertyOptions] = useState([]);
4040
const [propertySearchResults, setPropertySearchResults] = useState([]);
4141
const [showAddProperty, setShowAddProperty] = useState(false);
42+
const [isValidationActive, setIsValidationActive] = useState(false);
43+
const [propertyErrorText, setPropertyErrorText] = useState("");
4244

4345
const calendarState = useCalendarState();
44-
const { dateTimeStart, dateTimeEnd, } = calendarState;
46+
const { dateTimeStart, dateTimeEnd } = calendarState;
4547

4648
useMountEffect(() => getProperties());
4749

50+
useEffect(() => validateForm(), [propertySelection, dateTimeStart, dateTimeEnd]);
51+
4852
useEffect(() => {
4953
axios.post("/api/users/role", {
5054
userrole: RoleEnum.STAFF,
@@ -88,13 +92,8 @@ export const AddTenant = () => {
8892
};
8993

9094
const handleFormSubmit = (data) => {
91-
let body = {
92-
...data,
93-
propertyID: propertySelection[0].key,
94-
staffIDs: staffSelections && staffSelections.map(staff => staff.key)
95-
};
9695
axios
97-
.post(`/api/tenants`, body, makeAuthHeaders(context))
96+
.post(`/api/tenants`, data, makeAuthHeaders(context))
9897
.then((response) => {
9998
Toast("Tenant Created Successfully!", "success");
10099
})
@@ -144,6 +143,17 @@ export const AddTenant = () => {
144143
setStaffSelections(selectedChoices);
145144
};
146145

146+
const validateForm = (values) => {
147+
setIsValidationActive(true);
148+
149+
if (dateTimeStart !== dateTimeEnd) {
150+
setPropertyErrorText(dateTimeStart !== dateTimeEnd && propertySelection && propertySelection.length
151+
? ""
152+
: "*Property is a required field"
153+
);
154+
}
155+
};
156+
147157
return (
148158
<div className='main-container'>
149159
<div>
@@ -158,16 +168,30 @@ export const AddTenant = () => {
158168
occupants: "",
159169
}}
160170
validationSchema={validationSchema}
171+
validate={validateForm}
161172
validateOnBlur={false}
162-
onSubmit={(values, { setSubmitting }) => {
173+
onSubmit={(values, { setSubmitting, resetForm }) => {
163174
const toSubmit = {
164175
...values,
165-
dateTimeStart,
166-
dateTimeEnd,
176+
occupants: values.occupants || null,
177+
unitNum: values.unitNum || null,
178+
propertyID: propertySelection[0].key,
179+
staffIDs: staffSelections && staffSelections.map(staff => staff.key)
167180
};
181+
if (dateTimeStart !== dateTimeEnd) {
182+
toSubmit.dateTimeStart = dateTimeStart;
183+
toSubmit.dateTimeEnd = dateTimeEnd;
184+
}
168185

169186
setSubmitting(true);
170187
handleFormSubmit(toSubmit);
188+
resetForm();
189+
setPropertySearchText("");
190+
setPropertySelection([]);
191+
setStaffSearchText("");
192+
setStaffSelections([]);
193+
setPropertyErrorText("");
194+
setIsValidationActive(false);
171195
setSubmitting(false);
172196
}}
173197
>
@@ -275,16 +299,20 @@ export const AddTenant = () => {
275299
onSelectionChange={setPropertySelection}
276300
onChange={handlePropertySearch}
277301
onClear={handlePropertySearch}
302+
preSelectedChoices={propertySelection}
278303
shadow
279304
/>
305+
{isValidationActive && propertyErrorText ? (
306+
<div className="error-message">{propertyErrorText}</div>
307+
) : null}
280308
<button
281309
className="add-property-button"
282310
onClick={() => setShowAddProperty(!showAddProperty)}
283311
type="button"
284312
>
285313
<i className="fas fa-plus-circle icon-inline-space"></i>
286-
Create New Property
287-
</button>
314+
Create New Property
315+
</button>
288316
</div>
289317
<h1 className="section-title">UNIT</h1>
290318
<div className="form-row form-first-row">
@@ -294,17 +322,17 @@ export const AddTenant = () => {
294322
htmlFor="number"
295323
>
296324
Number
297-
</label>
325+
</label>
298326
<Field
299327
className="column form-field"
300328
type="text"
301329
name="unitNum"
302330
onChange={handleChange}
303331
value={values.unitNum}
304-
placeholder="Unit Number (Optional)"
332+
placeholder="Unit Number"
305333
/>
306-
{errors.number ? (
307-
<div className="error-message">{errors.number}</div>
334+
{errors.unitNum ? (
335+
<div className="error-message">{errors.unitNum}</div>
308336
) : null}
309337
</div>
310338
<div className="form-row">
@@ -339,15 +367,14 @@ export const AddTenant = () => {
339367
className="column form-field"
340368
type="text"
341369
name="lease"
342-
onChange={null}
343-
value={dateTimeEnd !== dateTimeStart ? `${dateTimeStart.toDateString()} - ${dateTimeEnd.toDateString()}` : ""}
344-
370+
onChange={validateForm}
371+
value={dateTimeEnd !== dateTimeStart
372+
? `${dateTimeStart.toDateString()} - ${dateTimeEnd.toDateString()}`
373+
: ""
374+
}
345375
placeholder="Lease dates (Start and End)"
346376
/>
347377
<CalendarModal title="Lease Range" calendarState={calendarState} iconYPosition="0.8rem" />
348-
{errors.lease ? (
349-
<div className="error-message">{errors.lease}</div>
350-
) : null}
351378
</div>
352379
<div className="button-container">
353380
<Button

0 commit comments

Comments
 (0)