Skip to content

Commit d5f116c

Browse files
Disable when uploading
1 parent 8c28b4b commit d5f116c

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

src/components/FileUpload.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +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;
9+
isDisabled: boolean;
1610
}
1711

1812
export const FileUpload: React.FC<FileUploadProps> = ({
1913
lessonPlan,
2014
setLessonPlan,
2115
existingFile,
16+
isDisabled,
2217
}) => {
2318
const [isDragging, setIsDragging] = useState(false);
2419
const inputRef = useRef<HTMLInputElement>(null);
@@ -32,33 +27,39 @@ export const FileUpload: React.FC<FileUploadProps> = ({
3227
title: 'Invalid File Type',
3328
description: 'Please upload a valid PDF file.',
3429
status: 'error',
35-
duration: 4000,
30+
duration: 5000,
3631
isClosable: true,
3732
});
3833
}
3934
};
4035

4136
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
42-
if (e.target.files && e.target.files.length > 0) {
43-
const file = e.target.files[0];
37+
if (isDisabled) return;
38+
39+
const file = e.target.files?.[0];
40+
if (file) {
4441
validateAndSetFile(file);
4542
}
4643
};
4744

4845
const handleDragOver = (e: React.DragEvent) => {
46+
if (isDisabled) return;
4947
e.preventDefault();
5048
setIsDragging(true);
5149
};
5250

5351
const handleDragLeave = () => {
52+
if (isDisabled) return;
5453
setIsDragging(false);
5554
};
5655

5756
const handleDrop = (e: React.DragEvent) => {
57+
if (isDisabled) return;
5858
e.preventDefault();
5959
setIsDragging(false);
60-
if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
61-
const file = e.dataTransfer.files[0];
60+
61+
const file = e.dataTransfer.files?.[0];
62+
if (file) {
6263
validateAndSetFile(file);
6364
}
6465
};
@@ -69,11 +70,20 @@ export const FileUpload: React.FC<FileUploadProps> = ({
6970
as="label"
7071
htmlFor="fileUpload"
7172
border="1px dashed"
72-
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'}
7383
borderRadius="10px"
7484
p={5}
7585
textAlign="center"
76-
cursor="pointer"
86+
cursor={isDisabled ? 'not-allowed' : 'pointer'}
7787
display="flex"
7888
flexDirection="column"
7989
alignItems="center"
@@ -100,6 +110,7 @@ export const FileUpload: React.FC<FileUploadProps> = ({
100110
onChange={handleFileChange}
101111
accept="application/pdf"
102112
display="none"
113+
disabled={isDisabled}
103114
/>
104115
</>
105116
);

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)