This repository was archived by the owner on Jun 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateClassModal.tsx
More file actions
176 lines (164 loc) · 4.37 KB
/
CreateClassModal.tsx
File metadata and controls
176 lines (164 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use client';
import { api } from '@/api/api';
import { authAtom } from '@/store/auth';
import { classroomsAtom } from '@/store/classrooms';
import type { IClassroom } from '@/types/IClassroomCard';
import {
Button,
FormControl,
FormLabel,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Select,
Textarea,
VStack,
useToast
} from '@chakra-ui/react';
import { useAtom } from 'jotai';
import { useEffect, useState } from 'react';
interface CreateClassModalProps {
isOpen: boolean;
onClose: () => void;
}
export default function CreateClassModal({ isOpen, onClose }: Readonly<CreateClassModalProps>) {
const [isLoading, setIsLoading] = useState(false);
const [formData, setFormData] = useState({
name: '',
description: '',
thumbnailId: 1
});
const [, setClassrooms] = useAtom(classroomsAtom);
const toast = useToast();
useEffect(() => {
if (isOpen) {
setFormData({
name: '',
description: '',
thumbnailId: 1
});
}
}, [isOpen]);
const handleSubmit = async () => {
if (!formData.name.trim() || !formData.description.trim()) {
toast({
title: 'Error',
description: 'Por favor completa todos los campos requeridos',
status: 'error',
position: 'top-right',
duration: 3000,
isClosable: true
});
return;
}
setIsLoading(true);
try {
await api.classroom.create(formData);
try {
const data = await api.classroom.getAll();
setClassrooms(data);
} catch (error) {
console.error('Failed to fetch classrooms:', error);
}
toast({
title: 'Clase creada',
description: 'La clase se ha creado exitosamente',
status: 'success',
position: 'top-right',
duration: 3000,
isClosable: true
});
onClose();
} catch {
toast({
title: 'Error',
description: 'Ocurrió un error al crear la clase',
status: 'error',
position: 'top-right',
duration: 3000,
isClosable: true
});
} finally {
setIsLoading(false);
}
};
return (
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay backdropFilter='blur(4px)' />
<ModalContent bg='brand.dark.900' border='1px solid' borderColor='brand.dark.800'>
<ModalHeader>Crear Nueva Clase</ModalHeader>
<ModalCloseButton />
<ModalBody>
<VStack spacing={4}>
<FormControl isRequired>
<FormLabel>Nombre</FormLabel>
<Input
placeholder='Ej: Programación Web'
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
bg='brand.dark.800'
border='1px solid'
borderColor='brand.dark.700'
_hover={{ borderColor: 'brand.primary.500' }}
_focus={{
borderColor: 'brand.primary.500',
boxShadow: '0 0 0 1px var(--chakra-colors-brand-primary-500)'
}}
/>
</FormControl>
<FormControl isRequired>
<FormLabel>Descripción</FormLabel>
<Input
placeholder='Describe tu clase...'
value={formData.description}
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
bg='brand.dark.800'
border='1px solid'
borderColor='brand.dark.700'
_hover={{ borderColor: 'brand.primary.500' }}
_focus={{
borderColor: 'brand.primary.500',
boxShadow: '0 0 0 1px var(--chakra-colors-brand-primary-500)'
}}
/>
</FormControl>
<FormControl>
<FormLabel>Imagen de portada</FormLabel>
<Select
value={formData.thumbnailId}
onChange={(e) => setFormData({ ...formData, thumbnailId: Number(e.target.value) })}
bg='brand.dark.800'
border='1px solid'
borderColor='brand.dark.700'
_hover={{ borderColor: 'brand.primary.500' }}
_focus={{
borderColor: 'brand.primary.500',
boxShadow: '0 0 0 1px var(--chakra-colors-brand-primary-500)'
}}
>
{Array.from({ length: 9 }, (_, i) => (
<option key={i + 1} value={i + 1}>
Imagen {i + 1}
</option>
))}
</Select>
</FormControl>
</VStack>
</ModalBody>
<ModalFooter gap={2}>
<Button variant='ghost' onClick={onClose}>
Cancelar
</Button>
<Button colorScheme='blue' onClick={handleSubmit} isLoading={isLoading} loadingText='Creando...'>
Crear Clase
</Button>
</ModalFooter>
</ModalContent>
</Modal>
);
}