-
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 branch 'develop' of https://github.com/prgrms-web-devcourse-fin…
…al-project/WEB1_1_DDang_FE into 150-feature/enhance-web-socket
- Loading branch information
Showing
67 changed files
with
3,121 additions
and
1,401 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
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,49 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
import { DayOfWeek } from '~types/common' | ||
|
||
export type FetchFamilyDDangResponse = Pick< | ||
CommonAPIResponse, | ||
'familyId' | 'dogs' | 'totalWalkCount' | 'totalDistanceInKilometers' | 'totalCalorie' | ||
> & { | ||
members: (CommonAPIResponse['members'][number] & { | ||
walkScheduleInfoList: { | ||
walkScheduleId: number | ||
dayOfWeek: DayOfWeek | ||
walkTime: string | ||
}[] | ||
totalWalkCount: number // 추가된 속성 | ||
})[] | ||
} | ||
|
||
export const fetchFamilyDDang = async (): Promise<APIResponse<FetchFamilyDDangResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.get<APIResponse<FetchFamilyDDangResponse>>(`/family`) | ||
console.log('패밀리댕 정보 바인딩 : ', 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('다시 시도해주세요') | ||
} | ||
} |
Oops, something went wrong.