Skip to content

Commit b7c2dab

Browse files
Merge pull request #108 from uwblueprint/david/fix-file-upload-for-events
Fix File Upload for Absences
2 parents b26d9c9 + 318de59 commit b7c2dab

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

src/components/DeclareAbsenceForm.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ const DeclareAbsenceForm: React.FC<DeclareAbsenceFormProps> = ({
271271
<FormErrorMessage>{errors.reasonOfAbsence}</FormErrorMessage>
272272
</FormControl>
273273

274-
<FileUpload lessonPlan={lessonPlan} setLessonPlan={setLessonPlan} />
274+
<FormControl>
275+
<Text textStyle="h4" mb={2}>
276+
Lesson Plan
277+
</Text>
278+
<FileUpload lessonPlan={lessonPlan} setLessonPlan={setLessonPlan} />
279+
</FormControl>
275280

276281
<FormControl>
277282
<FormLabel htmlFor="notes" sx={{ display: 'flex' }}>

src/components/EditAbsenceForm.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,16 @@ const EditAbsenceForm: React.FC<EditAbsenceFormProps> = ({
274274
<FormErrorMessage>{errors.reasonOfAbsence}</FormErrorMessage>
275275
</FormControl>
276276

277-
<FileUpload
278-
lessonPlan={lessonPlan}
279-
setLessonPlan={setLessonPlan}
280-
existingFile={existingLessonPlan}
281-
/>
277+
<FormControl>
278+
<Text textStyle="h4" mb={2}>
279+
Lesson Plan
280+
</Text>
281+
<FileUpload
282+
lessonPlan={lessonPlan}
283+
setLessonPlan={setLessonPlan}
284+
existingFile={existingLessonPlan}
285+
/>
286+
</FormControl>
282287

283288
<FormControl>
284289
<FormLabel htmlFor="notes" sx={{ display: 'flex' }}>

src/components/FileUpload.tsx

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
import {
2-
Box,
3-
FormControl,
4-
Image,
5-
Input,
6-
Text,
7-
useToast,
8-
} from '@chakra-ui/react';
1+
import { Box, Image, Input, Text, useToast } from '@chakra-ui/react';
92
import { LessonPlanFile } from '@utils/types';
103
import { useRef, useState } from 'react';
114

125
interface FileUploadProps {
136
lessonPlan: File | null;
147
setLessonPlan: (file: File | null) => void;
158
existingFile?: LessonPlanFile | null;
16-
label?: string;
9+
isDisabled?: boolean;
1710
}
1811

1912
export const FileUpload: React.FC<FileUploadProps> = ({
2013
lessonPlan,
2114
setLessonPlan,
2215
existingFile,
23-
label = 'Lesson Plan',
16+
isDisabled,
2417
}) => {
2518
const [isDragging, setIsDragging] = useState(false);
2619
const inputRef = useRef<HTMLInputElement>(null);
@@ -34,51 +27,63 @@ export const FileUpload: React.FC<FileUploadProps> = ({
3427
title: 'Invalid File Type',
3528
description: 'Please upload a valid PDF file.',
3629
status: 'error',
37-
duration: 4000,
30+
duration: 5000,
3831
isClosable: true,
3932
});
4033
}
4134
};
4235

4336
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
44-
if (e.target.files && e.target.files.length > 0) {
45-
const file = e.target.files[0];
37+
if (isDisabled) return;
38+
39+
const file = e.target.files?.[0];
40+
if (file) {
4641
validateAndSetFile(file);
4742
}
4843
};
4944

5045
const handleDragOver = (e: React.DragEvent) => {
46+
if (isDisabled) return;
5147
e.preventDefault();
5248
setIsDragging(true);
5349
};
5450

5551
const handleDragLeave = () => {
52+
if (isDisabled) return;
5653
setIsDragging(false);
5754
};
5855

5956
const handleDrop = (e: React.DragEvent) => {
57+
if (isDisabled) return;
6058
e.preventDefault();
6159
setIsDragging(false);
62-
if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
63-
const file = e.dataTransfer.files[0];
60+
61+
const file = e.dataTransfer.files?.[0];
62+
if (file) {
6463
validateAndSetFile(file);
6564
}
6665
};
6766

6867
return (
69-
<FormControl>
70-
<Text textStyle="h4" mb={2}>
71-
{label}
72-
</Text>
68+
<>
7369
<Box
7470
as="label"
7571
htmlFor="fileUpload"
7672
border="1px dashed"
77-
borderColor={isDragging ? 'primaryBlue.300' : 'outline'}
73+
borderColor={
74+
isDisabled
75+
? 'neutralGray.300'
76+
: isDragging
77+
? 'primaryBlue.300'
78+
: 'outline'
79+
}
80+
bg={isDisabled ? 'neutralGray.50' : 'transparent'}
81+
opacity={isDisabled ? 0.6 : 1}
82+
pointerEvents={isDisabled ? 'none' : 'auto'}
7883
borderRadius="10px"
7984
p={5}
8085
textAlign="center"
81-
cursor="pointer"
86+
cursor={isDisabled ? 'not-allowed' : 'pointer'}
8287
display="flex"
8388
flexDirection="column"
8489
alignItems="center"
@@ -105,7 +110,8 @@ export const FileUpload: React.FC<FileUploadProps> = ({
105110
onChange={handleFileChange}
106111
accept="application/pdf"
107112
display="none"
113+
disabled={isDisabled}
108114
/>
109-
</FormControl>
115+
</>
110116
);
111117
};

src/components/LessonPlanView.tsx

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { LessonPlanFile } from '@utils/types';
1313
import { uploadFile } from '@utils/uploadFile';
1414
import { useEffect, useRef, useState } from 'react';
1515
import { FiFileText } from 'react-icons/fi';
16+
import { FileUpload } from './FileUpload';
1617

1718
const formatFileSize = (sizeInBytes: number) => {
1819
if (sizeInBytes === 0) return '0 B';
@@ -127,21 +128,6 @@ const NoLessonPlanViewingDisplay = ({
127128
);
128129
};
129130

130-
const NoLessonPlanDeclaredDisplay = () => {
131-
const theme = useTheme();
132-
133-
return (
134-
<Flex width="100%">
135-
<Text
136-
fontSize={theme.textStyles.body.fontSize}
137-
color={theme.colors.neutralGray[500]}
138-
>
139-
Upload PDF component
140-
</Text>
141-
</Flex>
142-
);
143-
};
144-
145131
const LessonPlanView = ({
146132
lessonPlan,
147133
absentTeacherFirstName,
@@ -168,8 +154,7 @@ const LessonPlanView = ({
168154
fileInputRef.current?.click();
169155
};
170156

171-
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
172-
const file = e.target.files?.[0];
157+
const handleFileUpload = async (file: File) => {
173158
if (!file || file.type !== 'application/pdf') return;
174159

175160
setIsUploading(true);
@@ -248,11 +233,21 @@ const LessonPlanView = ({
248233
ref={fileInputRef}
249234
display="none"
250235
accept="application/pdf"
251-
onChange={handleFileChange}
236+
onChange={(e) => {
237+
const file = e.target.files?.[0];
238+
if (file) {
239+
handleFileUpload(file);
240+
}
241+
}}
252242
/>
253243
</>
254244
) : isUserAbsentTeacher && !isAdminMode ? (
255-
<NoLessonPlanDeclaredDisplay />
245+
<FileUpload
246+
lessonPlan={null}
247+
setLessonPlan={handleFileUpload}
248+
existingFile={null}
249+
isDisabled={isUploading}
250+
/>
256251
) : (
257252
<NoLessonPlanViewingDisplay
258253
absentTeacherFirstName={absentTeacherFirstName}

0 commit comments

Comments
 (0)