Skip to content

Commit

Permalink
Merge pull request #15 from TripMingle/feat/SWM-259
Browse files Browse the repository at this point in the history
[refactor/SWM-259] 기존 코드 개선
  • Loading branch information
kxmmxnzx authored Oct 31, 2024
2 parents a12a2df + bcf7ba8 commit c7dd40e
Show file tree
Hide file tree
Showing 21 changed files with 299 additions and 186 deletions.
60 changes: 13 additions & 47 deletions src/app/[country]/board/[Id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
'use client';
import Image from 'next/image';
import '@/styles/font.css';
import { useEffect, useState } from 'react';
import Header from '@/components/header/Header';
import * as styles from '@/styles/country/board/id/page.css';
import TravelerCard from '@/components/country/board/id/TravelerCard';
import { Language } from '@/components/common/Language';
import CommentInput from '@/components/common/CommentInput';
import ChatModal from '@/components/country/board/id/ChatModal';
import CommentList from '@/components/common/CommentList';
import { usePathname } from 'next/navigation';
import { getBoardDetail } from '@/api/board';
import { BoardDetailType } from '@/types/country/board';
import { initialBoardDetail } from '@/store/boardStore';
import { TripTypeButton } from '@/components/common/TripTypeButton';
import AttributeBox from '@/components/country/board/id/AttributeBox';
import LoginModal from '@/components/common/LoginModal';
import { useUserStore } from '@/store/userStore';
import useModal from '@/hooks/useModal';

const Page = () => {
const [boardDetail, setBoardDetail] =
useState<BoardDetailType>(initialBoardDetail);
const pathname = usePathname();
import { BoardDetailType } from '@/types/country/board';
import { headers } from 'next/headers';

const isLoggedIn = useUserStore((state) => state.isLoggedIn);
const { isOpen, openModal, closeModal } = useModal();
const Page = async ({ params }: { params: { id: string } }) => {
const headersList = headers();
const host = headersList.get('host');

const getBoardId = () => {
const idx = pathname.lastIndexOf('/');
return Number(pathname.slice(idx + 1));
};
const boardData = await fetch(
`http://${host}/api/board?boardId=${params.id}`,
).then((res) => {
return res.json();
});

const getBoardData = async () => {
const boardId = getBoardId();
const data = await getBoardDetail(boardId);
setBoardDetail(data.data);
};
const boardDetail: BoardDetailType = boardData.data;

const hasTripStyle = () => {
if (boardDetail.preferGender !== 3) return true;
Expand All @@ -47,23 +32,9 @@ const Page = () => {
return false;
};

useEffect(() => {
getBoardData();
}, []);

return (
<main>
<Header />
{isLoggedIn ? (
<ChatModal
isOpen={isOpen}
nickName={boardDetail.nickName}
userId={boardDetail.userId}
closeModal={closeModal}
/>
) : (
<LoginModal isOpen={isOpen} closeModal={closeModal} />
)}
<div className={styles.pageContainer}>
<div className={styles.imageContainer}>
<Image
Expand Down Expand Up @@ -152,12 +123,12 @@ const Page = () => {
props={{
userImageUrl: boardDetail.userImageUrl,
nickName: boardDetail.nickName,
userId: boardDetail.userId,
ageRange: boardDetail.ageRange,
gender: boardDetail.gender,
nationality: boardDetail.nationality,
selfIntroduction: boardDetail.selfIntroduction ?? '',
}}
openModal={openModal}
/>
</div>
<div className={styles.container}>
Expand All @@ -169,12 +140,7 @@ const Page = () => {
</strong>
{' 개'}
</p>
<CommentInput
boardId={boardDetail.boardId}
inputHandler={getBoardData}
isOpen={isOpen}
openModal={openModal}
/>
<CommentInput boardId={boardDetail.boardId} />
<CommentList comments={boardDetail.boardComments} />
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/BoardCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const BoardCard = ({ props }: { props: BoardPreviewProps }) => {
};

return (
<div className={styles.postCard} onClick={clickHandler}>
<div className={styles.boardCard} onClick={clickHandler}>
<div className={styles.imageBox}>
<Image
src={props.imageUrl}
Expand Down
29 changes: 17 additions & 12 deletions src/components/common/CommentInput.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use client';
import * as styles from '@/styles/components/common/comment-input.css';
import Profile from './Profile';
import { postBoardComment } from '@/api/board';
import { useEffect, useRef, useState } from 'react';
import { useUserStore } from '@/store/userStore';
import useModal from '@/hooks/useModal';
import { useRouter } from 'next/navigation';
import LoginModal from './LoginModal';

const CommentInput = ({
boardId,
inputHandler,
isOpen,
openModal,
}: {
boardId: number;
isOpen: boolean;
inputHandler: () => void;
openModal: () => void;
}) => {
// TODO :: isOpen, openModal
const CommentInput = ({ boardId }: { boardId: number }) => {
const [content, setContent] = useState<string>('');

const { isOpen, openModal, closeModal } = useModal();

const router = useRouter();

const isLoggedIn = useUserStore((state) => state.isLoggedIn);

const inputRef = useRef<HTMLInputElement | null>(null);

const changeHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -34,7 +34,7 @@ const CommentInput = ({
const submitComment = async () => {
if (content.trim()) {
await postBoardComment(boardId, content.trim());
inputHandler();
router.refresh();
}
setContent('');
};
Expand All @@ -48,6 +48,11 @@ const CommentInput = ({

return (
<>
{isLoggedIn ? (
<></>
) : (
<LoginModal isOpen={isOpen} closeModal={closeModal} />
)}
<div
className={styles.commentInputProfileContainer}
onClick={clickHandler}
Expand Down
56 changes: 20 additions & 36 deletions src/components/common/Language.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
import * as styles from '@/styles/components/common/language.css';

const Korean = () => {
return (
<div className={styles.koreanBox}>
<span className={styles.text}>한국어</span>
</div>
);
};

const English = () => {
return (
<div className={styles.englishBox}>
<span className={styles.text}>영어</span>
</div>
);
};

const Chinese = () => {
return (
<div className={styles.ChineseBox}>
<span className={styles.text}>중국어</span>
</div>
);
};

const Japanese = () => {
return (
<div className={styles.JapaneseBox}>
<span className={styles.text}>일본어</span>
</div>
);
};

export const Language = ({ language }: { language: string }) => {
if (language === 'Korean') {
return <Korean />;
return (
<div>
<span className={styles.text({ language: 'korean' })}>한국어</span>
</div>
);
} else if (language === 'English') {
return <English />;
return (
<div>
<span className={styles.text({ language: 'english' })}>영어</span>
</div>
);
} else if (language === 'Chinese') {
return <Chinese />;
return (
<div>
<span className={styles.text({ language: 'chinese' })}>중국어</span>
</div>
);
} else {
return <Japanese />;
return (
<div>
<span className={styles.text({ language: 'japanese' })}>일본어</span>
</div>
);
}
};
1 change: 1 addition & 0 deletions src/components/common/LoginModal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import ModalWrapper from './ModalWrapper';
import { useUserStore } from '@/store/userStore';
import Image from 'next/image';
Expand Down
14 changes: 4 additions & 10 deletions src/components/common/ModalWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';
import * as styles from '@/styles/components/common/modal-wrapper.css';
import { ReactNode, useEffect } from 'react';
import { createPortal } from 'react-dom';

type Props = {
isOpen: boolean;
Expand All @@ -18,22 +19,15 @@ const ModalWrapper = ({ isOpen, closeModal, children }: Props) => {
closeModal();
};

useEffect(() => {
if (isOpen) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = 'auto';
}
}, [isOpen]);

if (!isOpen) return <></>;

return (
return createPortal(
<div className={styles.background} onClick={backgroundHandler}>
<div className={styles.modal} onClick={propagationHandler}>
{children}
</div>
</div>
</div>,
document.body,
);
};

Expand Down
7 changes: 6 additions & 1 deletion src/components/country/BoardPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import { BoardPreviewProps } from '@/types/country/board';
import { EmptyBoard } from './EmptyBoard';
import { usePathname } from 'next/navigation';
import { getCountryName } from '@/utils/country';
import BoardPreviewSkeleton from './BoardPreviewSkeleton';

const BoardPreview = () => {
const pathname = usePathname();
const [country, setCountry] = useState<string>('');
const [boardPreview, setBoardPreview] = useState<BoardPreviewProps[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);

const getBoardPreviewList = async () => {
if (country) {
const data = await getBoardPreview(country);
setBoardPreview(data.data);
setIsLoading(false);
}
};

Expand All @@ -41,7 +44,9 @@ const BoardPreview = () => {
</span>
<More path="/board" category="" />
</div>
{boardPreview.length ? (
{isLoading ? (
<BoardPreviewSkeleton />
) : boardPreview.length ? (
<ul className={styles.boardContainer}>
{boardPreview.map((board) => (
<li key={board.boardId}>
Expand Down
40 changes: 40 additions & 0 deletions src/components/country/BoardPreviewSkeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as styles from '@/styles/components/common/board-card.css';
import { boardContainer } from '@/styles/country/page.css';

const BoardPreviewSkeleton = () => {
return (
<div className={boardContainer}>
{[...Array(4)].map((_, index) => (
<div className={styles.containerSkeleton}>
<div className={`${styles.imageBox}`}>
<div className={`${styles.image} ${styles.skeleton}`}></div>
</div>
<div className={`${styles.contentContainer}`}>
<div
className={`${styles.languageSkeleton} ${styles.skeleton}`}
></div>
<span className={`${styles.title} ${styles.skeleton}`}></span>
<div className={`${styles.infoContainer} ${styles.skeleton}`}>
-
</div>
<div className={`${styles.infoContainer} ${styles.skeleton}`}>
-
</div>
<div className={`${styles.profileContainer}`}>
<div
className={`${styles.profileSkeleton} ${styles.skeleton}`}
></div>
<span
className={`${styles.profileInfoSkeleton} ${styles.skeleton}`}
>
f
</span>
</div>
</div>
</div>
))}
</div>
);
};

export default BoardPreviewSkeleton;
4 changes: 4 additions & 0 deletions src/components/country/MoveToMain.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use client';
import { useCountryStore } from '@/store/countryStore';
import { countryText, moveToMain } from '@/styles/country/page.css';
import { CountryInfo } from '@/types/main/country';
import { useRouter } from 'next/navigation';

const MoveToMain = ({ props }: { props: CountryInfo }) => {
const router = useRouter();
const { initialize, setContinent } = useCountryStore();

const continentClickHandler = () => {
setContinent(props.continentEnglishName, props.continentName);
router.push('/');
};

Expand All @@ -15,6 +18,7 @@ const MoveToMain = ({ props }: { props: CountryInfo }) => {
};

const homeClickHandler = () => {
initialize();
router.push('/');
};

Expand Down
Loading

0 comments on commit c7dd40e

Please sign in to comment.