-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from ppochaco/fix/166-form-input
Week10/issue#166 react hook form을 사용해서 form 관리하기
- Loading branch information
Showing
36 changed files
with
1,133 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Avatar, HStack, StackProps, Text } from '@chakra-ui/react' | ||
|
||
import { colors } from '@/styles/colors' | ||
|
||
interface AvatarLabelProps extends StackProps { | ||
avatarSrc?: string | ||
label: string | ||
} | ||
|
||
export const AvatarLabel = ({ avatarSrc, label }: AvatarLabelProps) => { | ||
return ( | ||
<HStack gap={1.5}> | ||
<Avatar | ||
width={7} | ||
height={7} | ||
src={avatarSrc} | ||
border={`0.8px solid ${colors.black[300]}`} | ||
/> | ||
<Text>{label}</Text> | ||
</HStack> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { ChangeEvent, useState } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
import { BiError } from 'react-icons/bi' | ||
|
||
import { Box, Input, useDisclosure } from '@chakra-ui/react' | ||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import { useMutation } from '@tanstack/react-query' | ||
|
||
import { queryClient } from '@/api/instance' | ||
import { | ||
ModifyGroupImgRequestBody, | ||
modifyGroupImg, | ||
} from '@/api/services/group/group.api' | ||
import { Form, FormControl, FormField, FormItem } from '@/components/Form' | ||
import { AlertModal } from '@/components/Modal/AlertModal' | ||
import { ModifyGroupImageFields, ModifyGroupImageSchema } from '@/schema/group' | ||
import { Group, GroupRole } from '@/types' | ||
|
||
type ImgModifyProps = { | ||
gprofile: Group | ||
role: GroupRole | ||
} | ||
|
||
export default function ImgModify({ role, gprofile }: ImgModifyProps) { | ||
const form = useForm<ModifyGroupImageFields>({ | ||
resolver: zodResolver(ModifyGroupImageSchema), | ||
mode: 'onSubmit', | ||
defaultValues: { | ||
groupId: gprofile.groupId, | ||
image: new File([], ''), | ||
}, | ||
}) | ||
|
||
const [errorMessage, setErrorMessage] = useState('') | ||
const errorModal = useDisclosure() | ||
|
||
const { mutate: uploadImage } = useMutation({ | ||
mutationFn: (data: ModifyGroupImgRequestBody) => modifyGroupImg(data), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['group', gprofile.groupId] }) | ||
}, | ||
}) | ||
|
||
const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
const file = e.target.files ? e.target.files[0] : null | ||
|
||
if (file) { | ||
form.setValue('image', file) | ||
form.handleSubmit( | ||
() => { | ||
uploadImage(form.getValues()) | ||
}, | ||
(errors) => { | ||
const errorMessages = | ||
Object.values(errors).flatMap((error) => error.message)[0] || '' | ||
|
||
setErrorMessage(errorMessages) | ||
errorModal.onOpen() | ||
} | ||
)() | ||
} | ||
} | ||
|
||
if (role === 'MEMBER') { | ||
return ( | ||
<Box | ||
width="70px" | ||
height="70px" | ||
sx={{ border: '0.8px solid', borderColor: 'black.300' }} | ||
backgroundImage={`url('${gprofile.groupImageUrl}')`} | ||
backgroundSize="cover" | ||
backgroundPosition="center" | ||
borderRadius="100%" | ||
/> | ||
) | ||
} | ||
|
||
return ( | ||
<Box | ||
width="70px" | ||
height="70px" | ||
sx={{ border: '0.8px solid', borderColor: 'black.300' }} | ||
_hover={{ opacity: 0.5 }} | ||
cursor="pointer" | ||
onClick={() => document.getElementById('fileInput')?.click()} | ||
backgroundImage={`url('${gprofile.groupImageUrl}')`} | ||
backgroundSize="cover" | ||
backgroundPosition="center" | ||
borderRadius="100%" | ||
> | ||
<Form {...form}> | ||
<form> | ||
<FormField | ||
control={form.control} | ||
name="image" | ||
render={() => ( | ||
<FormItem> | ||
<FormControl> | ||
<Input | ||
type="file" | ||
id="fileInput" | ||
accept=".jpg, .jpeg, .png" | ||
display="none" | ||
multiple={false} | ||
onChange={handleFileChange} | ||
/> | ||
</FormControl> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
<AlertModal | ||
isOpen={errorModal.isOpen} | ||
onClose={errorModal.onClose} | ||
icon={<BiError />} | ||
title={errorMessage} | ||
description="" | ||
/> | ||
</Box> | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
src/pages/GroupPage/GroupProfile/ModifySuccessModal/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { BiCheckCircle } from 'react-icons/bi' | ||
|
||
import { AlertModal } from '@/components/Modal/AlertModal' | ||
import { Modal } from '@/types' | ||
|
||
interface ModifySuccessModalProps { | ||
successModal: Modal | ||
} | ||
|
||
export const ModifySuccessModal = ({ | ||
successModal, | ||
}: ModifySuccessModalProps) => { | ||
return ( | ||
<AlertModal | ||
isOpen={successModal.isOpen} | ||
onClose={() => { | ||
successModal.onClose() | ||
}} | ||
icon={<BiCheckCircle />} | ||
title="그룹 이름과 소개를 수정하였습니다" | ||
description="" | ||
/> | ||
) | ||
} |
Oops, something went wrong.