Skip to content

Commit

Permalink
Resident form updates (#234)
Browse files Browse the repository at this point in the history
* shift resident columns around and add move out date to add resident form

* run. the. linter.
  • Loading branch information
Connor Bechthold authored Feb 6, 2024
1 parent 9d4249f commit 3a6c403
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 44 deletions.
7 changes: 4 additions & 3 deletions frontend/src/APIClients/ResidentAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const createResident = async ({
initial,
roomNum,
dateJoined,
dateLeft,
buildingId,
}: CreateResidentParams): Promise<boolean | ErrorResponse> => {
try {
Expand All @@ -93,7 +94,7 @@ const createResident = async ({
)}`;
await baseAPIClient.post(
"/residents",
{ initial, roomNum, dateJoined, buildingId },
{ initial, roomNum, dateJoined, dateLeft, buildingId },
{ headers: { Authorization: bearerToken } },
);
return true;
Expand Down Expand Up @@ -135,8 +136,8 @@ const editResident = async ({
initial,
roomNum,
dateJoined,
buildingId,
dateLeft,
buildingId,
}: EditResidentParams): Promise<boolean | ErrorResponse> => {
try {
const bearerToken = `Bearer ${getLocalStorageObjProperty(
Expand All @@ -145,7 +146,7 @@ const editResident = async ({
)}`;
await baseAPIClient.put(
`/residents/${id}`,
{ initial, roomNum, dateJoined, buildingId, dateLeft },
{ initial, roomNum, dateJoined, dateLeft, buildingId },
{ headers: { Authorization: bearerToken } },
);
return true;
Expand Down
87 changes: 68 additions & 19 deletions frontend/src/components/forms/CreateResident.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import {
ModalFooter,
ScaleFade,
Divider,
InputGroup,
IconButton,
InputRightElement,
} from "@chakra-ui/react";

import { AddIcon } from "@chakra-ui/icons";
import { AddIcon, SmallCloseIcon } from "@chakra-ui/icons";
import { SingleDatepicker } from "chakra-dayzed-datepicker";
import { Col, Row } from "react-bootstrap";
import CreateToast from "../common/Toasts";
Expand Down Expand Up @@ -49,11 +52,12 @@ const CreateResident = ({
const [initials, setInitials] = useState("");
const [roomNumber, setRoomNumber] = useState("");
const [moveInDate, setMoveInDate] = useState(new Date());
const [moveOutDate, setMoveOutDate] = useState<Date | undefined>();
const [buildingId, setBuildingId] = useState<number>(-1);

const [initialsError, setInitialsError] = useState(false);
const [roomNumberError, setRoomNumberError] = useState(false);
const [moveInDateError, setMoveInDateError] = useState(false);
const [moveOutDateError, setMoveOutDateError] = useState(false);
const [buildingError, setBuildingError] = useState(false);

const [isOpen, setIsOpen] = useState(false);
Expand All @@ -67,6 +71,7 @@ const CreateResident = ({
initial: initials.toUpperCase(),
roomNum: roomNumber,
dateJoined: convertToString(moveInDate),
dateLeft: moveOutDate ? convertToString(moveOutDate) : undefined,
buildingId,
});

Expand Down Expand Up @@ -98,9 +103,16 @@ const CreateResident = ({
};

const handleMoveInDateChange = (inputValue: Date) => {
if (inputValue !== null) {
setMoveInDate(inputValue);
setMoveInDateError(false);
setMoveInDate(inputValue);
if (moveOutDate && inputValue < moveOutDate) {
setMoveOutDateError(false);
}
};

const handleMoveOutDateChange = (inputValue: Date) => {
setMoveOutDate(inputValue);
if (inputValue > moveInDate) {
setMoveOutDateError(false);
}
};

Expand Down Expand Up @@ -133,12 +145,13 @@ const CreateResident = ({
setInitials("");
setRoomNumber("");
setMoveInDate(new Date());
setMoveOutDate(undefined);
setBuildingId(-1);

// Reset the error states
setInitialsError(false);
setRoomNumberError(false);
setMoveInDateError(false);
setMoveOutDateError(false);
setBuildingError(false);

// Reset alert state
Expand All @@ -150,17 +163,20 @@ const CreateResident = ({
};

const handleSubmit = () => {
setInitialsError(initials.length !== 2);
setRoomNumberError(roomNumber.length !== 3);
setBuildingError(buildingId === -1);

// Prevents form submission if any required values are incorrect
if (
initials.length !== 2 ||
roomNumber.length !== 3 ||
moveInDateError ||
buildingId === -1
) {
if (initials.length !== 2) {
setInitialsError(true);
return;
}
if (!roomNumber || roomNumber.toString().length !== 3) {
setRoomNumberError(true);
return;
}
if (moveOutDate && moveOutDate <= moveInDate) {
setMoveOutDateError(true);
return;
}
if (buildingId === -1) {
setBuildingError(true);
return;
}

Expand Down Expand Up @@ -222,7 +238,7 @@ const CreateResident = ({
</Row>
<Row style={{ marginTop: "16px" }}>
<Col>
<FormControl isRequired isInvalid={moveInDateError}>
<FormControl isRequired>
<FormLabel>Move In Date</FormLabel>
<SingleDatepicker
name="date-input"
Expand All @@ -231,7 +247,40 @@ const CreateResident = ({
propsConfigs={singleDatePickerStyle}
/>
<FormErrorMessage>
Move In Date is required.
Move in date is required.
</FormErrorMessage>
</FormControl>
</Col>
<Col>
<FormControl isInvalid={moveOutDateError}>
<FormLabel>Move Out Date</FormLabel>
<InputGroup>
<SingleDatepicker
name="date-input"
date={moveOutDate}
onDateChange={handleMoveOutDateChange}
propsConfigs={singleDatePickerStyle}
/>
{moveOutDate && (
<InputRightElement>
<IconButton
onClick={() => setMoveOutDate(undefined)}
aria-label="clear"
variant="icon"
icon={
<SmallCloseIcon
boxSize="5"
color="gray.200"
_hover={{ color: "gray.400" }}
transition="color 0.1s ease-in-out"
/>
}
/>
</InputRightElement>
)}
</InputGroup>
<FormErrorMessage>
Move out date must be after the move in date.
</FormErrorMessage>
</FormControl>
</Col>
Expand Down
40 changes: 19 additions & 21 deletions frontend/src/components/forms/EditResident.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const EditResident = ({
initial: initials.toUpperCase(),
roomNum: roomNumber,
dateJoined: convertToString(moveInDate),
buildingId,
dateLeft: moveOutDate ? convertToString(moveOutDate) : undefined,
buildingId,
});

if (isErrorResponse(res)) {
Expand Down Expand Up @@ -227,28 +227,10 @@ const EditResident = ({
propsConfigs={singleDatePickerStyle}
/>
<FormErrorMessage>
Move In Date is required and must be before Move Out Date.
Move in date is required and must be before move out date.
</FormErrorMessage>
</FormControl>
</Col>
</Row>
<Row style={{ marginTop: "16px" }}>
<Col>
<FormControl isRequired isInvalid={buildingError}>
<FormLabel>Building</FormLabel>
<Select
options={buildingOptions}
defaultValue={buildingOptions.find(
(item) => item.value === buildingId,
)}
onChange={handleBuildingChange}
styles={selectStyle}
/>
<FormErrorMessage>Building is required.</FormErrorMessage>
</FormControl>
</Col>
</Row>
<Row style={{ marginTop: "16px" }}>
<Col>
<FormControl isInvalid={moveOutDateError}>
<FormLabel>Move Out Date</FormLabel>
Expand Down Expand Up @@ -278,11 +260,27 @@ const EditResident = ({
)}
</InputGroup>
<FormErrorMessage marginBottom="8px">
Move out Date must be after Move in Date
Move out date must be after the move in date.
</FormErrorMessage>
</FormControl>
</Col>
</Row>
<Row style={{ marginTop: "16px" }}>
<Col>
<FormControl isRequired isInvalid={buildingError}>
<FormLabel>Building</FormLabel>
<Select
options={buildingOptions}
defaultValue={buildingOptions.find(
(item) => item.value === buildingId,
)}
onChange={handleBuildingChange}
styles={selectStyle}
/>
<FormErrorMessage>Building is required.</FormErrorMessage>
</FormControl>
</Col>
</Row>
</ModalBody>
<ModalFooter>
<Button onClick={handleSave} variant="primary" type="submit">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/ResidentTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type CountResidentsResponse = {

export type CreateResidentParams = Omit<
Resident,
"id" | "residentId" | "dateLeft" | "building" | "status"
"id" | "residentId" | "building" | "status"
> & { buildingId: number };

export type EditResidentParams = Omit<
Expand Down

0 comments on commit 3a6c403

Please sign in to comment.