|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { api } from '@/api/api'; |
| 4 | +import { CDN_URL } from '@/constants/constants'; |
| 5 | +import { authAtom, loadUser } from '@/store/auth'; |
| 6 | +import { |
| 7 | + Avatar, |
| 8 | + Button, |
| 9 | + FormControl, |
| 10 | + FormLabel, |
| 11 | + Input, |
| 12 | + Modal, |
| 13 | + ModalBody, |
| 14 | + ModalCloseButton, |
| 15 | + ModalContent, |
| 16 | + ModalFooter, |
| 17 | + ModalHeader, |
| 18 | + ModalOverlay, |
| 19 | + VStack, |
| 20 | + useToast |
| 21 | +} from '@chakra-ui/react'; |
| 22 | +import { useAtom } from 'jotai'; |
| 23 | +import { useCallback, useEffect, useState } from 'react'; |
| 24 | +import { useDropzone } from 'react-dropzone'; |
| 25 | + |
| 26 | +interface EditAccountModalProps { |
| 27 | + isOpen: boolean; |
| 28 | + onClose: () => void; |
| 29 | +} |
| 30 | + |
| 31 | +export default function EditAccountModal({ isOpen, onClose }: Readonly<EditAccountModalProps>) { |
| 32 | + const [isLoading, setIsLoading] = useState(false); |
| 33 | + const [avatarFile, setAvatarFile] = useState<File | null>(null); |
| 34 | + const [avatarPreview, setAvatarPreview] = useState<string | null>(null); |
| 35 | + const toast = useToast(); |
| 36 | + const [auth, setAuth] = useAtom(authAtom); |
| 37 | + |
| 38 | + const onDrop = useCallback((acceptedFiles: File[]) => { |
| 39 | + const file = acceptedFiles[0]; |
| 40 | + if (file) { |
| 41 | + setAvatarFile(file); |
| 42 | + const reader = new FileReader(); |
| 43 | + reader.onloadend = () => { |
| 44 | + setAvatarPreview(reader.result as string); |
| 45 | + }; |
| 46 | + reader.readAsDataURL(file); |
| 47 | + } |
| 48 | + }, []); |
| 49 | + |
| 50 | + const { getRootProps, getInputProps } = useDropzone({ |
| 51 | + onDrop, |
| 52 | + accept: { |
| 53 | + 'image/*': ['.png', '.jpg', '.jpeg', '.gif'] |
| 54 | + }, |
| 55 | + maxFiles: 1, |
| 56 | + maxSize: 10 * 1024 * 1024 |
| 57 | + }); |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + if (!isOpen) { |
| 61 | + setAvatarFile(null); |
| 62 | + setAvatarPreview(null); |
| 63 | + } |
| 64 | + }, [isOpen]); |
| 65 | + |
| 66 | + const handleSubmit = async () => { |
| 67 | + if (!avatarFile) { |
| 68 | + toast({ |
| 69 | + title: 'Error', |
| 70 | + description: 'Por favor selecciona una imagen', |
| 71 | + status: 'error', |
| 72 | + position: 'top-right', |
| 73 | + duration: 3000, |
| 74 | + isClosable: true |
| 75 | + }); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + setIsLoading(true); |
| 80 | + try { |
| 81 | + const res = await api.user.avatar(); |
| 82 | + if (!res) throw new Error('Failed to get signed URL'); |
| 83 | + |
| 84 | + const uploadRes = await fetch(res.url, { |
| 85 | + method: 'PUT', |
| 86 | + body: avatarFile, |
| 87 | + headers: { |
| 88 | + 'Content-Type': avatarFile.type |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + if (!uploadRes.ok) throw new Error('Failed to upload file to S3'); |
| 93 | + |
| 94 | + if (auth.token) { |
| 95 | + const updatedUser = await loadUser(auth.token); |
| 96 | + setAuth((prev) => ({ ...prev, user: updatedUser })); |
| 97 | + } |
| 98 | + |
| 99 | + toast({ |
| 100 | + title: 'Éxito', |
| 101 | + description: 'Tu avatar ha sido actualizado correctamente', |
| 102 | + status: 'success', |
| 103 | + position: 'top-right', |
| 104 | + duration: 3000, |
| 105 | + isClosable: true |
| 106 | + }); |
| 107 | + onClose(); |
| 108 | + } catch { |
| 109 | + toast({ |
| 110 | + title: 'Error', |
| 111 | + description: 'Ha ocurrido un error al actualizar tu avatar', |
| 112 | + status: 'error', |
| 113 | + position: 'top-right', |
| 114 | + duration: 3000, |
| 115 | + isClosable: true |
| 116 | + }); |
| 117 | + } finally { |
| 118 | + setIsLoading(false); |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + return ( |
| 123 | + <Modal isOpen={isOpen} onClose={onClose}> |
| 124 | + <ModalOverlay backdropFilter='blur(4px)' /> |
| 125 | + <ModalContent bg='brand.dark.900' border='1px solid' borderColor='brand.dark.800'> |
| 126 | + <ModalHeader>Editar Cuenta</ModalHeader> |
| 127 | + <ModalCloseButton /> |
| 128 | + <ModalBody> |
| 129 | + <VStack spacing={6}> |
| 130 | + <FormControl> |
| 131 | + <FormLabel>Avatar</FormLabel> |
| 132 | + <VStack spacing={4}> |
| 133 | + <Avatar |
| 134 | + size='2xl' |
| 135 | + src={ |
| 136 | + avatarPreview || |
| 137 | + (auth.user?.avatar |
| 138 | + ? `${CDN_URL}/avatars/${auth.user?.id}/${auth.user?.avatar}.png` |
| 139 | + : '') |
| 140 | + } |
| 141 | + cursor='pointer' |
| 142 | + name={auth.user?.username} |
| 143 | + {...(getRootProps() as any)} |
| 144 | + /> |
| 145 | + <Input {...(getInputProps() as any)} /> |
| 146 | + <Button size='sm' variant='outline' {...getRootProps()}> |
| 147 | + Cambiar Avatar |
| 148 | + </Button> |
| 149 | + </VStack> |
| 150 | + </FormControl> |
| 151 | + </VStack> |
| 152 | + </ModalBody> |
| 153 | + |
| 154 | + <ModalFooter gap={2}> |
| 155 | + <Button variant='ghost' onClick={onClose}> |
| 156 | + Cancelar |
| 157 | + </Button> |
| 158 | + <Button colorScheme='blue' onClick={handleSubmit} isLoading={isLoading} loadingText='Guardando...'> |
| 159 | + Guardar Cambios |
| 160 | + </Button> |
| 161 | + </ModalFooter> |
| 162 | + </ModalContent> |
| 163 | + </Modal> |
| 164 | + ); |
| 165 | +} |
0 commit comments