๊ฐ์ ์ผ๊ธฐ์ฅ ๋ง๋ค๊ธฐ 2 - CRUD ๊ธฐ๋ฅ ๊ตฌํ (4) ์์ธ

DIARY
๋ง์ง๋ง์ผ๋ก ์์ธํ์ด์ง๋ฅผ ๊ตฌํํ๋ ์ผ๋ง ๋จ์๋ค
๋น๊ต์ ์ฌ์ธ ๊ฑฐ ๊ฐ๋ค๊ณ ์๊ฐํ๋ฉฐ ์์..!
์ผ๋จ diary.js ์์๋ ํ ์ผ์ด ์๋ค.
ํ์ฌ ์กฐํํ๊ณ ์ ํ๋ ์ผ๊ธฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ผ ํจ.
Edit์์ ํ๋ ๊ฒ์ฒ๋ผ path variable๋ก ๋ฐ์ ์ผ๊ธฐ id๋ฅผ ๊ฐ์ง๊ณ
DiaryStateContext๊ฐ ๊ณต๊ธํ๋ ์ผ๊ธฐ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ง๊ณ find ๋ฉ์๋๋ฅผ ์ด์ฉํด
ํ์ฌ ์ฐพ์ผ๋ ค๊ณ ํ๋ ์ผ๊ธฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ตฌํํ๋ ๊ณผ์ ์ ๋ ์ฐ๋ฉด ๋จ.
import { useContext, useEffect } from "react";
import { useParams } from "react-router-dom";
import { DiaryStateContext } from "../App";
const Diary = () => {
const { id } = useParams();
const diaryList = useContext(DiaryStateContext);
useEffect(() => {
if (diaryList.length >= 1) {
const targetDiary = diaryList.find(
(it) => parseInt(it.id) === parseInt(id)
);
console.log(targetDiary);
}
}, [id, diaryList]);
...

๋ง์ฐฌ๊ฐ์ง๋ก targetDiary๊ฐ ์ ๋ถ๋ฌ์ ์ง๋ค.
const [data, setData] = useState();
...
if (targetDiary) {
setData(targetDiary);
} else {
alert("์๋ ์ผ๊ธฐ์
๋๋ค.");
navigate("/", { replace: true });
}
...
๋ฅผ useEffect์ ์ถ๊ฐํด setData๋ ์ค์ ํด์ฃผ๊ณ ๋๋ฉด

id๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๊ฐ์ง๊ณ ์จ๋ค.
์ด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ ๋ฟ๋ ค์ฃผ๊ธฐ๋ง ํ๋ฉด ๋จ
header
ํค๋์๋ ํด๋น ์ผ๊ธฐ์ ๋ ์ง๋ฅผ ๋ฃ๊ณ ์ถ์๋ฐ, ๊ทธ๋ฌ๋ ค๋ฉด
์์ ์ eidtor์์ ๋ง๋ค์๋ getStringDate ํจ์๋ฅผ ๋ถ๋ฌ์ค๋ฉด ๋๋ค.
const getStringDate = (date) => {
return date.toISOString().slice(0, 10);
};
๊ทธ๋์ ํธํ๊ฒ ์ฐ๊ธฐ ์ํด ์ด๊ฒ์ ๋ณต์ฌํด์ค๋๋ฐ...
์ฌ์ค ์ด๋ฐ ํจ์์ฝ๋๋ฅผ ์ค๋ณต์ผ๋ก ์ฐ๋๊ฑด ๋ณ๋ก ์ข์ง ์๋ค๊ณ ํจ
๊ทธ๋์ ๋ฐ๋ก ํ์ผ์ ํ๋ ๋ง๋ค์ด ์ค ํ export const~ ํด์ฃผ๊ณ ,
์ฌ์ฉํ ํ์ผ์์ import ํด์ฃผ๋ฉด ๋จ.
<MyHeader headtext={`${getStringDate(new Date(data.date))} ์ผ๊ธฐ`} />

๊ทผ๋ฐ ์ด ๋ชจ์์ด ๋ง์์ ์๋ค์ด์
getStringDate๋ ์ฐ์ง ์๊ณ ,
<MyHeader
headtext={`${new Date(data.date).getFullYear()}๋
${new Date(data.date).getMonth() + 1}์
${new Date(data.date).getDate()}์ผ ์ผ๊ธฐ`}
/>
์ด๋ ๊ฒ ์ ์ด์ ์ด๋ฐ ํ์ดํ๋ก ๋ฐ๊พธ์๋น ใ

์กฐ๊ธ ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์์๊น? ๋ ์ ๋ชจ๋ฅด๊ฒ ์ง๋ง...
์๋ฌดํผ ๋ฒํผ๋ค๊น์ง ๋ฃ์ผ๋ฉด ํค๋ ์์ฑ
<MyHeader
headtext={`${new Date(data.date).getFullYear()}๋
${
new Date(data.date).getMonth() + 1
}์ ${new Date(data.date).getDate()}์ผ ์ผ๊ธฐ`}
leftchild={
<MyButton text={"< ๋ค๋ก ๊ฐ๊ธฐ"} onClick={() => navigate(-1)} />
}
rightchild={
<MyButton
text={"์์ ํ๊ธฐ"}
type={"positive"}
onClick={() => navigate(`/edit/${id}`)}
/>
}
/>
๊ตณ์ด ์ ์ ์ผ๋ ค๋ค๊ฐ navigate(`/edit/${id}`) ์ด ๋ถ๋ถ ํ ๋ฒ ๋ ๋ณด๋ ค๊ณ ๋ฃ์!
emotion ๋ถ๋ฌ์ค๊ธฐ
dairyEditor์์ ์ผ๋ emotion ๋ค์ ๋ ์ฐ๊ธฐ์ํด
์ด๊ฒ๋ค๋ util๋ก ๊บผ๋ด๋๊ณ ๋ค์ ๋ถ๋ฌ์์ ์ฌ์ฉํ๋ค.
util/emotion.js
export const emotionList = [
{
emotion_id: 1,
emotion_img: process.env.PUBLIC_URL + `/assets/emotion1.png`,
emotion_descript: "๋งค์ฐ ์ข์",
},
{
emotion_id: 2,
emotion_img: process.env.PUBLIC_URL + `/assets/emotion2.png`,
emotion_descript: "์ข์",
},
{
emotion_id: 3,
emotion_img: process.env.PUBLIC_URL + `/assets/emotion3.png`,
emotion_descript: "๋ณดํต",
},
{
emotion_id: 4,
emotion_img: process.env.PUBLIC_URL + `/assets/emotion4.png`,
emotion_descript: "๋์จ",
},
{
emotion_id: 5,
emotion_img: process.env.PUBLIC_URL + `/assets/emotion5.png`,
emotion_descript: "๋์ฐํจ",
},
];
Diary.js
import { emotionList } from "../util/emotion";
...
if (!data) {
return <div className="DiaryPage">๋ก๋ฉ ์ค์
๋๋ค...</div>;
} else {
const curEmotionData = emotionList.find(
(it) => parseInt(it.emotion_id) === parseInt(data.emotion)
);
console.log(curEmotionData);
curEmotionData๊ฐ ์ ๋ค์ด์ค๋์ง ํ์ธํ๋ค

<section>
<h4>์ค๋์ ๊ฐ์ </h4>
<div className="diary_img_wrap">
<img src={curEmotionData.emotion_img} />
<div className="emotion_descript">
{curEmotionData.emotion_descript}
</div>
</div>
</section>
์ด์ ๋ชจ ๋์ถฉ ์๋์๋ ๋ฟ๋ ค์ฃผ๋ฉด ์๋์๋ ๋์ค์ง~

์ด์ css ๋์ถฉ ๋์ถฉ ์ด์ด ๊ฑด๋๋ ค์ฃผ๋ฉด
<div className={[
"diary_img_wrap",
`diary_img_wrap_${data.emotion}`,
].join(" ")}
>
.DiaryPage .emotion_descript {
font-size: 20px;
color: #c4c4c4;
font-weight: bold;
position: absolute;
top: 400px;
}
.DiaryPage .diary_img_wrap {
background-image: url("./../public/assets/back.png");
background-size: 600px 300px;
width: 600px;
height: 300px;
border-radius: 5px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.DiaryPage .diary_img_wrap_1 img {
width: 60px;
margin: 0 0 150px 0;
}
.DiaryPage .diary_img_wrap_2 img {
width: 60px;
margin: 40px 100px 150px 0;
}
...
์ฌ์ค descript ์์น๋ฅผ ์ ํ๋๋ฐ ์ ๋จน์๋ค.
๊ทธ๋ฆผ์ margin์ผ๋ก ์ฃผ๋ค๋ณด๋ ๊ฐ์ div ๋ด์ ์๋ descript๊ฐ ๊ฐ์ด ์์ง์ฌ์...
๊ทธ๋์ ์ ๋ ์์น๋ก ์ฃผ๊ณ ์ธ๋ก์์น๋ง ์ฃผ์๋ค!
๊ทธ๋ฌ๋๋ ์ฐฝํฌ๊ธฐ์ ์๊ด์์ด ๊ฐ์ ์์น์ ์์์

์์ฃผ์์ฃผ ๋ง์กฑ์ค๋ฝ๊ตฌ๋ง
๋ด์ฉ ๋ถ๋ฌ์ค๊ธฐ
<section>
<h4>์ค๋์ ์ผ๊ธฐ</h4>
<div className="diary_content_wrap">
<p>{data.content}</p>
</div>
</section>
.DiaryPage .diary_content_wrap p {
padding: 20px;
text-align: left;
}
์ด๋ ๊ฒ ํด๋ ๊ธ๋ค์ด ์ผ์ชฝ ์ ๋ ฌ๋ก ๋์ง ์๋๋ฐ,
์ด๋ ๋ถ๋ชจ์์๊ฐ 100%์ width๋ฅผ ๊ฐ์ง์ง ๋ชปํด์ ๊ทธ๋ ๋ค

.DiaryPage .diary_content_wrap {
width: 100%;
}
๊ทธ๋ผ ์ด๋ ๊ฒ ์์ ์์์ 100%๋ฅผ ๋จน์ฌ์ฃผ๋ฉด ๋ฐ๋ก ์ผ์ชฝ ์ ๋ ฌ์ด ๋จ!
์ถ๊ฐ css
- word-break: keep-all : ํ ์คํธ๊ฐ ๊ธธ์ด์ ธ์ ์ค์ ๋ฐ๊ฟ์ผํ๋ ์ํฉ์ด ์์ ๋ ๋จ์ด๋ฅผ ์ชผ๊ฐ์ง ์๋ ์์ฑ
- overflow-wrap: break-word : ์ด wrap์ด ๋๋ฌด ๊ธธ์ด์ ๋์ด๊ฐ ๊ฒฝ์ฐ ์ค ๋ฐ๊ฟ์ ํด์ค
- line-height : ์ค ๊ฐ๊ฒฉ์ ์ ํ๋ ์์ฑ
์ถ๊ฐ ์์
์ถ๊ฐ ์์ ์ผ๋ก ์ผ๋จ, ์์ ํ๊ณ ๋๋ฉด list๋ก ๊ฐ์ง ์๊ณ diary detail ํ๋ฉด์ผ๋ก ๊ฐ๊ฒ ํ๋ค
if (!isEdit) {
onCreate(date, content, emotion);
navigate("/", { replace: true });
} else {
onEdit(originData.id, date, content, emotion);
navigate(`/diary/${originData.id}`, { replace: true });
}
}
css ์ ์ฉํ ๊ฑฐ๋ ์์ ํ ๊ฒ๋ค ํ ๋ฒ์ ๋ณด๋ฉด ์ด๋ฐ ๋๋!๊ธ์ธ ๋ ์ ์ฉํด๋ณด๋ค ๋์ด์ฐ๊ธฐ ์์ ํ๋ ๋ฐ ์ ๊ทธ๋ฌ๋์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง,,
'์ ์ฉํด ๋ณด๋ค'๊ฐ ๋ง๋ค.. ํฌํ .. ๋ณธ์ฉ์ธ(ํ์์ด)+๋ณด์กฐ์ฉ์ธ์ด๊ธฐ ๋๋ฌธ์ ๋ถ์ฌ ์ธ ์ ์์..
