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

[FE] refactor/#450 모아보기 기능 비회원도 가능하도록 수정 #516

Merged
merged 14 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 26 additions & 2 deletions frontend/src/components/AddSeeTogether/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ interface AddSeeTogetherProps {
id: number;
children: React.ReactNode;
getTopicsFromServer: () => void;
onClickAtlas: () => boolean;
}

const AddSeeTogether = ({
isInAtlas,
id,
children,
getTopicsFromServer,
onClickAtlas,
}: AddSeeTogetherProps) => {
const { showToast } = useToast();
const { seeTogetherTopics, setSeeTogetherTopics } =
useContext(SeeTogetherContext);

const accessToken = localStorage.getItem('userToken');

const addSeeTogetherList = async (e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation();

Expand All @@ -37,10 +41,11 @@ const AddSeeTogether = ({

const topics = await getApi<TopicCardProps[]>('/members/my/atlas');

setSeeTogetherTopics(topics);
setSeeTogetherTopics(topics.map((topic) => topic.id));

// TODO: 모아보기 페이지에서는 GET /members/my/atlas 두 번 됨
getTopicsFromServer();
onClickAtlas();

showToast('info', '모아보기에 추가했습니다.');
} catch {
Expand All @@ -56,17 +61,36 @@ const AddSeeTogether = ({

const topics = await getApi<TopicCardProps[]>('/members/my/atlas');

setSeeTogetherTopics(topics);
setSeeTogetherTopics(topics.map((topic) => topic.id));

// TODO: 모아보기 페이지에서는 GET /members/my/atlas 두 번 됨
getTopicsFromServer();
onClickAtlas();

showToast('info', '해당 지도를 모아보기에서 제외했습니다.');
} catch {
showToast('error', '로그인 후 사용해주세요.');
}
};

const onChangeIsInAtlas = (e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation();

const isChangeAtlas = onClickAtlas();

if (isChangeAtlas) {
if (seeTogetherTopics?.includes(id))
setSeeTogetherTopics(
seeTogetherTopics.filter((topicId) => topicId !== id),
);
else setSeeTogetherTopics((prev) => [...prev, id]);
}
};

if (accessToken === null) {
return <Wrapper onClick={onChangeIsInAtlas}>{children}</Wrapper>;
}

return (
<Wrapper onClick={isInAtlas ? deleteSeeTogether : addSeeTogetherList}>
{children}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/SeeTogetherCounter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const SeeTogetherCounter = () => {
if (!userToken) return;

const topics = await getApi<TopicCardProps[]>('/members/my/atlas');
setSeeTogetherTopics(topics);
setSeeTogetherTopics(topics.map((topic) => topic.id));
} catch {
showToast(
'error',
Expand Down
22 changes: 20 additions & 2 deletions frontend/src/components/TopicCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Text from '../common/Text';
import useNavigator from '../../hooks/useNavigator';
import Box from '../common/Box';
import Image from '../common/Image';
import { SyntheticEvent, useContext } from 'react';
import { SyntheticEvent, useContext, useState } from 'react';
import Space from '../common/Space';
import Flex from '../common/Flex';
import FavoriteSVG from '../../assets/favoriteBtn_filled.svg';
Expand All @@ -18,6 +18,7 @@ import AddFavorite from '../AddFavorite';
import { TopicCardProps } from '../../types/Topic';
import useKeyDown from '../../hooks/useKeyDown';
import { ModalContext } from '../../context/ModalContext';
import useToast from '../../hooks/useToast';

interface OnClickDesignatedProps {
topicId: number;
Expand Down Expand Up @@ -47,6 +48,9 @@ const TopicCard = ({
const { routePage } = useNavigator();
const { closeModal } = useContext(ModalContext);
const { elementRef, onElementKeyDown } = useKeyDown<HTMLLIElement>();
const [isInNonMemberAtlas, setIsInNonMemberAtlas] =
useState<boolean>(isInAtlas);
const { showToast } = useToast();

const goToSelectedTopic = () => {
routePage(`/topics/${id}`, [id]);
Expand All @@ -60,6 +64,15 @@ const TopicCard = ({
closeModal('newPin');
};

const onClickIsInAtlas = () => {
setIsInNonMemberAtlas(!isInNonMemberAtlas);
if (!isInNonMemberAtlas) {
showToast('info', '모아보기에 추가했습니다.');
return true;
}
showToast('info', '해당 지도를 모아보기에서 제외했습니다.');
return true;
};
return (
<Wrapper
data-cy="topic-card"
Expand Down Expand Up @@ -139,10 +152,15 @@ const TopicCard = ({
<ButtonWrapper>
<AddSeeTogether
isInAtlas={isInAtlas}
onClickAtlas={onClickIsInAtlas}
id={id}
getTopicsFromServer={getTopicsFromServer}
>
{isInAtlas ? <SeeTogetherSVG /> : <SeeTogetherNotFilledSVG />}
{isInNonMemberAtlas ? (
<SeeTogetherSVG />
) : (
<SeeTogetherNotFilledSVG />
)}
</AddSeeTogether>
<AddFavorite
isBookmarked={isBookmarked}
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/components/TopicInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ const TopicInfo = ({
showToast('error', '토픽 링크를 복사하는데 실패했습니다.');
}
};

const onChangeIsInAtlas = () => {
showToast('warning', '비회원은 홈에서만 모아보기에 담을 수 있습니다.');
return false;
};

useEffect(() => {
if (!isUpdate) setTopicsFromServer();
}, [isUpdate]);

if (isUpdate) {
return (
Expand Down Expand Up @@ -148,6 +157,7 @@ const TopicInfo = ({
<ButtonsWrapper>
<AddSeeTogether
isInAtlas={isInAtlas}
onClickAtlas={onChangeIsInAtlas}
id={Number(topicId)}
getTopicsFromServer={setTopicsFromServer}
>
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/context/SeeTogetherContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
import { TopicCardProps } from '../types/Topic';

interface SeeTogetherContextProps {
seeTogetherTopics: TopicCardProps[] | null;
setSeeTogetherTopics: Dispatch<SetStateAction<TopicCardProps[] | null>>;
seeTogetherTopics: number[] | null;
setSeeTogetherTopics: Dispatch<SetStateAction<number[] | []>>;
Comment on lines +11 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

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

이전에 전화로 전달해주셨을 때 그대로 써도 문제없을 것 같았는데 AddSeeTogether onChangeIsInAtlas 함수안에

    else setSeeTogetherTopics((prev) => [...prev, id]);

요 부분 때문에 바꿔야만 하는군요 ㅋㅋㅋㅋ 좋습니다. 👍👍

}

interface SeeTogetherProviderProps {
Expand All @@ -23,8 +23,8 @@ export const SeeTogetherContext = createContext<SeeTogetherContextProps>({

const SeeTogetherProvider = ({ children }: SeeTogetherProviderProps) => {
const [seeTogetherTopics, setSeeTogetherTopics] = useState<
TopicCardProps[] | null
>(null);
number[] | []
>([]);

return (
<SeeTogetherContext.Provider
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/hooks/useNavigator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useContext } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { ModalContext } from '../context/ModalContext';
import { SeeTogetherContext } from '../context/SeeTogetherContext';

const useNavigator = () => {
const navigator = useNavigate();

const { openModal, closeModal } = useContext(ModalContext);
const { topicId } = useParams();
const { seeTogetherTopics } = useContext(SeeTogetherContext);

const routePage = (url: string | -1, value?: string | number | number[]) => {
if (typeof url === 'string') navigator(url, { state: value });
if (url === -1) navigator(url);
Expand All @@ -15,7 +18,12 @@ const useNavigator = () => {
return {
routingHandlers: {
home: () => routePage('/'),
seeTogether: () => routePage('/see-together'),
seeTogether: () =>
routePage(
`/topics/${
seeTogetherTopics?.length === 0 ? -1 : seeTogetherTopics?.join(',')
}`,
),
addMapOrPin: () => openModal('addMapOrPin'),
favorite: () => routePage('/favorite'),
profile: () => routePage('/my-page'),
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Suspense, lazy, useContext, useEffect } from 'react';
import { MarkerContext } from '../context/MarkerContext';
import TopicCardContainerSkeleton from '../components/Skeletons/TopicListSkeleton';
import { setFullScreenResponsive } from '../constants/responsive';
import { SeeTogetherContext } from '../context/SeeTogetherContext';
import SearchBar from '../components/SearchBar/SearchBar';

const TopicListContainer = lazy(
Expand All @@ -18,13 +19,21 @@ const Home = () => {
const { routingHandlers } = useNavigator();
const { goToPopularTopics, goToLatestTopics, goToNearByMeTopics } =
routingHandlers;

const { seeTogetherTopics, setSeeTogetherTopics } =
useContext(SeeTogetherContext);
const { markers, removeMarkers, removeInfowindows } =
useContext(MarkerContext);
const accessToken = localStorage.getItem('userToken');

useSetLayoutWidth(FULLSCREEN);
useSetNavbarHighlight('home');

useEffect(() => {
if (accessToken === null && seeTogetherTopics?.length !== 0) {
setSeeTogetherTopics([]);
}
}, []);

Comment on lines +31 to +36
Copy link
Collaborator

Choose a reason for hiding this comment

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

요 로직이 모아보기 페이지에도 있어야할 것 같네요. 제가 원래 생각했던 것은 비회원에게는 모아보기가 일회성 장바구니처럼 기능을 제공하는 쪽이었습니다. 그래서 모아보기 리스트를 초기화하면서 로그인을 하면 모아보기 리스트가 유지돼요! 이런식의 토스트를 남겨둘 생각이었어요. (로그인 유도도 좀 할겸?)

그래도 홈에도 여전히 이 로직이 필요합니다. 왜그러냐면 비회원이 접속할 수 있는 페이지가 홈이랑 모아보기 둘 뿐이면 홈에는 필요가 없지만, 안타깝게도 저희에겐 askLogin 페이지도 있기 때문입니다 ㅋㅋㅋㅋ 그래서 홈 -> 모아보기 담고 -> 즐겨찾기(애스크 로그인으로 전환) -> 홈 이렇게 이동하면 모아보기 리스트는 그대로지만 UI는 초기화 되어있겠죠.

모아보기 페이지에서 초기화를 안해주면 결정적으로 topicInfo 의 모아보기 버튼과 UI가 차이가 나는 결정적인 문제가 있기도합니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

세인! 이 부분은 저희 금요일에 만나서 한번 더 얘기보는게 어떨까요?! 제가 잘 이해를 못하기도 했고 실제로 만나 화면을 보면서 말하면 금방 이해할 수 있을 것 같아서요!! 일단 이 전까지 코멘트는 모두 반영하였습니다! 이 부분 만나서 화면으로 보고 바로 반영해봐요!!!

useEffect(() => {
if (markers && markers.length > 0) {
removeMarkers();
Expand Down
Loading