Project/Toy-project

๊ฐ์ •์ผ๊ธฐ์žฅ ๋งŒ๋“ค๊ธฐ 2 - CRUD ๊ธฐ๋Šฅ ๊ตฌํ˜„ (4) ์ƒ์„ธ

fairy_taIe 2023. 5. 19. 00:28

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๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž˜ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์™”์œผ๋‹ˆ ๋ฟŒ๋ ค์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋จ

 

 

ํ—ค๋”์—๋Š” ํ•ด๋‹น ์ผ๊ธฐ์˜ ๋‚ ์งœ๋ฅผ ๋„ฃ๊ณ  ์‹ถ์€๋ฐ, ๊ทธ๋Ÿฌ๋ ค๋ฉด
์˜ˆ์ „์— 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 ์ ์šฉํ•œ ๊ฑฐ๋ž‘ ์ˆ˜์ •ํ•œ ๊ฒƒ๋“ค ํ•œ ๋ฒˆ์— ๋ณด๋ฉด ์ด๋Ÿฐ ๋А๋‚Œ!
๊ธ€์“ธ ๋•Œ ์ ์šฉํ•ด๋ณด๋‹ค ๋„์–ด์“ฐ๊ธฐ ์ˆ˜์ •ํ•˜๋Š” ๋ฐ ์™œ ๊ทธ๋žฌ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ,,
'์ ์šฉํ•ด ๋ณด๋‹ค'๊ฐ€ ๋งž๋‹ค.. ํฌํ .. ๋ณธ์šฉ์–ธ(ํŒŒ์ƒ์–ด)+๋ณด์กฐ์šฉ์–ธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ™์—ฌ ์“ธ ์ˆ˜ ์—†์Œ..