Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functionality to allow for multiple TEC attendance picture submission #576

Merged
merged 16 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@
display: flex;
justify-content: flex-end;
}

.modalContent {
max-height: 60vh !important;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
const [pendingAttendance, setPendingAttendance] = useState<TeamEventAttendance[]>([]);
const [rejectedAttendance, setRejectedAttendance] = useState<TeamEventAttendance[]>([]);
const [isAttendanceLoading, setIsAttendanceLoading] = useState<boolean>(true);
const [imageIndex, setImageIndex] = useState<number[]>([0]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this imageIndex, setImageIndex hook to be an array. How about just a number to say how many image upload slots you have?

const [images, setImages] = useState<string[]>([]);

useEffect(() => {
TeamEventsAPI.getAllTeamEventInfo().then((teamEvents) => setTeamEventInfoList(teamEvents));
Expand All @@ -30,17 +32,25 @@
});
}, []);

const handleAddIconClick = () => {
const newIndex = imageIndex.length;
setImageIndex([...imageIndex, newIndex]);
};

const handleNewImage = (e: React.ChangeEvent<HTMLInputElement>): void => {
if (!e.target.files) return;
const newImage = URL.createObjectURL(e.target.files[0]);
setImage(newImage);
setImages([...images, newImage]);
};

const requestTeamEventCredit = async (eventCreditRequest: TeamEventAttendance) => {
const requestTeamEventCredit = async (
eventCreditRequest: TeamEventAttendance,
uploadedImage: string
) => {
const createdAttendance = await TeamEventsAPI.requestTeamEventCredit(eventCreditRequest);

// upload image
const blob = await fetch(image).then((res) => res.blob());
const blob = await fetch(uploadedImage).then((res) => res.blob());
const imageURL: string = window.URL.createObjectURL(blob);
await ImagesAPI.uploadEventProofImage(blob, eventCreditRequest.image).then(() =>
setImage(imageURL)
Expand Down Expand Up @@ -70,7 +80,25 @@
contentMsg: 'Team events must be logged for at least 0.5 hours!'
});
} else {

images.map(async (image, index) => {
const newTeamEventAttendance: TeamEventAttendance = {
member: userInfo,
hoursAttended: teamEvent.hasHours ? Number(hours) : undefined,
image: `eventProofs/${getNetIDFromEmail(userInfo.email)}/${new Date().toISOString()}`,
eventUuid: teamEvent.uuid,
pending: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type checker should catch this: we killed off the pending field already in #575.

status: 'pending' as Status,
reason: '',
uuid: ''
};

const createdAttendance = await requestTeamEventCredit(
newTeamEventAttendance,
images[index]
);

const newTeamEventAttendance: TeamEventAttendance = {

Check warning on line 101 in frontend/src/components/Forms/TeamEventCreditsForm/TeamEventCreditsForm.tsx

View workflow job for this annotation

GitHub Actions / check

'newTeamEventAttendance' is already defined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both newTeamEventAttendance and newTeamEventCreditAttendance here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrew032011 the check that did not pass said cannot redeclare block-scoped variable 'newteameventattendance'.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but those two variables look almost the exact same, do we need both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you meant! I removed one of them as I was cleaning up code

member: userInfo,
hoursAttended: teamEvent.hasHours ? Number(hours) : undefined,
image: `eventProofs/${getNetIDFromEmail(userInfo.email)}/${new Date().toISOString()}`,
Expand All @@ -80,7 +108,7 @@
uuid: ''
};

requestTeamEventCredit(newTeamEventAttendance).then((createdAttendance) => {

if (createdAttendance) {
const updatedAttendance = { ...newTeamEventAttendance, uuid: createdAttendance.uuid };
setPendingAttendance((pending) => [...pending, updatedAttendance]);
Expand Down Expand Up @@ -198,15 +226,27 @@
Please include a picture of yourself (and others) and/or an email chain only if the
former is not possible.
</p>
<input
id="newImage"
type="file"
accept="image/png, image/jpeg"
defaultValue=""
value={image ? undefined : ''}
onChange={handleNewImage}
/>
{imageIndex.map((item, index) => (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, I don't think you're using item here either. Actually item and index are equal here. Don't we just want to know how many images there are? So the only state we want to be storing is numImagesSlots (or something like that), just a number.

<div className="input_container" style={{ marginBottom: '10px' }} key={index}>
<input
id="newImage"
type="file"
accept="image/png, image/jpeg"
defaultValue=""
value={image ? undefined : ''}
onChange={handleNewImage}
/>
</div>
))}
</div>
<Form.Button
floated="right"
size="mini"
style={{ marginBottom: '10px' }}
onClick={handleAddIconClick}
>
+
</Form.Button>
<Form.Button floated="right" onClick={submitTeamEventCredit}>
Submit
</Form.Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Dispatch, SetStateAction } from 'react';
import { Card, Loader, Message, Button } from 'semantic-ui-react';
import React, { Dispatch, SetStateAction, useState } from 'react';
import { Card, Loader, Message, Button, Modal, Header, Image } from 'semantic-ui-react';
import { useSelf } from '../../Common/FirestoreDataProvider';
import styles from './TeamEventCreditsForm.module.css';
import { TeamEventsAPI } from '../../../API/TeamEventsAPI';
Expand Down Expand Up @@ -28,6 +28,9 @@ const TeamEventCreditDashboard = (props: {
isAttendanceLoading,
setPendingAttendance
} = props;
const [image, setImage] = useState('');
const [open, setOpen] = useState(false);
const [isLoading, setLoading] = useState(true);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userRole = useSelf()!.role;
Expand All @@ -41,6 +44,14 @@ const TeamEventCreditDashboard = (props: {
return 1;
};

const getTeamEventImage = (attendance: TeamEventAttendance) => {
setLoading(true);
ImagesAPI.getEventProofImage(attendance.image).then((url: string) => {
setImage(url);
setLoading(false);
});
};

const deleteTECAttendanceRequest = (attendance: TeamEventAttendance) => {
TeamEventsAPI.deleteTeamEventAttendance(attendance.uuid)
.then(() => {
Expand Down Expand Up @@ -121,15 +132,53 @@ const TeamEventCreditDashboard = (props: {
{attendance.reason ? <Card.Meta>Reason: {attendance.reason}</Card.Meta> : null}
<Card.Meta>
{attendance.status === 'pending' && (
<Button
basic
color="red"
onClick={() => {
deleteTECAttendanceRequest(attendance);
}}
>
Cancel
</Button>
<div>
<Button
basic
color="red"
floated="right"
size="small"
onClick={() => {
deleteTECAttendanceRequest(attendance);
}}
>
Cancel
</Button>

<Modal
closeIcon
onClose={() => setOpen(false)}
onOpen={() => {
getTeamEventImage(attendance);
setOpen(true);
}}
open={open}
trigger={
<Button basic color="green" floated="left" size="small">
Preview
</Button>
}
>
<Modal.Header>Team Event Credit Preview</Modal.Header>
<Modal.Content className={styles.modalContent} scrolling>
<Modal.Description>
<Header>
{attendance.member.firstName} {attendance.member.lastName}
</Header>
<p>Team Event: {teamEvent.name}</p>
<p>Number of Credits: {teamEvent.numCredits}</p>
{teamEvent.hasHours && (
<p> Hours Attended: {attendance.hoursAttended}</p>
)}
{isLoading ? (
<Loader className="modalLoader" active inline />
) : (
<Image src={image} />
)}
</Modal.Description>
</Modal.Content>
</Modal>
</div>
)}
</Card.Meta>
{COMMUNITY_EVENTS && (
Expand Down
Loading