2023-09-27 09:11:32 +00:00
|
|
|
import { useContext, useState } from "react";
|
2023-05-15 06:05:07 +00:00
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// contexts
|
|
|
|
import { issueViewContext } from "contexts/issue-view.context";
|
|
|
|
// services
|
|
|
|
import issuesService from "services/issues.service";
|
|
|
|
import cyclesService from "services/cycles.service";
|
|
|
|
import modulesService from "services/modules.service";
|
2023-09-27 09:11:32 +00:00
|
|
|
// helpers
|
|
|
|
import { renderDateFormat } from "helpers/date-time.helper";
|
2023-05-15 06:05:07 +00:00
|
|
|
// types
|
|
|
|
import { IIssue } from "types";
|
|
|
|
// fetch-keys
|
|
|
|
import {
|
|
|
|
CYCLE_ISSUES_WITH_PARAMS,
|
|
|
|
MODULE_ISSUES_WITH_PARAMS,
|
|
|
|
PROJECT_ISSUES_LIST_WITH_PARAMS,
|
|
|
|
VIEW_ISSUES,
|
|
|
|
} from "constants/fetch-keys";
|
|
|
|
|
|
|
|
const useCalendarIssuesView = () => {
|
|
|
|
const {
|
2023-09-12 16:57:15 +00:00
|
|
|
display_filters: displayFilters,
|
2023-05-15 06:05:07 +00:00
|
|
|
filters,
|
|
|
|
setFilters,
|
|
|
|
resetFilterToDefault,
|
|
|
|
setNewFilterDefaultView,
|
|
|
|
} = useContext(issueViewContext);
|
|
|
|
|
2023-09-27 09:11:32 +00:00
|
|
|
const [activeMonthDate, setActiveMonthDate] = useState(new Date());
|
|
|
|
|
|
|
|
const firstDayOfMonth = new Date(activeMonthDate.getFullYear(), activeMonthDate.getMonth(), 1);
|
|
|
|
const lastDayOfMonth = new Date(activeMonthDate.getFullYear(), activeMonthDate.getMonth() + 1, 0);
|
|
|
|
|
2023-05-15 06:05:07 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId, cycleId, moduleId, viewId } = router.query;
|
|
|
|
|
|
|
|
const params: any = {
|
|
|
|
assignees: filters?.assignees ? filters?.assignees.join(",") : undefined,
|
|
|
|
state: filters?.state ? filters?.state.join(",") : undefined,
|
|
|
|
priority: filters?.priority ? filters?.priority.join(",") : undefined,
|
2023-09-12 16:57:15 +00:00
|
|
|
type: displayFilters?.type ? displayFilters?.type : undefined,
|
2023-05-15 06:05:07 +00:00
|
|
|
labels: filters?.labels ? filters?.labels.join(",") : undefined,
|
|
|
|
created_by: filters?.created_by ? filters?.created_by.join(",") : undefined,
|
2023-08-29 14:41:38 +00:00
|
|
|
start_date: filters?.start_date ? filters?.start_date.join(",") : undefined,
|
2023-09-27 09:11:32 +00:00
|
|
|
target_date: `${renderDateFormat(firstDayOfMonth)};after,${renderDateFormat(
|
|
|
|
lastDayOfMonth
|
|
|
|
)};before`,
|
2023-05-15 06:05:07 +00:00
|
|
|
};
|
|
|
|
|
2023-09-13 14:03:58 +00:00
|
|
|
const { data: projectCalendarIssues, mutate: mutateProjectCalendarIssues } = useSWR(
|
2023-05-15 06:05:07 +00:00
|
|
|
workspaceSlug && projectId
|
|
|
|
? PROJECT_ISSUES_LIST_WITH_PARAMS(projectId.toString(), params)
|
|
|
|
: null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () =>
|
|
|
|
issuesService.getIssuesWithParams(workspaceSlug.toString(), projectId.toString(), params)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-09-13 14:03:58 +00:00
|
|
|
const { data: cycleCalendarIssues, mutate: mutateCycleCalendarIssues } = useSWR(
|
2023-05-15 06:05:07 +00:00
|
|
|
workspaceSlug && projectId && cycleId
|
|
|
|
? CYCLE_ISSUES_WITH_PARAMS(cycleId.toString(), params)
|
|
|
|
: null,
|
|
|
|
workspaceSlug && projectId && cycleId
|
|
|
|
? () =>
|
|
|
|
cyclesService.getCycleIssuesWithParams(
|
|
|
|
workspaceSlug.toString(),
|
|
|
|
projectId.toString(),
|
|
|
|
cycleId.toString(),
|
|
|
|
params
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-09-13 14:03:58 +00:00
|
|
|
const { data: moduleCalendarIssues, mutate: mutateModuleCalendarIssues } = useSWR(
|
2023-05-15 06:05:07 +00:00
|
|
|
workspaceSlug && projectId && moduleId
|
|
|
|
? MODULE_ISSUES_WITH_PARAMS(moduleId.toString(), params)
|
|
|
|
: null,
|
|
|
|
workspaceSlug && projectId && moduleId
|
|
|
|
? () =>
|
|
|
|
modulesService.getModuleIssuesWithParams(
|
|
|
|
workspaceSlug.toString(),
|
|
|
|
projectId.toString(),
|
|
|
|
moduleId.toString(),
|
|
|
|
params
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-09-13 14:03:58 +00:00
|
|
|
const { data: viewCalendarIssues, mutate: mutateViewCalendarIssues } = useSWR(
|
2023-05-15 06:05:07 +00:00
|
|
|
workspaceSlug && projectId && viewId && params ? VIEW_ISSUES(viewId.toString(), params) : null,
|
|
|
|
workspaceSlug && projectId && viewId && params
|
|
|
|
? () =>
|
|
|
|
issuesService.getIssuesWithParams(workspaceSlug.toString(), projectId.toString(), params)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const calendarIssues = cycleId
|
|
|
|
? (cycleCalendarIssues as IIssue[])
|
|
|
|
: moduleId
|
|
|
|
? (moduleCalendarIssues as IIssue[])
|
|
|
|
: viewId
|
|
|
|
? (viewCalendarIssues as IIssue[])
|
|
|
|
: (projectCalendarIssues as IIssue[]);
|
|
|
|
|
|
|
|
return {
|
2023-09-27 09:11:32 +00:00
|
|
|
activeMonthDate,
|
|
|
|
setActiveMonthDate,
|
2023-05-15 06:05:07 +00:00
|
|
|
calendarIssues: calendarIssues ?? [],
|
2023-09-13 14:03:58 +00:00
|
|
|
mutateIssues: cycleId
|
|
|
|
? mutateCycleCalendarIssues
|
|
|
|
: moduleId
|
|
|
|
? mutateModuleCalendarIssues
|
|
|
|
: viewId
|
|
|
|
? mutateViewCalendarIssues
|
|
|
|
: mutateProjectCalendarIssues,
|
2023-05-15 06:05:07 +00:00
|
|
|
filters,
|
|
|
|
setFilters,
|
|
|
|
params,
|
|
|
|
resetFilterToDefault,
|
|
|
|
setNewFilterDefaultView,
|
|
|
|
} as const;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useCalendarIssuesView;
|