Skip to content

Commit

Permalink
Merge pull request #45 from Chaem03/feature/#22
Browse files Browse the repository at this point in the history
✨ Feature: 페이지간 data 전달 location 처리
  • Loading branch information
Chaem03 authored Sep 5, 2024
2 parents 147aca4 + 03f2c7b commit aa39a95
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/pages/CompletePage/CompletePage.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as S from "./styled";
import HeartBackG from "../../components/common/Heartbackground/heartBackG";
import { useNavigate } from "react-router-dom";
import { useParams } from "react-router-dom";
import { useOtherBox } from "../../hooks/useOtherBox";
import { useNavigate, useParams, useLocation } from "react-router-dom";
export const CompletePage = () => {
const { otherData } = useOtherBox();
const { boxId } = useParams();
const navigate = useNavigate();
const location = useLocation();
const otherData = location.state?.otherData;

if (!otherData) {
return <div>Data not found</div>;
}
return (
<S.Container>
<HeartBackG />
Expand Down
8 changes: 4 additions & 4 deletions src/pages/LetterPage/LetterPostPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NameSetting } from "../../components/NameSetting/NameSetting";
import HeartBackG from "../../components/common/Heartbackground/heartBackG";
import ChocoSelector from "../../components/ChocoSelector/ChocoSelector";
import { LetterPost } from "../../components/letterPost/LetterPost";
import { useNavigate, useParams } from "react-router-dom";
import { useNavigate, useParams, useLocation } from "react-router-dom";
import { postChocolate } from "../../apis/postChoco";

export const LetterPostPage = () => {
Expand All @@ -14,7 +14,8 @@ export const LetterPostPage = () => {
const [isSelectedChoco, setIsSelectedChoco] = useState(false);
const [nickName, setNickName] = useState("");
const [isNameSetting, setIsNameSetting] = useState(false);

const location = useLocation();
const { otherData } = location.state || {};
const handleSelectChoco = (id) => {
setSelectedChoco(id);
console.log(`choco ID: ${id}`);
Expand Down Expand Up @@ -44,10 +45,9 @@ export const LetterPostPage = () => {

try {
const res = await postChocolate(boxId, nickName, content, selectedChoco);
console.log("post된 값:", res);
if (res.status === 201) {
console.log("초콜릿전달 성공");
navigate(`/box/${boxId}/complete`);
navigate(`/box/${boxId}/complete`, { state: { otherData } });
} else {
console.log("초콜릿 전송 실패");
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/otherboxPage/otherboxPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const OtherboxPage = () => {
const MoveOnLetterP = () => {
const boxId = otherData.boxId;

navigate(`/box/${boxId}/choco`);
navigate(`/box/${boxId}/choco`, { state: { otherData } });
};
if (!otherData) {
return <div>Loading...</div>; // 데이터가 로드되기 전에 로딩 메시지를 표시하거나 로딩 스피너를 추가할 수 있음
Expand Down

0 comments on commit aa39a95

Please sign in to comment.