Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 대출제한일 계산 로직 통합 #536

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 33 additions & 10 deletions src/util/date.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { isNumber, isString } from "./typeCheck";
/* 기본적인 날짜표시 형식 20yy-mm-dd */
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])$/;

export const isFormattedDate = (string: string) => RegExp(dateReg).test(string);
export const dateFormat = (string: string) => {
if (isFormattedDate(string)) return string;
return string?.slice(0, 10)?.replace(".", "-") || "";
};

Expand All @@ -22,9 +21,16 @@ export const splitDate = (string: string) =>
?.map(v => parseInt(v)) || [];

/* string 형식의 날짜 비교 */
export const compareDate = (
date1: string,
date2 = nowDate,
compare: (date1: string, date2: string) => boolean,
) => {
return compare(dateFormat(date1), dateFormat(date2));
};

export const dateLessThan = (date: string, now = nowDate) => {
if (!isString(date) || !isString(now)) return undefined;
return dateFormat(date) < now;
return compareDate(date, now, (date1, date2) => date1 < date2);
};

/* 날짜 및 시간 계산 */
Expand All @@ -44,10 +50,27 @@ export const isExpiredDate = (expireDateString: string) => {
};

export const addDay = (num: number, date = nowDate) => {
if (!isString(date) || !isNumber(num)) return date;
const splited = splitDate(dateFormat(date));
if (!splited) return date;
const [year, month, day] = splited;
const dateObj = new Date(year, month - 1, day);
if (!isFormattedDate(date)) return date;
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 };
};