Skip to content

Commit

Permalink
refactor: 대출제한일 계산 로직 변경 및 통합
Browse files Browse the repository at this point in the history
  • Loading branch information
not-using committed Aug 14, 2023
1 parent eb78104 commit b5dd738
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 65 deletions.
37 changes: 3 additions & 34 deletions src/component/mypage/Mypage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { myPageTabList } from "../../constant/tablist";
import { useTabFocus } from "../../hook/useTabFocus";
import { useNewDialog } from "../../hook/useNewDialog";
import { useGetUsersSearchId } from "../../api/users/useGetUsersSearchId";
import { lendingRestriction } from "../../util/date";
import MyRent from "./MyRentInfo/MyRent";
import MyReservation from "./MyReservation";
import MyReview from "./MyReview";
Expand Down Expand Up @@ -65,35 +66,6 @@ const Mypage = () => {
};
}, []);

const concatDate = (day: Date) => {
if (!userInfo) return "";
let overDueDate = "";
day.setDate(day.getDate() + userInfo.overDueDay);
overDueDate += day.getFullYear();
overDueDate += "-";
overDueDate +=
day.getMonth() + 1 >= 10
? day.getMonth() + 1
: "0".concat(`${day.getMonth() + 1}}`);
overDueDate += "-";
overDueDate +=
day.getDate() >= 10 ? day.getDate() : "0".concat(`${day.getDate()}`);
return overDueDate;
};

const getOverDueDate = () => {
if (
!userInfo ||
(userInfo &&
(!userInfo.penaltyEndDate ||
new Date(userInfo.penaltyEndDate).setHours(0, 0, 0, 0) <
new Date().setHours(0, 0, 0, 0)))
) {
return concatDate(new Date());
}
return concatDate(new Date(userInfo.penaltyEndDate));
};

return (
<>
{deviceMode === "desktop" && (
Expand Down Expand Up @@ -170,11 +142,8 @@ const Mypage = () => {
</span>
<span className="font-14-bold color-54">대출제한</span>
<span className="font-14">
{userInfo.overDueDay ||
(userInfo.penaltyEndDate &&
new Date(userInfo.penaltyEndDate).setHours(0, 0, 0, 0) >=
new Date().setHours(0, 0, 0, 0))
? `${getOverDueDate()} 까지`
{lendingRestriction(userInfo).isRestricted
? `${lendingRestriction(userInfo).restrictionDate} 까지`
: "-"}
</span>
<span className="font-14-bold color-54">정보수정</span>
Expand Down
35 changes: 4 additions & 31 deletions src/component/userManagement/UserBriefInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { User } from "../../type";
import { lendingRestriction } from "../../util/date";
import Image from "../utils/Image";
import UserUsage from "../../asset/img/book-arrow-right.svg";
import UserEdit from "../../asset/img/edit.svg";
import "../../asset/css/UserBriefInfo.css";
import { User } from "../../type";

const roles = ["미인증", "일반", "사서", "운영진"];
const USAGE = 1;
Expand All @@ -26,30 +27,7 @@ const UserBriefInfo = ({ user, line, setModal, setSelectedUser }: Props) => {
setModal(EDIT);
};

const concatDate = (day: Date) => {
let overDueDate = "";

day.setDate(day.getDate() + user.overDueDay);
overDueDate += day.getFullYear();
overDueDate += "-";
overDueDate += day.getMonth() + 1 < 10 ? "0" : "";
overDueDate += day.getMonth() + 1;
overDueDate += "-";
overDueDate += day.getDate() < 10 ? "0" : "";
overDueDate += day.getDate();
return overDueDate;
};

const getOverDueDate = () => {
if (
!user.penaltyEndDate ||
new Date(user.penaltyEndDate).setHours(0, 0, 0, 0) <
new Date().setHours(0, 0, 0, 0)
) {
return concatDate(nowDay);
}
return concatDate(new Date(user.penaltyEndDate));
};
const { isRestricted, restrictionDate } = lendingRestriction(user);

return (
<div className={`user-info ${line ? "user-info-line" : ""}`}>
Expand All @@ -68,12 +46,7 @@ const UserBriefInfo = ({ user, line, setModal, setSelectedUser }: Props) => {
)}
<div className="user-info__email font-18-bold color-54">{user.email}</div>
<div className="user-info__overdue font-18 color-54">
{user.overDueDay ||
(user.penaltyEndDate &&
new Date(user.penaltyEndDate).setHours(0, 0, 0, 0) >=
new Date().setHours(0, 0, 0, 0))
? getOverDueDate()
: "-"}
{isRestricted ? restrictionDate : "-"}
</div>
{user.nickname ? (
<button
Expand Down
22 changes: 22 additions & 0 deletions src/util/date.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { User } from "../type";

/* 기본적인 날짜표시 형식 20yy-mm-dd */
const dateReg = /^(20\d{2})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/;

Expand Down Expand Up @@ -52,3 +54,23 @@ export const addDay = (num: number, date = nowDate) => {
const dateObj = new Date(date);
return dateFormat(addDayDateObject(dateObj, num).toISOString());
};

/* lending 관련 날짜 함수 */

export const lendingRestriction = (user: User) => {
// 대출제한날짜는 이미 반납한 대출건의 연체제한 + 대출중인 도서의 연체일로 계산
const restrictionDate =
!user.penaltyEndDate || dateLessThan(user.penaltyEndDate)
? addDay(user.overDueDay) // 오늘 날짜 + 대출중인 도서를 오늘 반납시 받게 될 연체일
: addDay(user.overDueDay, user.penaltyEndDate);
// 이미 반납한 대출건의 연체 제한날짜 + 대출중인 도서를 오늘 반납시 받게 될 연체일

// 대출제한날짜가 현재 날짜보다 크면 대출제한
const isRestricted = compareDate(
restrictionDate,
nowDate,
(d1, d2) => d1 > d2,
);

return { isRestricted, restrictionDate };
};

0 comments on commit b5dd738

Please sign in to comment.