Skip to content

Commit 94fb2f0

Browse files
authored
Merge pull request #128 from prgrms-web-devcourse-final-project/127-feature/homepage-data-binding
[Feature] #127 홈페이지 데이터 바인딩
2 parents 7390324 + cff5501 commit 94fb2f0

File tree

22 files changed

+173
-91
lines changed

22 files changed

+173
-91
lines changed

src/apis/main/fetchHomePageData.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { AxiosError } from 'axios'
2+
import { APIResponse, CommonAPIResponse, ErrorResponse } from '~types/api'
3+
import { axiosInstance } from '~apis/axiosInstance'
4+
5+
export type FetchHomePageDataResponse = Pick<
6+
CommonAPIResponse,
7+
'memberId' | 'familyRole' | 'dogName' | 'timeDuration' | 'totalDistanceMeter' | 'totalCalorie' | 'memberProfileImgUrl'
8+
>
9+
10+
export const fetchHomePageData = async (): Promise<APIResponse<FetchHomePageDataResponse>> => {
11+
try {
12+
const { data } = await axiosInstance.get<APIResponse<FetchHomePageDataResponse>>(`/main`)
13+
return data
14+
} catch (error) {
15+
if (error instanceof AxiosError) {
16+
const { response } = error as AxiosError<ErrorResponse>
17+
18+
if (response) {
19+
const { code, message } = response.data
20+
switch (code) {
21+
case 400:
22+
throw new Error(message || '잘못된 요청입니다.')
23+
case 401:
24+
throw new Error(message || '인증에 실패했습니다.')
25+
case 500:
26+
throw new Error(message || '서버 오류가 발생했습니다.')
27+
default:
28+
throw new Error(message || '알 수 없는 오류가 발생했습니다.')
29+
}
30+
} else {
31+
// 요청 자체가 실패한 경우
32+
throw new Error('네트워크 연결을 확인해주세요')
33+
}
34+
}
35+
36+
console.error('예상치 못한 에러:', error)
37+
throw new Error('다시 시도해주세요')
38+
}
39+
}

src/apis/main/useHomePageData.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { useSuspenseQuery, UseSuspenseQueryResult } from '@tanstack/react-query'
2+
import { AxiosError } from 'axios'
3+
import { queryKey } from '~constants/queryKey'
4+
import { fetchHomePageData, FetchHomePageDataResponse } from './fetchHomePageData'
5+
6+
export const useHomePageData = (): UseSuspenseQueryResult<FetchHomePageDataResponse, AxiosError> => {
7+
return useSuspenseQuery<FetchHomePageDataResponse, AxiosError>({
8+
queryKey: queryKey.home(),
9+
queryFn: () => fetchHomePageData().then(data => data.data),
10+
staleTime: 5 * 60 * 1000, // 5분
11+
})
12+
}

src/assets/buttons/back_btn.svg

Lines changed: 3 additions & 0 deletions
Loading

src/assets/buttons/close_btn.svg

Lines changed: 4 additions & 0 deletions
Loading

src/assets/dog_hand.svg

Lines changed: 23 additions & 0 deletions
Loading

src/assets/icons/bell_icon.svg

Lines changed: 3 additions & 0 deletions
Loading

src/assets/icons/clock_icon.svg

Lines changed: 3 additions & 0 deletions
Loading

src/assets/icons/gps_icon.svg

Lines changed: 4 additions & 0 deletions
Loading

src/components/Button/CloseBtn.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { styled } from 'styled-components'
2+
import Close from '~assets/buttons/close_btn.svg?react'
3+
4+
export const CloseBtn = styled(Close)`
5+
cursor: pointer;
6+
`

src/components/Button/CloseButton/index.tsx

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/Button/CloseButton/styles.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/Button/PrevBtn.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { styled } from 'styled-components'
2+
import BackBtn from '~assets/buttons/back_btn.svg?react'
3+
4+
export const PrevBtn = styled(BackBtn)`
5+
cursor: pointer;
6+
`

src/components/Button/PrevButton/index.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/components/Button/PrevButton/styles.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/Header/styles.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import styled from 'styled-components'
2-
import CloseButton from '~components/Button/CloseButton'
3-
import PrevButton from '~components/Button/PrevButton'
2+
import { CloseBtn } from '~components/Button/CloseBtn'
3+
import { PrevBtn } from '~components/Button/PrevBtn'
44
import { Typo17 } from '~components/Typo'
55
import { HEADER_HEIGHT, HEADER_HEIGHT_LG, HEADER_Z_INDEX } from '~constants/layout'
66
import { HeaderType } from '~types/headerType'
@@ -24,10 +24,10 @@ export const Header = styled.header<HeaderProps>`
2424
z-index: ${HEADER_Z_INDEX};
2525
`
2626

27-
export const HeaderPrevBtn = styled(PrevButton)`
27+
export const HeaderPrevBtn = styled(PrevBtn)`
2828
margin-right: 8px;
2929
`
30-
export const HeaderCloseBtn = styled(CloseButton)`
30+
export const HeaderCloseBtn = styled(CloseBtn)`
3131
position: absolute;
3232
right: 1.25rem;
3333
`

src/constants/queryKey.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export const queryKey = {
44
friendList: () => ['friendList'],
55
},
66
profile: (memberId: number) => ['profile', memberId],
7+
home: () => ['homePageData'],
78
}

src/modals/NotificationModal/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import PrevButton from '~components/Button/PrevButton'
2-
import * as S from './styles'
3-
import { Typo17 } from '~components/Typo'
1+
import Header from '~components/Header'
42
import NotificationList from '~components/NotificationList'
53
import { useModalStore } from '~stores/modalStore'
4+
import * as S from './styles'
65

76
export default function NotificationModal() {
87
const { popModal } = useModalStore()
98
return (
109
<S.NotificationModal>
11-
<PrevButton onClick={popModal} />
12-
<S.Header>
13-
<Typo17 $weight='700'>알림</Typo17>
14-
</S.Header>
10+
<Header prevBtn onClickPrev={popModal} type='sm' title='알림' />
1511
<NotificationList />
1612
</S.NotificationModal>
1713
)

src/modals/NotificationModal/styles.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,3 @@ export const NotificationModal = styled.div`
77
overflow: auto;
88
padding-top: ${HEADER_HEIGHT}px;
99
`
10-
export const Header = styled.header`
11-
position: fixed;
12-
background-color: ${({ theme }) => theme.colors.grayscale.gc_4};
13-
z-index: 3;
14-
width: 100%;
15-
top: 0;
16-
height: ${HEADER_HEIGHT}px;
17-
display: flex;
18-
justify-content: center;
19-
align-items: center;
20-
`

0 commit comments

Comments
 (0)