-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[박기범] Week20 #1087
The head ref may contain hidden characters: "part4-\uBC15\uAE30\uBC94-week20"
[박기범] Week20 #1087
Changes from all commits
766a0f8
7a48590
e9664e9
02cd63a
d417acc
8f8c1d6
2b21a2f
6f8eea8
aa31f01
afc170e
f03bf11
812e73c
6fcd2b8
e582d68
ed738b2
35a8485
21c0711
2d52220
85721b6
f0f3991
0fbacff
24445a3
747091f
abe6eee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,35 +1,21 @@ | ||||||||||||||||
import React from "react"; | ||||||||||||||||
import { useEffect, useState } from "react"; | ||||||||||||||||
import { getFolder } from "@/pages/api/api"; | ||||||||||||||||
import { useQuery } from "@tanstack/react-query"; | ||||||||||||||||
import CardList from "@/components/CardList"; | ||||||||||||||||
import SearchBar from "@/components/SearchBar"; | ||||||||||||||||
import styles from "@/styles/CardSection.module.css"; | ||||||||||||||||
|
||||||||||||||||
interface CardListType { | ||||||||||||||||
id: number; | ||||||||||||||||
createdAt: string; | ||||||||||||||||
created_at: string; | ||||||||||||||||
url: string; | ||||||||||||||||
description: string; | ||||||||||||||||
imageSource: string; | ||||||||||||||||
image_source: string; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
export default function CardSection() { | ||||||||||||||||
const [cardList, setCardList] = useState<CardListType[]>([]); | ||||||||||||||||
|
||||||||||||||||
useEffect(() => { | ||||||||||||||||
async function getProfileFolder() { | ||||||||||||||||
try { | ||||||||||||||||
const { | ||||||||||||||||
folder: { links }, | ||||||||||||||||
} = await getFolder(); | ||||||||||||||||
setCardList(links); | ||||||||||||||||
} catch (error) { | ||||||||||||||||
console.error(error); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
getProfileFolder(); | ||||||||||||||||
}, []); | ||||||||||||||||
export default function CardSection({ id }: any) { | ||||||||||||||||
const cardList = useQuery({ | ||||||||||||||||
queryKey: ["individualList", Number(id)], | ||||||||||||||||
}); | ||||||||||||||||
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어랏
|
const cardList = useQuery({ | |
queryKey: ["individualList", Number(id)], | |
}); | |
const cardList = useQuery({ | |
queryKey: ["individualList", Number(id)], | |
queryFn: () => getCards(), | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cardList
의 각 요소들이 CardList
를 구성하여 다시 반환하고 있습니다.
이차원 배열이 아닌 이상 CardList
의 네이밍이 잘못된게 아닐까 싶어요. Card
로 짓는게 어떨까요?
<CardList | |
<Card |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
import React from "react"; | ||
import { useEffect, useState } from "react"; | ||
import { getFolder } from "@/pages/api/api"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { getFolder, getFolderUser } from "@/pages/api/api"; | ||
import styles from "@/styles/FolderBar.module.css"; | ||
|
||
export default function FolderBar() { | ||
const [folderName, setFolderName] = useState<string>(""); | ||
const [userName, setUserName] = useState<string>(""); | ||
const [profileImage, setProfileImage] = useState<string>(""); | ||
export default function FolderBar({ id }: any) { | ||
//폴더 이름을 가져오기 위한 쿼리 | ||
const folderInfo = useQuery({ | ||
queryKey: ["folderInfo"], | ||
queryFn: () => getFolder(id), | ||
staleTime: 0, | ||
enabled: id !== undefined, | ||
}); | ||
|
||
useEffect(() => { | ||
async function getProFileFolder() { | ||
try { | ||
const { | ||
folder: { name, owner }, | ||
} = await getFolder(); | ||
setFolderName(name); | ||
setUserName(owner.name); | ||
setProfileImage(owner.profileImageSource); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
//폴더 소유자를 가져오기 위한 쿼리 | ||
const folderOwner = useQuery({ | ||
queryKey: ["folderOwner"], | ||
queryFn: () => getFolderUser(folderInfo.data[0].user_id), | ||
enabled: !!folderInfo.data, | ||
}); | ||
Comment on lines
+16
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굳굳. 키와 API 함수가 적절하게 들어갔군요.리액트 쿼리를 쓰게 되면 |
||
|
||
getProFileFolder(); | ||
}, []); | ||
return ( | ||
<div className={styles.FolderBar}> | ||
<div className={styles.user}> | ||
<img id={styles.folderImg} src={profileImage} alt="폴더 이미지"></img> | ||
<span id={styles.userName}>@{userName}</span> | ||
<span id={styles.folderName}>{folderName}</span> | ||
<img | ||
id={styles.folderImg} | ||
src={folderOwner.data && folderOwner.data[0]?.image_source} | ||
alt="폴더 이미지" | ||
></img> | ||
<span id={styles.userName}> | ||
@{folderOwner.data && folderOwner.data[0]?.name} | ||
</span> | ||
<span id={styles.folderName}> | ||
{folderInfo.data && folderInfo.data[0]?.name} | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ import React from "react"; | |||||||||||||||||||||||||||
import Image from "next/image"; | ||||||||||||||||||||||||||||
import { useEffect, useState } from "react"; | ||||||||||||||||||||||||||||
import { getFolderList, getAllLinks, getFolderLink } from "@/pages/api/api"; | ||||||||||||||||||||||||||||
import { useQuery, useQueryClient } from "@tanstack/react-query"; | ||||||||||||||||||||||||||||
import CardList from "@/components/CardList"; | ||||||||||||||||||||||||||||
import SearchBar from "@/components/SearchBar"; | ||||||||||||||||||||||||||||
import addImg from "@/public/images/add.svg"; | ||||||||||||||||||||||||||||
|
@@ -28,69 +29,99 @@ interface CardListType { | |||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export default function FolderSection() { | ||||||||||||||||||||||||||||
const [folderName, setFolderName] = useState("폴더를 선택해주세요"); | ||||||||||||||||||||||||||||
const [folderList, setFolderList] = useState<FolderListType[]>([]); | ||||||||||||||||||||||||||||
const [folderName, setFolderName] = useState<string | undefined>( | ||||||||||||||||||||||||||||
"폴더를 선택해주세요" | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 초기 값이
|
const [folderName, setFolderName] = useState<string | undefined>( | |
"폴더를 선택해주세요" | |
); | |
const [folderName, setFolderName] = useState<string | null>( | |
null | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
쿼리 키는 한 곳에서 관리할 수도 있습니다 😊
상수를 선언하고 팩토리를 만들어서 동적인 쿼리키를 만들어낼 수 있습니다:
const articleKeys = {
all: ['articles'] as const,
list: (page: number) => [...articleKeys.all, 'list', page] as const,
favoriteList: (page: number) => [...articleKeys.all, 'favorite', page] as const,
detail: (id: number) => [...articleKeys.all, 'detail', id] as const,
}
const {data} = useQuery(articleKeys.list(page), ()=> getArticle(page));
혹은 다음 코드처럼 쿼리 키를 만들어볼 수도 있습니다:
// queries/queries.ts
import { createQueryKeys, mergeQueryKeys } from "@lukemorales/query-key-factory";
// queries/users.ts
export const users = createQueryKeys('users', {
all: null,
detail: (userId: string) => ({
queryKey: [userId],
queryFn: () => api.getUser(userId),
}),
});
예제에서는 lukemorales
라이브러리를 사용하나, 사용하지 않고 return
값만 쿼리키로 잘 구성되면 문제 없을 것 같네요 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기다린 후 처리되는 로직이 없기에 await
할 필요가 없어보입니다 !
queryFn: async () => { | |
if (selectedId) { | |
const data = await getFolderLink(selectedId); | |
return data; | |
} else { | |
return []; // 선택된 폴더가 없는 경우 빈 배열 반환 | |
} | |
queryFn: () => { | |
if (selectedId) { | |
return getFolderLink(selectedId); | |
} else { | |
return []; // 선택된 폴더가 없는 경우 빈 배열 반환 | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
의도적으로 캐싱을 하신 것으로 보입니다.
필요한 부분에서 folderId
를 사용하면 되지 않을까요? useQuery
를 사용하지 않아도 되보입니다 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오잉? 혹시 스네이크 케이스로 바꾸신 이유가 있으실까요?