-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ab23be
commit 3e5236b
Showing
15 changed files
with
312 additions
and
720 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,23 @@ | ||
import { useContext, useEffect, useState } from "react"; | ||
import { createAttendanceChange } from "../client/client"; | ||
import { AuthContext } from "../hooks/useAuth"; | ||
import { AttendanceChange, AttendanceData, ChangeStatus } from "../util/Types"; | ||
import { AttendanceChange, ChangeStatus } from "../util/Types"; | ||
import { AttendanceButtonStyles } from "../util/styleConfig"; | ||
import Loading from "./Loading"; | ||
|
||
interface AttendanceButtonProps { | ||
openModal: () => void; | ||
setIsRegistered: React.Dispatch<React.SetStateAction<boolean>>; | ||
setErrorType: React.Dispatch<React.SetStateAction<number>>; | ||
attendanceChange?: AttendanceChange; | ||
createdAttendanceChange: AttendanceData | {}; | ||
eventid: number; | ||
} | ||
|
||
export const AttendanceButton = ({ | ||
attendanceChange, | ||
createdAttendanceChange, | ||
eventid, | ||
openModal, | ||
setIsRegistered, | ||
setErrorType, | ||
}: AttendanceButtonProps) => { | ||
const { member } = useContext(AuthContext); | ||
const [isCreatingAttendance, setIsCreatingAttendance] = useState(false); | ||
const [initialAttendanceStatus, setAttendanceStatus] = useState( | ||
attendanceChange?.change_status || ChangeStatus.UNREGISTER | ||
); | ||
|
||
useEffect(() => { | ||
const makeAttendanceChange = async () => { | ||
try { | ||
setIsCreatingAttendance(true); | ||
//using non-null assertion since it's assumed the user is logged in to make it past the home page | ||
if (member) { | ||
await createAttendanceChange(member.id, eventid); | ||
setIsRegistered(false); | ||
} | ||
setIsCreatingAttendance(false); | ||
// once we successfully created an AttendanceChange its back to pending | ||
setAttendanceStatus(ChangeStatus.NOT_REVIEWED); | ||
} catch (e) { | ||
setErrorType(1); | ||
setIsCreatingAttendance(false); | ||
} | ||
}; | ||
//on Mount this useEffect starts, | ||
//so only want this makeAttendanceChange function to start when we actually have something/ this json is not empty | ||
if (!(Object.keys(createdAttendanceChange).length === 0)) { | ||
makeAttendanceChange(); | ||
} | ||
}, [member, createdAttendanceChange, eventid, setErrorType, setIsRegistered]); | ||
|
||
const renderText = AttendanceButtonStyles[initialAttendanceStatus]; | ||
const renderText = | ||
AttendanceButtonStyles[ | ||
attendanceChange?.changeStatus || ChangeStatus.UNREGISTER | ||
]; | ||
|
||
return ( | ||
<button onClick={openModal} className={`${renderText.className}`}> | ||
{isCreatingAttendance ? <Loading fullScreen={false} /> : renderText.text} | ||
{renderText.text} | ||
</button> | ||
); | ||
}; |
Oops, something went wrong.