Skip to content
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

[김한샘] Week15 #475

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2af053a
refactor: GetDaysAgo 조건 수정
hansaemkim38 May 21, 2024
aab086e
refactor: useAuthGuard 훅 추가
hansaemkim38 May 21, 2024
65877ab
refactor: 피드백 반영 > loginButton -> submitButton 변경
hansaemkim38 May 21, 2024
18c383a
refactor: 피드백 반영 > 오타수정
hansaemkim38 May 21, 2024
c391bed
add: folder, shared 추가 제거
hansaemkim38 May 22, 2024
c6710a1
feat: Request interceptors 추가
hansaemkim38 May 22, 2024
46d3d70
feat: 기존 탭 관련 folder페이지에서 불러오던 api 변경
hansaemkim38 May 22, 2024
3e1c926
feat: folder page > 다이나믹 라우터 추가
hansaemkim38 May 22, 2024
35afd15
refactor : 피드백 반영 > 절대경로
hansaemkim38 May 26, 2024
b7e80f5
refactor : FolderTabList > 이름 상위에서 처리
hansaemkim38 May 26, 2024
f16dc75
feat: 미사용 삭제 및 api 추가
hansaemkim38 May 26, 2024
78774a0
feat: Shared 페이지 동적 라우터 추가
hansaemkim38 May 26, 2024
165b2d5
refactor: Folder page 리다이렉트 추가, 상수 추가
hansaemkim38 May 26, 2024
78fe1ed
refactor: 개별 상태 변수를 단일 상태 객체로 교체
hansaemkim38 May 26, 2024
4b4dbd3
refactor: 피드백 반영 > 변수명 변경
hansaemkim38 May 26, 2024
37510cd
remove: 미사용 type 삭제
hansaemkim38 May 26, 2024
7552961
feat: 기존 샘플 헤더 데이터를 로그인 유저 정보로 변경
hansaemkim38 May 26, 2024
47d2e30
refactor: 기존 해더부분 folder/[id]일시 스타일 적용 수정
hansaemkim38 May 26, 2024
73cbf87
feat: TabData 관련 기존 샘플 삭제 및 교체
hansaemkim38 May 26, 2024
468a86d
test: test
hansaemkim38 May 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const nextConfig = {
"ssl.pstatic.net",
"data1.pokemonkorea.co.kr",
"jasonwatmore.com",
"avatars.githubusercontent.com",
],
},
};
Expand Down
97 changes: 97 additions & 0 deletions pages/folder/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Search from "@/src/components/Search/Search";
import React, { useEffect, useState } from "react";
import AddLinkForm from "@/src/components/AddLinkForm/AddLinkForm";
import { getFolderIdLinks, getSharedFolderIdData } from "@/src/fetchUtils/index";
import FolderTabList from "@/src/components/FolderTabList/FolderTabList";
import CardList from "@/src/components/CardList/CardList";
import useModal from "@/src/hooks/useModal";
import ModalContext from "@/src/components/Modal/ModalContext";
import ModalContainer from "@/src/components/Modal/ModalContainer";
import Header from "@/src/components/Header/Header";
import Footer from "@/src/components/Footer/Footer";
import { useRouter } from "next/router";
import { getAccessToken } from "@/src/utils/constants";

function Folder() {
const [folderTabDataList, setFolderTabDataList] = useState<FolderTabDataList[]>([]);
const [userFolderDataList, setUserFolderDataList] = useState<UserFolderdataList[]>();

const { isOpen, openModal, closeModal } = useModal();
const [modalType, setModalType] = useState("");
const [cardUrl, setCardUrl] = useState("");
const [folderTabName, setFolderTabName] = useState<string | null>("");
const [searchInputValue, setSearchInputValue] = useState<string>("");
const [folderDataId, setFolderDataId] = useState<number>(0);
const [name, setName] = useState<string>("");

const router = useRouter();
const { id } = router.query;

useEffect(() => {
const token = getAccessToken();
if (!token) {
router.push("/signin");
}
}, [router]);

useEffect(() => {
if (!router.isReady) return;

async function fetchDataAndSetState() {
const folderTabDataListPromise = getSharedFolderIdData();
const userFolderDataListPromise = getFolderIdLinks(Number(id));

const [fetchedFolderTabDataList, fetchedUserFolderDataList] = await Promise.all([
folderTabDataListPromise,
userFolderDataListPromise,
]);

fetchedFolderTabDataList.data.folder.filter((item: FolderTabDataList) => {
if (item.id === Number(id)) {
setName(item.name);
}
});
setFolderTabDataList(fetchedFolderTabDataList.data.folder);
setUserFolderDataList(fetchedUserFolderDataList?.data.folder);
setFolderDataId(Number(id));
}
fetchDataAndSetState();
}, [id, router]);

const onChangeValue = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchInputValue(e.target.value);
};

return (
<>
<Header setFolderDataId={setFolderDataId} />
<div className="content-wrap">
<ModalContext.Provider
value={{ isOpen, openModal, closeModal, setModalType, setCardUrl, folderDataId }}
>
<AddLinkForm />
<ModalContainer
modalType={modalType}
folderTabDataList={folderTabDataList}
cardUrl={cardUrl}
folderTabName={folderTabName}
/>
<div className="wrap">
<Search searchInputValue={searchInputValue} onChangeValue={onChangeValue} />
<FolderTabList
folderTabDataList={folderTabDataList}
setUserFolderDataList={setUserFolderDataList}
setFolderTabName={setFolderTabName}
name={name}
setName={setName}
/>
<CardList userFolderDataList={userFolderDataList} searchInputValue={searchInputValue} />
</div>
</ModalContext.Provider>
</div>
<Footer />
</>
);
}

export default Folder;
33 changes: 22 additions & 11 deletions pages/folder.tsx → pages/folder/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Search from "@/src/components/Search/Search";
import React, { useEffect, useState } from "react";
import AddLinkForm from "@/src/components/AddLinkForm/AddLinkForm";
import { tabDataList, userFoldersData } from "@/src/fetchUtils/index";
import { getFolderIdLinks, getSharedFolderIdData } from "@/src/fetchUtils/index";
import FolderTabList from "@/src/components/FolderTabList/FolderTabList";
import CardList from "@/src/components/CardList/CardList";
import useModal from "@/src/hooks/useModal";
import ModalContext from "@/src/components/Modal/ModalContext";
import ModalContainer from "@/src/components/Modal/ModalContainer";
import Header from "@/src/components/Header/Header";
import Footer from "@/src/components/Footer/Footer";
import { useRouter } from "next/router";
import { getAccessToken } from "@/src/utils/constants";

function Folder() {
const [folderTabDataList, setFolderTabDataList] = useState<FolderTabDataList[]>([]);
Expand All @@ -19,20 +21,29 @@ function Folder() {
const [cardUrl, setCardUrl] = useState("");
const [folderTabName, setFolderTabName] = useState<string | null>("");
const [searchInputValue, setSearchInputValue] = useState<string>("");
const [forderDataId, setForderDataId] = useState<number>(0);
const [folderDataId, setFolderDataId] = useState<number>(0);
const [name, setName] = useState<string>("");
const router = useRouter();

useEffect(() => {
const token = getAccessToken();
if (!token) {
router.push("/signin");
}
}, [router]);

useEffect(() => {
async function fetchDataAndSetState() {
const folderTabDataListPromise = tabDataList();
const userFolderDataListPromise = userFoldersData();
const folderTabDataListPromise = getSharedFolderIdData();
const userFolderDataListPromise = getFolderIdLinks();

const [folderTabDataList, userFolderDataList] = await Promise.all([
const [fetchedFolderTabDataList, fetchedUserFolderDataList] = await Promise.all([
folderTabDataListPromise,
userFolderDataListPromise,
]);

setFolderTabDataList(folderTabDataList.data);
setUserFolderDataList(userFolderDataList.data);
setFolderTabDataList(fetchedFolderTabDataList.data.folder);
setUserFolderDataList(fetchedUserFolderDataList?.data.folder);
}
fetchDataAndSetState();
}, []);
Expand All @@ -43,10 +54,10 @@ function Folder() {

return (
<>
<Header />
<Header setFolderDataId={setFolderDataId} />
<div className="content-wrap">
<ModalContext.Provider
value={{ isOpen, openModal, closeModal, setModalType, setCardUrl, forderDataId }}
value={{ isOpen, openModal, closeModal, setModalType, setCardUrl, folderDataId }}
>
<AddLinkForm />
<ModalContainer
Expand All @@ -61,8 +72,8 @@ function Folder() {
folderTabDataList={folderTabDataList}
setUserFolderDataList={setUserFolderDataList}
setFolderTabName={setFolderTabName}
forderDataId={forderDataId}
setForderDataId={setForderDataId}
name={name}
setName={setName}
/>
<CardList userFolderDataList={userFolderDataList} searchInputValue={searchInputValue} />
</div>
Expand Down
51 changes: 0 additions & 51 deletions pages/shared.tsx

This file was deleted.

92 changes: 92 additions & 0 deletions pages/shared/[id].tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비즈니스 로직이 길어지게 되면 해당 로직을 커스텀 훅으로 별도로 분리 해보는 것도 하나의 방법이 될 수 있습니다.
먼저 하나의 커스텀 훅으로 분리를 해보고 해당 훅으로부터 연관된것들끼리 묶어 별도의 커스텀 훅으로 쪼개나가는 것도 좋은 리팩토링 방법이 될것으로 보여집니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React from "react";
import ConHeader from "@/src/components/ConHeader/ConHeader";
import Search from "@/src/components/Search/Search";
import CardList from "@/src/components/CardList/CardList";
import { useEffect, useState } from "react";
import {
getSharedData,
getSharedFolderIdData,
getSharedFolderUserData,
} from "@/src/fetchUtils/index";
import Header from "@/src/components/Header/Header";
import Footer from "@/src/components/Footer/Footer";
import { useRouter } from "next/router";

function Shared() {
const [folderData, setFolderData] = useState<SharedAuthData | null>(null);
const [cardListData, setCardListData] = useState<FolderLinks[]>([]);
const [searchInputValue, setSearchInputValue] = useState<string>("");
const [folderInfo, setFolderInfo] = useState({
userId: 0,
name: "",
});
const router = useRouter();
const { id } = router.query; // folderId

// 폴더 이름 id 세팅.
useEffect(() => {
if (!router.isReady) return;
async function fetchDataAndSetState() {
const response = await getSharedFolderIdData(Number(id));
if (response) {
const { data } = response;

setFolderInfo((prev) => ({
...prev,
userId: data[0].userId,
}));
setFolderInfo((prev) => ({
...prev,
name: data[0].name,
}));
}
}
fetchDataAndSetState();
}, [router, id]);

// UserId 코드잇 이름정보, 이미지 세팅
useEffect(() => {
if (!router.isReady) return;
async function fetchDataAndSetState() {
const response = await getSharedFolderUserData(folderInfo.userId);
if (response) {
const { data } = response;
setFolderData(data[0]);
}
}
fetchDataAndSetState();
}, [folderInfo.userId, router]);

// 카드정보
useEffect(() => {
if (!router.isReady) return;
async function fetchDataAndSetState() {
const response = await getSharedData(folderInfo.userId, Number(id));
if (response) {
const { data } = response;
setCardListData(data);
}
}
fetchDataAndSetState();
}, [id, folderInfo.userId, router]);

const onChangeValue = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchInputValue(e.target.value);
};

return (
<>
<Header />
<div className="content-wrap">
<ConHeader folderData={folderData} folderInfo={folderInfo} />
<div className="wrap">
<Search searchInputValue={searchInputValue} onChangeValue={onChangeValue} />
<CardList cardListData={cardListData} searchInputValue={searchInputValue} />
</div>
</div>
<Footer />
</>
);
}

export default Shared;
4 changes: 2 additions & 2 deletions src/components/AuthHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ function AuthHeader() {
{router.pathname === "/signin" && (
<p>
회원이 아니신가요?
<Link href="./signup" className="underline">
<Link href="/signup" className="underline">
회원 가입하기
</Link>
</p>
)}
{router.pathname === "/signup" && (
<p>
이미 회원이신가요?
<Link href="./signin" className="underline">
<Link href="/signin" className="underline">
로그인 하기
</Link>
</p>
Expand Down
4 changes: 2 additions & 2 deletions src/components/CardList/CardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function CardList({
searchInputValue: string;
}) {
const router = useRouter();
const isFolder = router.pathname === "/folder";
const isFolder = router.pathname.includes("/folder");
const searchValue = searchInputValue.toLowerCase();

const filteredCardData = () => {
Expand All @@ -47,7 +47,7 @@ function CardList({
return cardListData.filter((card) => {
return (
card.title?.includes(searchValue) ||
card.url.includes(searchValue) ||
card.url?.includes(searchValue) ||
card.description?.includes(searchValue)
);
});
Expand Down
21 changes: 14 additions & 7 deletions src/components/ConHeader/ConHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import styles from "@/src/components/ConHeader/ConHeader.module.css";
import Image from "next/image";

function ConHeader({ folderData }: { folderData: FolderData | null }) {
const owner = folderData?.owner;

function ConHeader({
folderData,
folderInfo,
}: {
folderData: SharedAuthData | null;
folderInfo: {
userId: number;
name: string;
};
}) {
return (
<div className={styles.conHeaderWrap}>
<div className={styles.conHeader}>
{owner?.profileImageSource && (
<Image width={60} height={60} src={owner.profileImageSource} alt={owner.name} />
{folderData?.imageSource && (
<Image width={60} height={60} src={folderData?.imageSource} alt={folderData?.name} />
)}
<h3 className={styles.h3}>{owner?.name}</h3>
<h3 className={styles.h3}>{folderData?.name}</h3>
</div>
<h2 className={styles.h2}>{folderData?.name}</h2>
<h2 className={styles.h2}>{folderInfo.name}</h2>
</div>
);
}
Expand Down
Loading
Loading