Skip to content

Commit

Permalink
fix: 일기 삭제 시 별자리 삭제 안 되는 오류 해결
Browse files Browse the repository at this point in the history
- 별자리 정보를 메인 페이지에서 Recoil로 관리
- 일기 삭제 시 별자리 정보 refetch
  • Loading branch information
dmson1218 committed Dec 7, 2023
1 parent fb6bda1 commit ad84327
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
1 change: 1 addition & 0 deletions FE/src/atoms/starAtom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const starAtom = atom({
mode: "create",
drag: true,
selected: null,
points: [],
},
});

Expand Down
3 changes: 2 additions & 1 deletion FE/src/components/DiaryModal/DiaryDeleteModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lastPageAtom from "../../atoms/lastPageAtom";
import ModalWrapper from "../../styles/Modal/ModalWrapper";

function DiaryDeleteModal(props) {
const { refetch } = props;
const { refetch, pointsRefetch } = props;
const [diaryState, setDiaryState] = useRecoilState(diaryAtom);
const userState = useRecoilValue(userAtom);
const [lastPageState, setLastPageState] = useRecoilState(lastPageAtom);
Expand Down Expand Up @@ -40,6 +40,7 @@ function DiaryDeleteModal(props) {
})
.then(() => {
refetch();
pointsRefetch();
});
}

Expand Down
6 changes: 4 additions & 2 deletions FE/src/components/DiaryModal/DiaryReadModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function getDiary(accessToken, diaryUuid, setUserState) {
}

function DiaryReadModal(props) {
const { refetch } = props;
const { refetch, pointsRefetch } = props;
const [diaryState, setDiaryState] = useRecoilState(diaryAtom);
const [userState, setUserState] = useRecoilState(userAtom);
const [lastPageState, setLastPageState] = useRecoilState(lastPageAtom);
Expand Down Expand Up @@ -172,7 +172,9 @@ function DiaryReadModal(props) {
/>
</DiaryModalIcon>
</DiaryModalEmotionBar>
{diaryState.isDelete ? <DiaryDeleteModal refetch={refetch} /> : null}
{diaryState.isDelete ? (
<DiaryDeleteModal refetch={refetch} pointsRefetch={pointsRefetch} />
) : null}
<ModalSideButtonWrapper>
<ModalSideButton
onClick={() => {
Expand Down
35 changes: 33 additions & 2 deletions FE/src/pages/MainPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import diaryAtom from "../atoms/diaryAtom";
import shapeAtom from "../atoms/shapeAtom";
import userAtom from "../atoms/userAtom";
import starAtom from "../atoms/starAtom";
import lastPageAtom from "../atoms/lastPageAtom";
import DiaryCreateModal from "../components/DiaryModal/DiaryCreateModal";
import DiaryReadModal from "../components/DiaryModal/DiaryReadModal";
Expand All @@ -23,6 +24,7 @@ function MainPage() {
const [userState, setUserState] = useRecoilState(userAtom);
const setShapeState = useSetRecoilState(shapeAtom);
const [loaded, setLoaded] = React.useState(false);
const setStarState = useSetRecoilState(starAtom);

const { refetch } = useQuery(
["diaryList", userState.accessToken],
Expand Down Expand Up @@ -86,6 +88,33 @@ function MainPage() {
},
);

const { refetch: pointsRefetch } = useQuery(
"points",
() =>
fetch(`${process.env.REACT_APP_BACKEND_URL}/lines`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${userState.accessToken}`,
},
}).then((res) => res.json()),
{
onSuccess: (data) => {
data.forEach((point) => {
const { first, second } = point;
first.coordinate.x /= 100000;
first.coordinate.y /= 100000;
first.coordinate.z /= 100000;
second.coordinate.x /= 100000;
second.coordinate.y /= 100000;
second.coordinate.z /= 100000;
});

setStarState((prev) => ({ ...prev, points: data }));
},
},
);

useEffect(() => {
setDiaryState((prev) => {
const newState = {
Expand Down Expand Up @@ -130,9 +159,11 @@ function MainPage() {
<NickNameWrapper>
<NickName>{userState.nickname}님의 별숲</NickName>
</NickNameWrapper>
<StarPage refetch={refetch} />
<StarPage refetch={refetch} pointsRefetch={pointsRefetch} />
{diaryState.isCreate ? <DiaryCreateModal refetch={refetch} /> : null}
{diaryState.isRead ? <DiaryReadModal refetch={refetch} /> : null}
{diaryState.isRead ? (
<DiaryReadModal refetch={refetch} pointsRefetch={pointsRefetch} />
) : null}
{diaryState.isUpdate ? <DiaryUpdateModal refetch={refetch} /> : null}
{diaryState.isList ? <DiaryListModal /> : null}
{diaryState.isAnalysis ? <DiaryAnalysisModal /> : null}
Expand Down
41 changes: 7 additions & 34 deletions FE/src/pages/StarPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/no-unknown-property */

import React, { useState, useEffect } from "react";
import { useQuery, useMutation } from "react-query";
import { useMutation } from "react-query";
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import styled from "styled-components";
import { Canvas, useThree } from "@react-three/fiber";
Expand All @@ -19,7 +19,7 @@ import stella from "../assets/stella.svg";
import arrow from "../assets/arrow.svg";
import paint from "../assets/paint.svg";

function StarPage({ refetch }) {
function StarPage({ refetch, pointsRefetch }) {
const [diaryState, setDiaryState] = useRecoilState(diaryAtom);
const [starState, setStarState] = useRecoilState(starAtom);

Expand All @@ -45,7 +45,7 @@ function StarPage({ refetch }) {
target={[0, 0, 0]}
rotateSpeed={-0.25}
/>
<StarView refetch={refetch} />
<StarView refetch={refetch} pointsRefetch={pointsRefetch} />
</Canvas>
</CanvasContainer>
{!(
Expand Down Expand Up @@ -168,7 +168,7 @@ function Scene() {
);
}

function StarView({ refetch }) {
function StarView({ refetch, pointsRefetch }) {
const { scene, raycaster, camera } = useThree();
const [diaryState, setDiaryState] = useRecoilState(diaryAtom);
const userState = useRecoilValue(userAtom);
Expand All @@ -177,31 +177,6 @@ function StarView({ refetch }) {
const shapeState = useRecoilValue(shapeAtom);
const [texture, setTexture] = useState({});

const { data: points, refetch: pointsRefetch } = useQuery(
"points",
() =>
fetch(`${process.env.REACT_APP_BACKEND_URL}/lines`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${userState.accessToken}`,
},
}).then((res) => res.json()),
{
onSuccess: (data) => {
data.forEach((point) => {
const { first, second } = point;
first.coordinate.x /= 100000;
first.coordinate.y /= 100000;
first.coordinate.z /= 100000;
second.coordinate.x /= 100000;
second.coordinate.y /= 100000;
second.coordinate.z /= 100000;
});
},
},
);

async function updateDiaryFn(data) {
setDiaryState((prev) => ({
...prev,
Expand Down Expand Up @@ -385,12 +360,11 @@ function StarView({ refetch }) {
sentiment={diary.emotion.sentiment}
texture={texture[diary.shapeUuid]}
moveToStar={moveToStar}
points={points}
refetch={pointsRefetch}
/>
))
: null}
{points?.map((point) => (
{starState.points?.map((point) => (
<Line
key={point.id}
point={[point.first.coordinate, point.second.coordinate]}
Expand All @@ -401,8 +375,7 @@ function StarView({ refetch }) {
}

function Star(props) {
const { uuid, position, sentiment, texture, moveToStar, points, refetch } =
props;
const { uuid, position, sentiment, texture, moveToStar, refetch } = props;
const setDiaryState = useSetRecoilState(diaryAtom);
const userState = useRecoilValue(userAtom);
const [starState, setStarState] = useRecoilState(starAtom);
Expand Down Expand Up @@ -473,7 +446,7 @@ function Star(props) {
selected: { uuid, position },
}));
} else {
const isExist = points.find(
const isExist = starState.points.find(
(point) =>
(point.first.uuid === selected.uuid && point.second.uuid === uuid) ||
(point.first.uuid === uuid && point.second.uuid === selected.uuid),
Expand Down

0 comments on commit ad84327

Please sign in to comment.