-
Notifications
You must be signed in to change notification settings - Fork 0
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 #152 from prgrms-web-devcourse-final-project/113-f…
…eature/register-dog-profile-api [Feature] 반려견 정보 수정, 패밀리코드 초대 기능
- Loading branch information
Showing
42 changed files
with
1,932 additions
and
987 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
import { DogProfileType } from '~types/dogProfile' | ||
|
||
export type PatchDogProfileRequest = FormData | ||
|
||
export type PatchDogProfileResponse = DogProfileType | ||
|
||
/** | ||
* 반려견 프로필 정보를 수정합니다. | ||
*/ | ||
export const patchDogProfile = async ( | ||
id: number, | ||
req: PatchDogProfileRequest | ||
): Promise<APIResponse<PatchDogProfileResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.patch<APIResponse<PatchDogProfileResponse>>(`/dogs/${id}`, req, { | ||
headers: { | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
}) | ||
return data | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
const { response } = error as AxiosError<ErrorResponse> | ||
|
||
if (response) { | ||
const { code, message } = response.data | ||
switch (code) { | ||
case 400: | ||
throw new Error(message || '잘못된 요청입니다.') | ||
case 401: | ||
throw new Error(message || '인증에 실패했습니다.') | ||
case 500: | ||
throw new Error(message || '서버 오류가 발생했습니다.') | ||
default: | ||
throw new Error(message || '알 수 없는 오류가 발생했습니다.') | ||
} | ||
} | ||
|
||
throw new Error('네트워크 연결을 확인해주세요') | ||
} | ||
|
||
console.error('예상치 못한 에러:', error) | ||
throw new Error('다시 시도해주세요') | ||
} | ||
} |
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,62 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { createDogProfile } from '~apis/dog/createDogProfile' | ||
import { useNavigate } from 'react-router-dom' | ||
import { useModalStore } from '~stores/modalStore' | ||
import { useToastStore } from '~stores/toastStore' | ||
import { useDogProfileStore } from '~stores/dogProfileStore' | ||
import { useSuspenseQuery } from '@tanstack/react-query' | ||
import { fetchDogProfile } from '~apis/dog/fetchDogProfile' | ||
import { queryKey } from '~constants/queryKey' | ||
import { patchDogProfile, PatchDogProfileRequest } from '~apis/dog/patchDogProfile' | ||
import ConfirmModal from '~modals/ConfirmModal' | ||
|
||
export function useCreateDogProfile() { | ||
const { setDogProfile } = useDogProfileStore() | ||
const { pushModal, clearModal } = useModalStore() | ||
const { showToast } = useToastStore() | ||
const navigate = useNavigate() | ||
|
||
const completeRegistration = () => { | ||
navigate('/') | ||
clearModal() | ||
} | ||
|
||
return useMutation({ | ||
mutationFn: (formData: FormData) => createDogProfile(formData), | ||
onSuccess: response => { | ||
setDogProfile({ ...response.data }) | ||
pushModal(<ConfirmModal content='반려견 등록이 완료되었습니다' onClick={completeRegistration} />) | ||
}, | ||
onError: (error: Error) => { | ||
showToast(error.message) | ||
}, | ||
}) | ||
} | ||
|
||
export function useFetchDogProfile(id: number) { | ||
return useSuspenseQuery({ | ||
queryKey: queryKey.dog.profile(id), | ||
queryFn: () => fetchDogProfile({ id }).then(res => res.data), | ||
}) | ||
} | ||
|
||
export function usePatchDogProfile(id: number) { | ||
const queryClient = useQueryClient() | ||
const { clearModal, pushModal } = useModalStore() | ||
const { showToast } = useToastStore() | ||
|
||
const completeRegistration = () => { | ||
queryClient.invalidateQueries({ queryKey: queryKey.dog.profile(id) }) | ||
clearModal() | ||
} | ||
|
||
return useMutation({ | ||
mutationFn: (data: PatchDogProfileRequest) => patchDogProfile(id, data), | ||
onSuccess: () => { | ||
pushModal(<ConfirmModal content='반려견 정보가 수정되었습니다' onClick={completeRegistration} />) | ||
}, | ||
onError: (error: Error) => { | ||
showToast(error.message) | ||
}, | ||
}) | ||
} |
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,31 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
|
||
export type InviteCodeResponse = Pick<CommonAPIResponse, 'familyId' | 'inviteCode' | 'expiresInSeconds'> | ||
|
||
export const fetchInviteCode = async (): Promise<APIResponse<InviteCodeResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.get<APIResponse<InviteCodeResponse>>('/family/invite-code') | ||
return data | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
const { response } = error as AxiosError<APIResponse<ErrorResponse>> | ||
if (response) { | ||
const { code, message } = response.data | ||
switch (code) { | ||
case 400: | ||
throw new Error(message || '잘못된 요청입니다') | ||
case 401: | ||
throw new Error(message || '인증에 실패했습니다') | ||
case 500: | ||
throw new Error(message || '서버 오류가 발생했습니다') | ||
default: | ||
throw new Error(message || '알 수 없는 오류가 발생했습니다') | ||
} | ||
} | ||
throw new Error('네트워크 연결을 확인해주세요') | ||
} | ||
throw new Error('알 수 없는 오류가 발생했습니다') | ||
} | ||
} |
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,36 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
import { DogProfileType } from '~types/dogProfile' | ||
|
||
type FetchFamilyDogsRequest = Pick<CommonAPIResponse, 'inviteCode'> | ||
|
||
export const fetchFamilyDogs = async (request: FetchFamilyDogsRequest): Promise<APIResponse<DogProfileType[]>> => { | ||
try { | ||
const { data } = await axiosInstance.post<APIResponse<DogProfileType[]>>('/family/dogs', request) | ||
return data | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
const { response } = error as AxiosError<ErrorResponse> | ||
|
||
if (response) { | ||
const { code, message } = response.data | ||
switch (code) { | ||
case 400: | ||
throw new Error(message || '잘못된 요청입니다.') | ||
case 401: | ||
throw new Error(message || '유효하지 않은 코드입니다.') | ||
case 500: | ||
throw new Error(message || '서버 오류가 발생했습니다.') | ||
default: | ||
throw new Error(message || '알 수 없는 오류가 발생했습니다.') | ||
} | ||
} | ||
|
||
throw new Error('네트워크 연결을 확인해주세요') | ||
} | ||
|
||
console.error('예상치 못한 에러:', error) | ||
throw new Error('다시 시도해주세요') | ||
} | ||
} |
Oops, something went wrong.