Skip to content

Commit

Permalink
✨ fetch 함수로 ISR 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
khj0426 committed Sep 29, 2023
1 parent 8dce411 commit 4b05f81
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
16 changes: 13 additions & 3 deletions src/app/api/users/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { NextResponse } from 'next/server'
import { apiServer } from '@/lib/axiosSever'
import { Environment } from '@/config/environments'

export async function GET(_request: Request) {
try {
const { data } = await apiServer.get(`/users/get-users`)
const allUserRes = await fetch(`${Environment.baseUrl()}/users/get-users`, {
next: {
revalidate: 5,
},
})

return new Response(JSON.stringify(data))
if (allUserRes.ok) {
const allUser = await allUserRes.json()

return new Response(JSON.stringify(allUser))
}

return new Response(JSON.stringify(allUserRes))
} catch (error: any) {
return NextResponse.json(
{ error: error.response.data.message },
Expand Down
3 changes: 2 additions & 1 deletion src/components/molcules/EditNamesForm/EditNamesform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ const EditNamesform = ({ fullName }: NameProps) => {
setCurrentName(data.fullName)
notify('success', '닉네임이 수정되었습니다.')
setValue('fullName', '')
location.reload()
} catch (error) {
notify('error', '닉네임 수정에 실패했습니다.')
} finally {
location.reload()
}
})}
style={{ display: 'flex', justifyContent: 'space-between' }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import UserList from './UserList'

export default async function HydratedUserList() {
const queryClient = getQueryClient()
await queryClient.prefetchQuery(['getAllUsers'], getAllUsers)
await queryClient.prefetchQuery(['getAllUsers'], getAllUsers, {
staleTime: Infinity,
})
const dehydratedState = dehydrate(queryClient)

return (
Expand Down
7 changes: 5 additions & 2 deletions src/components/organisms/NavBar/UserList/UserList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import Avatar from '@/components/atoms/Avatar'
import { Text } from '@/components/atoms/Text'
import APP_PATH from '@/config/paths'
import useFollow from '@/hooks/useFollow'
import useGetAllUsers from '@/queries/users'
import { useGetAllUsers } from '@/queries/users'
import { UserSummary } from '@/types/user'

export default function UserList() {
const { data } = useGetAllUsers()

return (
<>
<div className="nav-title">
Expand All @@ -21,7 +22,9 @@ export default function UserList() {
</div>
</div>
<ul className="avatar-list">
{data?.map((user) => <UserListItem key={user._id} userData={user} />)}
{data?.map((user: UserSummary) => (
<UserListItem key={user._id} userData={user} />
))}
</ul>
</>
)
Expand Down
20 changes: 3 additions & 17 deletions src/queries/users/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import { useQuery } from '@tanstack/react-query'
import { useCurrentUser } from '@/hooks/useCurrentUser'
import { getAllUsers } from '@/services/user'
import { UserSummary } from '@/types/user'

const useGetAllUsers = () => {
const { currentUser } = useCurrentUser()
export const useGetAllUsers = () => {
return useQuery({
queryKey: [
'getAllUsers',
currentUser?._id,
currentUser?.coverImage,
currentUser?.fullName,
currentUser?.name,
],
queryFn: async () => {
const data: UserSummary[] = await getAllUsers()
return data
},
queryKey: ['getAllUsers'],
queryFn: getAllUsers,
})
}

export default useGetAllUsers

0 comments on commit 4b05f81

Please sign in to comment.