Skip to content

Commit

Permalink
Fix redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbar01234 committed Mar 14, 2024
1 parent 22ddecc commit fd6660f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
31 changes: 15 additions & 16 deletions src/components/AddSenior.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ type AddSeniorTileProps = {
setSeniorPatch: Dispatch<SetStateAction<string>>;
};

type SeniorData = Pick<
z.infer<typeof seniorSchema>,
"firstname" | "lastname" | "location" | "description"
>;

const EMPTY_SENIOR: SeniorData = {
firstname: "",
lastname: "",
location: "",
description: "",
};

export const AddSeniorTile = ({
showAddSeniorPopUp,
setShowAddSeniorPopUp,
Expand Down Expand Up @@ -92,11 +104,6 @@ const StudentSelector = ({
);
};

type SeniorData = Pick<
z.infer<typeof seniorSchema>,
"firstname" | "lastname" | "location" | "description"
>;

const AddSenior = ({
seniors,
students,
Expand All @@ -106,15 +113,7 @@ const AddSenior = ({
seniorPatch,
setSeniorPatch,
}: AddSeniorProps) => {
const emptySenior = useMemo(() => {
return {
firstname: "",
lastname: "",
location: "",
description: "",
};
}, []);
const [seniorData, setSeniorData] = useState<SeniorData>(emptySenior);
const [seniorData, setSeniorData] = useState<SeniorData>(EMPTY_SENIOR);
const [selectedStudents, setSelectedStudents] = useState<User[]>([]);
const [currentImage, setCurrentImage] = useState<string | StaticImageData>(
ImageIcon
Expand All @@ -135,7 +134,7 @@ const AddSenior = ({
location: initialSenior.location,
description: initialSenior.description,
});
}, [initialSenior, emptySenior]);
}, [initialSenior]);

useEffect(() => {
if (initialSenior) {
Expand All @@ -149,7 +148,7 @@ const AddSenior = ({

const handlePopUp = () => {
setShowAddSeniorPopUp(!showAddSeniorPopUp);
setSeniorData(emptySenior);
setSeniorData(EMPTY_SENIOR);
setSelectedStudents([]);
setCurrentImage(ImageIcon);
setSeniorPatch(""); // empty string used as falsey value to indicate update or patch
Expand Down
5 changes: 1 addition & 4 deletions src/components/SeniorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const SeniorView = ({ seniors, students }: SeniorViewProps) => {
setSeniorPatch={setSeniorPatch}
/>
}
elements={seniorsState ? seniorsState : []}
elements={seniorsState}
display={(senior) => {
const options: Parameters<typeof TileEdit>[0]["options"] = [];

Expand All @@ -43,9 +43,6 @@ export const SeniorView = ({ seniors, students }: SeniorViewProps) => {
onClick: (e) => {
e.stopPropagation();
e.preventDefault();
if (!setSeniorPatch || !setShowAddSeniorPopUp) {
return;
}
setSeniorPatch(senior.id);
setShowAddSeniorPopUp(true);
},
Expand Down

0 comments on commit fd6660f

Please sign in to comment.