2023-05-15 06:05:07 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2023-03-29 20:31:53 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-05-15 06:05:07 +00:00
|
|
|
import { mutate } from "swr";
|
|
|
|
import { DragDropContext, DropResult } from "react-beautiful-dnd";
|
2023-03-29 20:31:53 +00:00
|
|
|
// services
|
|
|
|
import issuesService from "services/issues.service";
|
2023-05-15 06:05:07 +00:00
|
|
|
// hooks
|
|
|
|
import useCalendarIssuesView from "hooks/use-calendar-issues-view";
|
|
|
|
// components
|
|
|
|
import { SingleCalendarDate, CalendarHeader } from "components/core";
|
2023-09-13 14:03:58 +00:00
|
|
|
import { IssuePeekOverview } from "components/issues";
|
2023-05-15 06:05:07 +00:00
|
|
|
// ui
|
|
|
|
import { Spinner } from "components/ui";
|
|
|
|
// helpers
|
2023-05-10 20:57:14 +00:00
|
|
|
import { renderDateFormat } from "helpers/date-time.helper";
|
|
|
|
import {
|
|
|
|
startOfWeek,
|
|
|
|
lastDayOfWeek,
|
|
|
|
eachDayOfInterval,
|
|
|
|
weekDayInterval,
|
|
|
|
formatDate,
|
|
|
|
} from "helpers/calendar.helper";
|
2023-05-15 06:05:07 +00:00
|
|
|
// types
|
2023-06-06 16:06:00 +00:00
|
|
|
import { ICalendarRange, ICurrentUserResponse, IIssue, UserAuth } from "types";
|
2023-05-15 06:05:07 +00:00
|
|
|
// fetch-keys
|
|
|
|
import {
|
|
|
|
CYCLE_ISSUES_WITH_PARAMS,
|
|
|
|
MODULE_ISSUES_WITH_PARAMS,
|
|
|
|
PROJECT_ISSUES_LIST_WITH_PARAMS,
|
|
|
|
VIEW_ISSUES,
|
|
|
|
} from "constants/fetch-keys";
|
2023-03-29 20:31:53 +00:00
|
|
|
|
2023-04-20 08:41:11 +00:00
|
|
|
type Props = {
|
2023-07-26 12:21:26 +00:00
|
|
|
handleIssueAction: (issue: IIssue, action: "copy" | "delete" | "edit") => void;
|
2023-04-20 08:41:11 +00:00
|
|
|
addIssueToDate: (date: string) => void;
|
2023-07-26 12:21:26 +00:00
|
|
|
disableUserActions: boolean;
|
2023-06-06 16:06:00 +00:00
|
|
|
user: ICurrentUserResponse | undefined;
|
2023-05-10 20:57:14 +00:00
|
|
|
userAuth: UserAuth;
|
2023-04-20 08:41:11 +00:00
|
|
|
};
|
|
|
|
|
2023-05-10 20:57:14 +00:00
|
|
|
export const CalendarView: React.FC<Props> = ({
|
2023-07-26 12:21:26 +00:00
|
|
|
handleIssueAction,
|
2023-05-10 20:57:14 +00:00
|
|
|
addIssueToDate,
|
2023-07-26 12:21:26 +00:00
|
|
|
disableUserActions,
|
2023-06-06 16:06:00 +00:00
|
|
|
user,
|
2023-05-10 20:57:14 +00:00
|
|
|
userAuth,
|
|
|
|
}) => {
|
2023-04-24 12:40:44 +00:00
|
|
|
const [showWeekEnds, setShowWeekEnds] = useState(false);
|
2023-09-27 09:11:32 +00:00
|
|
|
|
|
|
|
const { calendarIssues, mutateIssues, params, activeMonthDate, setActiveMonthDate } =
|
|
|
|
useCalendarIssuesView();
|
2023-03-29 20:31:53 +00:00
|
|
|
|
2023-05-15 06:05:07 +00:00
|
|
|
const [calendarDates, setCalendarDates] = useState<ICalendarRange>({
|
2023-09-27 09:11:32 +00:00
|
|
|
startDate: startOfWeek(activeMonthDate),
|
|
|
|
endDate: lastDayOfWeek(activeMonthDate),
|
2023-03-29 20:31:53 +00:00
|
|
|
});
|
|
|
|
|
2023-05-15 06:05:07 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId, cycleId, moduleId, viewId } = router.query;
|
|
|
|
|
2023-09-27 09:11:32 +00:00
|
|
|
const currentViewDays = showWeekEnds
|
|
|
|
? eachDayOfInterval({
|
|
|
|
start: calendarDates.startDate,
|
|
|
|
end: calendarDates.endDate,
|
|
|
|
})
|
|
|
|
: weekDayInterval({
|
|
|
|
start: calendarDates.startDate,
|
|
|
|
end: calendarDates.endDate,
|
|
|
|
});
|
2023-03-29 20:31:53 +00:00
|
|
|
|
|
|
|
const currentViewDaysData = currentViewDays.map((date: Date) => {
|
|
|
|
const filterIssue =
|
2023-05-15 06:05:07 +00:00
|
|
|
calendarIssues.length > 0
|
2023-04-24 12:40:44 +00:00
|
|
|
? calendarIssues.filter(
|
2023-03-29 20:31:53 +00:00
|
|
|
(issue) =>
|
|
|
|
issue.target_date && renderDateFormat(issue.target_date) === renderDateFormat(date)
|
|
|
|
)
|
|
|
|
: [];
|
|
|
|
return {
|
|
|
|
date: renderDateFormat(date),
|
|
|
|
issues: filterIssue,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const weeks = ((date: Date[]) => {
|
|
|
|
const weeks = [];
|
|
|
|
if (showWeekEnds) {
|
|
|
|
for (let day = 0; day <= 6; day++) {
|
|
|
|
weeks.push(date[day]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let day = 0; day <= 4; day++) {
|
|
|
|
weeks.push(date[day]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return weeks;
|
|
|
|
})(currentViewDays);
|
|
|
|
|
|
|
|
const onDragEnd = (result: DropResult) => {
|
|
|
|
const { source, destination, draggableId } = result;
|
|
|
|
|
|
|
|
if (!destination || !workspaceSlug || !projectId) return;
|
2023-05-15 06:05:07 +00:00
|
|
|
|
2023-03-29 20:31:53 +00:00
|
|
|
if (source.droppableId === destination.droppableId) return;
|
|
|
|
|
2023-03-30 21:13:38 +00:00
|
|
|
const fetchKey = cycleId
|
2023-05-15 06:05:07 +00:00
|
|
|
? CYCLE_ISSUES_WITH_PARAMS(cycleId.toString(), params)
|
2023-03-30 21:13:38 +00:00
|
|
|
: moduleId
|
2023-05-15 06:05:07 +00:00
|
|
|
? MODULE_ISSUES_WITH_PARAMS(moduleId.toString(), params)
|
|
|
|
: viewId
|
|
|
|
? VIEW_ISSUES(viewId.toString(), params)
|
|
|
|
: PROJECT_ISSUES_LIST_WITH_PARAMS(projectId.toString(), params);
|
2023-03-30 21:13:38 +00:00
|
|
|
|
|
|
|
mutate<IIssue[]>(
|
|
|
|
fetchKey,
|
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((p) => {
|
|
|
|
if (p.id === draggableId)
|
|
|
|
return {
|
|
|
|
...p,
|
|
|
|
target_date: destination.droppableId,
|
|
|
|
};
|
2023-05-15 06:05:07 +00:00
|
|
|
|
2023-03-30 21:13:38 +00:00
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
2023-03-29 20:31:53 +00:00
|
|
|
|
2023-05-15 06:05:07 +00:00
|
|
|
issuesService
|
2023-06-06 16:06:00 +00:00
|
|
|
.patchIssue(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
draggableId,
|
|
|
|
{
|
|
|
|
target_date: destination?.droppableId,
|
|
|
|
},
|
|
|
|
user
|
|
|
|
)
|
2023-05-15 06:05:07 +00:00
|
|
|
.then(() => mutate(fetchKey));
|
|
|
|
};
|
|
|
|
|
2023-09-27 09:11:32 +00:00
|
|
|
useEffect(() => {
|
2023-05-15 06:05:07 +00:00
|
|
|
setCalendarDates({
|
2023-09-27 09:11:32 +00:00
|
|
|
startDate: startOfWeek(activeMonthDate),
|
|
|
|
endDate: lastDayOfWeek(activeMonthDate),
|
2023-09-12 16:57:15 +00:00
|
|
|
});
|
2023-09-27 09:11:32 +00:00
|
|
|
}, [activeMonthDate]);
|
2023-05-15 06:05:07 +00:00
|
|
|
|
2023-07-26 12:21:26 +00:00
|
|
|
const isNotAllowed = userAuth.isGuest || userAuth.isViewer || disableUserActions;
|
2023-03-29 20:31:53 +00:00
|
|
|
|
2023-09-13 14:03:58 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<IssuePeekOverview
|
|
|
|
handleMutation={() => mutateIssues()}
|
|
|
|
projectId={projectId?.toString() ?? ""}
|
|
|
|
workspaceSlug={workspaceSlug?.toString() ?? ""}
|
|
|
|
readOnly={disableUserActions}
|
|
|
|
/>
|
|
|
|
{calendarIssues ? (
|
|
|
|
<div className="h-full overflow-y-auto">
|
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
2023-09-26 12:05:51 +00:00
|
|
|
<div
|
2023-09-27 03:21:29 +00:00
|
|
|
id={`calendar-view-${cycleId ?? moduleId ?? viewId ?? ""}`}
|
2023-09-26 12:05:51 +00:00
|
|
|
className="h-full rounded-lg p-8 text-custom-text-200"
|
|
|
|
>
|
2023-09-13 14:03:58 +00:00
|
|
|
<CalendarHeader
|
|
|
|
showWeekEnds={showWeekEnds}
|
|
|
|
setShowWeekEnds={setShowWeekEnds}
|
2023-09-27 09:11:32 +00:00
|
|
|
currentDate={activeMonthDate}
|
|
|
|
setCurrentDate={setActiveMonthDate}
|
2023-09-13 14:03:58 +00:00
|
|
|
/>
|
|
|
|
|
2023-05-10 20:57:14 +00:00
|
|
|
<div
|
2023-09-13 14:03:58 +00:00
|
|
|
className={`grid auto-rows-[minmax(36px,1fr)] rounded-lg ${
|
|
|
|
showWeekEnds ? "grid-cols-7" : "grid-cols-5"
|
2023-05-10 20:57:14 +00:00
|
|
|
}`}
|
2023-03-29 20:31:53 +00:00
|
|
|
>
|
2023-09-13 14:03:58 +00:00
|
|
|
{weeks.map((date, index) => (
|
|
|
|
<div
|
|
|
|
key={index}
|
2023-09-27 09:11:32 +00:00
|
|
|
className={`flex items-center justify-start gap-2 border-custom-border-200 bg-custom-background-90 p-1.5 text-base font-medium text-custom-text-200`}
|
2023-09-13 14:03:58 +00:00
|
|
|
>
|
2023-09-27 09:11:32 +00:00
|
|
|
<span>{formatDate(date, "eee").substring(0, 3)}</span>
|
2023-09-13 14:03:58 +00:00
|
|
|
</div>
|
|
|
|
))}
|
2023-03-29 20:31:53 +00:00
|
|
|
</div>
|
2023-09-13 14:03:58 +00:00
|
|
|
|
|
|
|
<div
|
2023-09-27 09:11:32 +00:00
|
|
|
className={`grid h-full auto-rows-min ${
|
2023-09-13 14:03:58 +00:00
|
|
|
showWeekEnds ? "grid-cols-7" : "grid-cols-5"
|
|
|
|
} `}
|
|
|
|
>
|
|
|
|
{currentViewDaysData.map((date, index) => (
|
|
|
|
<SingleCalendarDate
|
|
|
|
key={`${date}-${index}`}
|
|
|
|
index={index}
|
|
|
|
date={date}
|
|
|
|
handleIssueAction={handleIssueAction}
|
|
|
|
addIssueToDate={addIssueToDate}
|
|
|
|
showWeekEnds={showWeekEnds}
|
|
|
|
user={user}
|
|
|
|
isNotAllowed={isNotAllowed}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</DragDropContext>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="flex h-full w-full items-center justify-center">
|
|
|
|
<Spinner />
|
2023-03-29 20:31:53 +00:00
|
|
|
</div>
|
2023-09-13 14:03:58 +00:00
|
|
|
)}
|
|
|
|
</>
|
2023-03-29 20:31:53 +00:00
|
|
|
);
|
|
|
|
};
|