Skip to content

Commit 5a15da8

Browse files
author
Chris Boe
authored
Merge pull request #494 from codeforpdx/dpt-add-tenant-validation
Add tenant validation to match (current) backend requirements
2 parents 5a90c39 + 74c7808 commit 5a15da8

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

src/components/CalendarModal/CalendarModal.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ const today = new Date()
7979
export function useCalendarState(startDateInit = today, endDateInit = today) {
8080
const [dateTimeStart, setStart] = useState(startDateInit)
8181
const [dateTimeEnd, setEnd] = useState(endDateInit)
82+
const resetDates = () => {
83+
setStart(startDateInit);
84+
setEnd(startDateInit);
85+
};
8286

83-
return { dateTimeStart, dateTimeEnd, setStart, setEnd }
87+
return { dateTimeStart, dateTimeEnd, setStart, setEnd, resetDates }
8488
}

src/views/AddTenant/addTenant.js

Lines changed: 53 additions & 25 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, resetDates } = 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.length
151+
? ""
152+
: "*Property is a required field"
153+
);
154+
}
155+
};
156+
147157
return (
148158
<div className='main-container'>
149159
<div>
@@ -158,16 +168,31 @@ 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.length ? propertySelection[0].key : null,
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+
resetDates();
195+
setIsValidationActive(false);
171196
setSubmitting(false);
172197
}}
173198
>
@@ -275,26 +300,30 @@ export const AddTenant = () => {
275300
onSelectionChange={setPropertySelection}
276301
onChange={handlePropertySearch}
277302
onClear={handlePropertySearch}
303+
preSelectedChoices={propertySelection}
278304
shadow
279305
/>
306+
{isValidationActive && propertyErrorText ? (
307+
<div className="error-message">{propertyErrorText}</div>
308+
) : null}
280309
<button
281310
className="add-property-button"
282311
onClick={() => setShowAddProperty(!showAddProperty)}
283312
type="button"
284313
>
285314
<i className="fas fa-plus-circle icon-inline-space"></i>
286-
Create New Property
287-
</button>
315+
Create New Property
316+
</button>
288317
</div>
289-
<h1 className="section-title">UNIT</h1>
318+
<h1 className="section-title">LEASE</h1>
290319
<div className="form-row form-first-row">
291320
<label
292321
className="column is-one-fifth"
293322
id="number"
294323
htmlFor="number"
295324
>
296325
Number
297-
</label>
326+
</label>
298327
<Field
299328
className="column form-field"
300329
type="text"
@@ -303,8 +332,8 @@ export const AddTenant = () => {
303332
value={values.unitNum}
304333
placeholder="Unit Number (Optional)"
305334
/>
306-
{errors.number ? (
307-
<div className="error-message">{errors.number}</div>
335+
{errors.unitNum ? (
336+
<div className="error-message">{errors.unitNum}</div>
308337
) : null}
309338
</div>
310339
<div className="form-row">
@@ -321,7 +350,7 @@ export const AddTenant = () => {
321350
name="occupants"
322351
onChange={handleChange}
323352
value={values.occupants}
324-
placeholder="Total number of unit tenants"
353+
placeholder="Total number of unit tenants (Optional)"
325354
/>
326355
{errors.occupants ? (
327356
<div className="error-message">{errors.occupants}</div>
@@ -339,15 +368,14 @@ export const AddTenant = () => {
339368
className="column form-field"
340369
type="text"
341370
name="lease"
342-
onChange={null}
343-
value={dateTimeEnd !== dateTimeStart ? `${dateTimeStart.toDateString()} - ${dateTimeEnd.toDateString()}` : ""}
344-
345-
placeholder="Lease dates (Start and End)"
371+
onChange={validateForm}
372+
value={dateTimeEnd !== dateTimeStart
373+
? `${dateTimeStart.toDateString()} - ${dateTimeEnd.toDateString()}`
374+
: ""
375+
}
376+
placeholder="Lease dates (Optional)"
346377
/>
347378
<CalendarModal title="Lease Range" calendarState={calendarState} iconYPosition="0.8rem" />
348-
{errors.lease ? (
349-
<div className="error-message">{errors.lease}</div>
350-
) : null}
351379
</div>
352380
<div className="button-container">
353381
<Button

0 commit comments

Comments
 (0)