Skip to content

Commit

Permalink
feat: 댓글 시간 포매팅 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jul 7, 2024
1 parent 8f0e149 commit 4062c4b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
26 changes: 15 additions & 11 deletions src/components/ItemComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ReactComponent as GoBackIcon } from '../assets/ic_back.svg';
import emptyCommentPanda from "../assets/Img_inquiry_empty.png"
import { useNavigate } from "react-router-dom";
import LoadingErrorHandler from "./LoadingErrorHandler";
import formatComparedTime from "../utils/formatComparedTime";

const COMMENTS_LIMIT = 5;

Expand Down Expand Up @@ -60,18 +61,21 @@ function Comments({ comments }) {

return (
<ul className={styles.commentsList}>
{comments.map((comment) =>
<li className={styles.comment} key={comment.id}>
<span className={styles.commentContent}>{comment.content}</span>
<div className={styles.commentInfo}>
<img src={comment.writer.image} alt={comment.writer.nickname} className={styles.commentInfoImage} />
<div className={styles.commentInfoTexts}>
<span className={styles.commentInfoNickname}>{comment.writer.nickname}</span>
<span className={styles.commentInfoUpdatedAt}>{comment.updatedAt}</span>
{comments.map((comment) => {
const dateFormat = formatComparedTime(comment.updatedAt);

return (
<li className={styles.comment} key={comment.id}>
<span className={styles.commentContent}>{comment.content}</span>
<div className={styles.commentInfo}>
<img src={comment.writer.image} alt={comment.writer.nickname} className={styles.commentInfoImage} />
<div className={styles.commentInfoTexts}>
<span className={styles.commentInfoNickname}>{comment.writer.nickname}</span>
<span className={styles.commentInfoUpdatedAt}>{dateFormat}</span>
</div>
</div>
</div>
</li>
)}
</li>
)})}
</ul>
)
}
Expand Down
2 changes: 0 additions & 2 deletions src/components/ItemInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { ReactComponent as HeartIcon } from '../assets/ic_heart.svg';

function ItemInfo({ item }) {

console.log(item.images);

return (
<section className={styles.itemInfo}>
<div className={styles.itemImageContainer}>
Expand Down
8 changes: 8 additions & 0 deletions src/utils/compareTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function compareTime(targetTime) {
const target = Date.parse(targetTime);
const now = Date.now();

return now - target;
}

export default compareTime;
36 changes: 36 additions & 0 deletions src/utils/formatComparedTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import compareTime from "./compareTime";

const SEC = 1000;
const MIN = 60 * SEC;
const HOUR = 60 * MIN;
const DAY = 24 * HOUR;

function fitDigits(num) {
if (num < 10) return `0${num}`;
else return num;
}

function formatComparedTime(targetTime) {
const comparedTime = compareTime(targetTime);

if(comparedTime > 7 * DAY) {
const date = new Date(targetTime);
const hour = fitDigits(date.getHours());
const min = fitDigits(date.getMinutes());
const dateFormat = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}${hour}:${min}`;

return dateFormat;
}

else if(comparedTime > 2 * DAY) return `${comparedTime / DAY}일 전`;

else if(comparedTime > DAY) return "어제";

else if(comparedTime > HOUR) return `${comparedTime / HOUR}시간 전`;

else if(comparedTime > MIN) return `${compareTime / MIN}분 전`;

else return "방금 전";
}

export default formatComparedTime;

0 comments on commit 4062c4b

Please sign in to comment.