Skip to content

Commit

Permalink
Merge pull request #97 from uwblueprint/ammielle-kathleen/edit-reside…
Browse files Browse the repository at this point in the history
…nts-modal

Edit Residents Modal
  • Loading branch information
ya5er authored Jul 29, 2024
2 parents 1813a92 + ec33fca commit 7e08295
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 50 deletions.
29 changes: 19 additions & 10 deletions frontend/src/components/common/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import React from "react";
import {
Button,
Input,
Select,
Flex,
FormControl,
FormLabel,
InputRightElement,
InputGroup,
InputLeftElement,
Checkbox,
Container,
} from "@chakra-ui/react";

import VisibilityIcon from "@mui/icons-material/Visibility";
Expand All @@ -24,10 +21,11 @@ const FormField = ({
onBlur,
submitPressed,
required = false,
error = false,
isPassword = false,
showPassword,
setShowPassword,
leftElement
leftElement,
}: {
label: string;
value: string;
Expand All @@ -36,6 +34,7 @@ const FormField = ({
onBlur?: () => void;
submitPressed: boolean;
required?: boolean;
error?: boolean;
isPassword?: boolean;
showPassword?: boolean;
setShowPassword?: React.Dispatch<React.SetStateAction<boolean>>;
Expand All @@ -47,15 +46,25 @@ const FormField = ({
{label}
</FormLabel>
<InputGroup>
{leftElement && <InputLeftElement height='34px' pointerEvents='none' color='black'>
<Flex>{leftElement}</Flex>
</InputLeftElement>}
{leftElement && (
<InputLeftElement height="34px" pointerEvents="none" color="black">
<Flex>{leftElement}</Flex>
</InputLeftElement>
)}

<Input
variant="primary"
placeholder='Enter amount'
borderColor={submitPressed && !value ? "red.error" : "gray.300"}
boxShadow={submitPressed && !value ? "0 0 2px red.error" : "none"}
placeholder=""
borderColor={
error || (submitPressed && !value && required)
? "red.error"
: "gray.300"
}
boxShadow={
error || (submitPressed && !value && required)
? "0 0 2px red.error"
: "none"
}
type={
isPassword && setShowPassword && !showPassword ? "password" : type
}
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/common/ModalContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const ModalContainer = ({
children,
}: Props): React.ReactElement => {
return (
<Modal isOpen={isOpen} onClose={() => setIsOpen(false)}>
<Modal
closeOnOverlayClick={false}
isOpen={isOpen}
onClose={() => setIsOpen(false)}
>
<ModalOverlay />
<ModalContent>
<ModalHeader>
Expand Down
172 changes: 172 additions & 0 deletions frontend/src/components/pages/residents/ResidentEditModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import React, { useState } from "react";
import { Button, Text, Flex } from "@chakra-ui/react";
import ModalContainer from "../../common/ModalContainer";
import FormField from "../../common/FormField";

export type ResidentEditInfo = {
residentId: number;
roomNumber: number;
arrivalDate: string;
departureDate: string;
password: string;
};

type Props = {
isOpen: boolean;
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
residentInfo: ResidentEditInfo;
onCloseEditModal: any;
};

const ResidentEditModal = ({
isOpen,
setIsOpen,
residentInfo,
onCloseEditModal,
}: Props): React.ReactElement => {
const [residentId, setResidentId] = useState(residentInfo.residentId);
const [password, setPassword] = useState(residentInfo.password);
const [arrivalDate, setArrivalDate] = useState(residentInfo.arrivalDate);
const [departureDate, setDepartureDate] = useState(
residentInfo.departureDate,
);

const [roomNumber, setRoomNumber] = useState(residentInfo.roomNumber);

const [showPassword, setShowPassword] = useState(false);
const [submitPressed, setSubmitPressed] = useState(false);

const [roomList, setRoomList] = useState([1, 2, 3, 4, 5, 6, 7, 8]);
const [residentIdList, setresidentIdList] = useState<Array<number>>([
12345, 67890, 23456, 78901, 34567, 89012, 45678, 56789, 12346, 67891, 23457,
78902, 34568, 89013, 45679, 90124, 56790, 12347, 67892,
]);

const [invalidRoomNumber, setInvalidRoomNumber] = useState("");
const [invalidResidentId, setInvalidResidentId] = useState("");
// useEffect(() => {
// // TODO: get roomList, residentIdList
// }, []);

const handleSubmit = () => {
setSubmitPressed(true);
if (roomNumber !== residentInfo.roomNumber && roomNumber in roomList) {
setInvalidRoomNumber(
"This room number is already taken. Note: To set this field, please remove the room number from the Resident who occupies the room.",
);
} else {
setInvalidRoomNumber("");
}

if (
residentId !== residentInfo.residentId &&
residentIdList.includes(residentId)
) {
setInvalidResidentId("This ID number is already taken.");
} else {
setInvalidResidentId("");
}

if (invalidRoomNumber || invalidResidentId) {
return;
}

// TODO: API POST to Residents/Participants

if (!residentId || !password || !arrivalDate) {
// TODO: Add error handling
}
// TODO: API call to add resident
};

const resetFormState = () => {
onCloseEditModal();
};

return (
<ModalContainer title="Edit Resident" isOpen={isOpen} setIsOpen={setIsOpen}>
<Flex flexDir="column" gap="20px">
<Flex flexDir="column">
<FormField
label="ID Number"
value={residentId.toString()}
type="number"
onChange={(e) => setResidentId(parseInt(e.target.value, 10))}
submitPressed={submitPressed}
required
error={invalidResidentId !== ""}
/>
{invalidResidentId && (
<Text mb="5px" mt="1px" color="red.error" fontWeight="700">
{invalidResidentId}
</Text>
)}
</Flex>

<Flex flexDir="column">
<FormField
label="Room Number (Optional)"
value={roomNumber.toString()}
type="number"
onChange={(e) => setRoomNumber(parseInt(e.target.value, 10))}
submitPressed={submitPressed}
error={invalidRoomNumber !== ""}
/>
{invalidRoomNumber && (
<Text mb="5px" color="red.error" fontWeight="700">
{invalidRoomNumber}
</Text>
)}
</Flex>

<Flex gap="20px">
<FormField
label="Arrival Date"
value={arrivalDate.toString()}
type="date"
onChange={() => {}}
submitPressed={submitPressed}
required
/>
<FormField
label="Departure Date (Optional)"
value={departureDate.toString()}
type="date"
onChange={(e) => setDepartureDate(e.target.value)}
submitPressed={submitPressed}
/>
</Flex>
<Flex>
<FormField
label="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
submitPressed={submitPressed}
required
isPassword
showPassword={showPassword}
setShowPassword={setShowPassword}
/>
</Flex>

<Flex justifyContent="flex-end">
<Button
variant="cancel"
mr="8px"
onClick={() => {
resetFormState();
setIsOpen(false);
}}
>
Cancel
</Button>
<Button variant="primary" onClick={handleSubmit}>
Save
</Button>
</Flex>
</Flex>
</ModalContainer>
);
};

export default ResidentEditModal;
5 changes: 0 additions & 5 deletions frontend/src/components/pages/residents/ResidentModal.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import React, { useState } from "react";
import {
Button,
Input,
Textarea,
Flex,
FormControl,
FormLabel,
InputRightElement,
InputGroup,
} from "@chakra-ui/react";
import VisibilityIcon from "@mui/icons-material/Visibility";
import VisibilityOffIcon from "@mui/icons-material/VisibilityOff";

import ModalContainer from "../../common/ModalContainer";
import FormField from "../../common/FormField";
Expand Down
39 changes: 31 additions & 8 deletions frontend/src/components/pages/residents/ResidentsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import CommonTable, {
TableData,
} from "../../common/CommonTable";
import ResidentModal from "./ResidentModal";
import ResidentEditModal, { ResidentEditInfo } from "./ResidentEditModal";
import { residentsMockData } from "../../../mocks/residents";

const columnTypes: ColumnInfoTypes[] = [
Expand All @@ -33,21 +34,30 @@ const columnTypes: ColumnInfoTypes[] = [
header: "Departure Date",
key: "departureDate",
},
{
header: "Email",
key: "email",
},
];

const ResidentsPage = (): React.ReactElement => {
const [residents, setResidents] = useState<TableData[]>([]);
const [isModalOpen, setIsModalOpen] = useState(false);
const [isModalOpen, setIsModalOpen] = useState("none");
const [residentEditInfo, setEditInfo] = useState<ResidentEditInfo>();

useEffect(() => {
// TODO: Fetch residents from API
setResidents(residentsMockData);
}, []);

const handleResidentEdit = (row: any) => {
setIsModalOpen("edit");
// console.log(row);
setEditInfo(row);
};

const handleResidentSubmitEdit = () => {
setEditInfo(undefined);

// TODO: modify data
};

return (
<Flex flexDir="column" flexGrow={1} p="20px">
<Flex justifyContent="space-between" p="10px">
Expand All @@ -61,17 +71,30 @@ const ResidentsPage = (): React.ReactElement => {
variant="primary"
leftIcon={<Icon as={Add} color="white" />}
size="sm"
onClick={() => setIsModalOpen(true)}
onClick={() => setIsModalOpen("add")}
>
Add Resident
</Button>
</Flex>
<CommonTable
data={residents}
columnInfo={columnTypes}
onEdit={() => {}}
onEdit={handleResidentEdit}
/>

<ResidentModal
isOpen={isModalOpen === "add"}
setIsOpen={() => setIsModalOpen("none")}
/>
<ResidentModal isOpen={isModalOpen} setIsOpen={setIsModalOpen} />

{residentEditInfo && (
<ResidentEditModal
residentInfo={residentEditInfo}
isOpen={isModalOpen === "edit"}
setIsOpen={() => setIsModalOpen("none")}
onCloseEditModal={handleResidentSubmitEdit}
/>
)}
</Flex>
);
};
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/components/pages/tasks/TaskModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const TaskModal = ({ isOpen, setIsOpen }: Props): React.ReactElement => {
const handleDelete = () => {};

return (
<ModalContainer
<ModalContainer
title="Edit Chore"
isOpen={isOpen}
setIsOpen={setIsOpen}
Expand All @@ -197,7 +197,7 @@ const TaskModal = ({ isOpen, setIsOpen }: Props): React.ReactElement => {
<FormLabel mb="5px" color="gray.main" fontWeight="700">
Location
</FormLabel>

<Select
variant="primary"
value={location}
Expand Down Expand Up @@ -244,11 +244,13 @@ const TaskModal = ({ isOpen, setIsOpen }: Props): React.ReactElement => {
>
Cancel
</Button>
<Button variant="primary" onClick={ () => {
<Button
variant="primary"
onClick={() => {
handleSubmit();
setIsOpen(false);
}}
>
>
Save
</Button>
</Flex>
Expand Down
Loading

0 comments on commit 7e08295

Please sign in to comment.