2023-01-30 14:05:18 +00:00
|
|
|
import { createContext, useCallback, useEffect, useReducer } from "react";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
import useSWR, { mutate } from "swr";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
// components
|
|
|
|
import ToastAlert from "components/toast-alert";
|
|
|
|
// services
|
|
|
|
import projectService from "services/project.service";
|
2023-03-16 08:37:19 +00:00
|
|
|
import viewsService from "services/views.service";
|
2023-01-26 18:12:20 +00:00
|
|
|
// types
|
2023-03-15 06:14:44 +00:00
|
|
|
import { IIssueFilterOptions, IProjectMember, NestedKeyOf } from "types";
|
2023-01-26 18:12:20 +00:00
|
|
|
// fetch-keys
|
2023-03-15 06:14:44 +00:00
|
|
|
import {
|
|
|
|
CYCLE_ISSUES_WITH_PARAMS,
|
|
|
|
MODULE_ISSUES_WITH_PARAMS,
|
|
|
|
PROJECT_ISSUES_LIST_WITH_PARAMS,
|
|
|
|
USER_PROJECT_VIEW,
|
2023-03-16 08:37:19 +00:00
|
|
|
VIEW_DETAILS,
|
|
|
|
VIEW_ISSUES,
|
2023-03-15 06:14:44 +00:00
|
|
|
} from "constants/fetch-keys";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
export const issueViewContext = createContext<ContextType>({} as ContextType);
|
|
|
|
|
|
|
|
type IssueViewProps = {
|
2023-03-15 06:14:44 +00:00
|
|
|
issueView: "list" | "kanban";
|
|
|
|
groupByProperty: "state" | "priority" | "labels" | null;
|
|
|
|
orderBy: "created_at" | "updated_at" | "priority" | "sort_order";
|
|
|
|
filters: IIssueFilterOptions;
|
2023-01-26 18:12:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type ReducerActionType = {
|
|
|
|
type:
|
2023-01-30 14:05:18 +00:00
|
|
|
| "REHYDRATE_THEME"
|
2023-01-26 18:12:20 +00:00
|
|
|
| "SET_ISSUE_VIEW"
|
|
|
|
| "SET_ORDER_BY_PROPERTY"
|
2023-03-15 06:14:44 +00:00
|
|
|
| "SET_FILTERS"
|
2023-01-26 18:12:20 +00:00
|
|
|
| "SET_GROUP_BY_PROPERTY"
|
|
|
|
| "RESET_TO_DEFAULT";
|
|
|
|
payload?: Partial<IssueViewProps>;
|
|
|
|
};
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
type ContextType = IssueViewProps & {
|
|
|
|
setGroupByProperty: (property: "state" | "priority" | "labels" | null) => void;
|
|
|
|
setOrderBy: (property: "created_at" | "updated_at" | "priority" | "sort_order") => void;
|
|
|
|
setFilters: (filters: Partial<IIssueFilterOptions>) => void;
|
2023-01-26 18:12:20 +00:00
|
|
|
resetFilterToDefault: () => void;
|
|
|
|
setNewFilterDefaultView: () => void;
|
|
|
|
setIssueViewToKanban: () => void;
|
|
|
|
setIssueViewToList: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
type StateType = {
|
2023-03-15 06:14:44 +00:00
|
|
|
issueView: "list" | "kanban";
|
|
|
|
groupByProperty: "state" | "priority" | "labels" | null;
|
|
|
|
orderBy: "created_at" | "updated_at" | "priority" | "sort_order";
|
|
|
|
filters: IIssueFilterOptions;
|
2023-01-26 18:12:20 +00:00
|
|
|
};
|
|
|
|
type ReducerFunctionType = (state: StateType, action: ReducerActionType) => StateType;
|
|
|
|
|
|
|
|
export const initialState: StateType = {
|
|
|
|
issueView: "list",
|
|
|
|
groupByProperty: null,
|
2023-02-14 14:35:32 +00:00
|
|
|
orderBy: "created_at",
|
2023-03-15 06:14:44 +00:00
|
|
|
filters: {
|
|
|
|
type: null,
|
2023-03-16 08:37:19 +00:00
|
|
|
priority: null,
|
2023-03-15 06:14:44 +00:00
|
|
|
assignees: null,
|
|
|
|
labels: null,
|
2023-03-16 08:37:19 +00:00
|
|
|
state: null,
|
2023-03-15 06:14:44 +00:00
|
|
|
issue__assignees__id: null,
|
|
|
|
issue__labels__id: null,
|
|
|
|
},
|
2023-01-26 18:12:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const reducer: ReducerFunctionType = (state, action) => {
|
|
|
|
const { type, payload } = action;
|
|
|
|
|
|
|
|
switch (type) {
|
2023-01-30 14:05:18 +00:00
|
|
|
case "REHYDRATE_THEME": {
|
|
|
|
let collapsed: any = localStorage.getItem("collapsed");
|
|
|
|
collapsed = collapsed ? JSON.parse(collapsed) : false;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-30 14:05:18 +00:00
|
|
|
return { ...initialState, ...payload, collapsed };
|
|
|
|
}
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
case "SET_ISSUE_VIEW": {
|
|
|
|
const newState = {
|
|
|
|
...state,
|
|
|
|
issueView: payload?.issueView || "list",
|
|
|
|
};
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...newState,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case "SET_GROUP_BY_PROPERTY": {
|
|
|
|
const newState = {
|
|
|
|
...state,
|
|
|
|
groupByProperty: payload?.groupByProperty || null,
|
|
|
|
};
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...newState,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case "SET_ORDER_BY_PROPERTY": {
|
|
|
|
const newState = {
|
|
|
|
...state,
|
2023-03-15 06:14:44 +00:00
|
|
|
orderBy: payload?.orderBy || "created_at",
|
2023-01-26 18:12:20 +00:00
|
|
|
};
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...newState,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
case "SET_FILTERS": {
|
2023-01-26 18:12:20 +00:00
|
|
|
const newState = {
|
|
|
|
...state,
|
2023-03-15 06:14:44 +00:00
|
|
|
filters: {
|
|
|
|
...state.filters,
|
|
|
|
...payload,
|
|
|
|
},
|
2023-01-26 18:12:20 +00:00
|
|
|
};
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...newState,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case "RESET_TO_DEFAULT": {
|
|
|
|
return {
|
|
|
|
...initialState,
|
|
|
|
...payload,
|
|
|
|
};
|
|
|
|
}
|
2023-02-14 14:35:32 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
default: {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const saveDataToServer = async (workspaceSlug: string, projectID: string, state: any) => {
|
|
|
|
await projectService.setProjectView(workspaceSlug, projectID, {
|
|
|
|
view_props: state,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-16 08:37:19 +00:00
|
|
|
const sendFilterDataToServer = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectID: string,
|
|
|
|
viewId: string,
|
|
|
|
state: any
|
|
|
|
) => {
|
|
|
|
await viewsService.patchView(workspaceSlug, projectID, viewId, {
|
|
|
|
...state,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
const setNewDefault = async (workspaceSlug: string, projectId: string, state: any) => {
|
|
|
|
mutate<IProjectMember>(
|
|
|
|
workspaceSlug && projectId ? USER_PROJECT_VIEW(projectId as string) : null,
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
view_props: state,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
await projectService.setProjectView(workspaceSlug, projectId, {
|
2023-01-26 18:12:20 +00:00
|
|
|
view_props: state,
|
|
|
|
default_props: state,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueViewContextProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
|
|
|
|
|
|
const router = useRouter();
|
2023-03-16 08:37:19 +00:00
|
|
|
const { workspaceSlug, projectId, cycleId, moduleId, viewId } = router.query;
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
const { data: myViewProps, mutate: mutateMyViewProps } = useSWR(
|
|
|
|
workspaceSlug && projectId ? USER_PROJECT_VIEW(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => projectService.projectMemberMe(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-03-16 08:37:19 +00:00
|
|
|
const { data: viewDetails, mutate: mutateViewDetails } = useSWR(
|
|
|
|
workspaceSlug && projectId && viewId ? VIEW_DETAILS(viewId as string) : null,
|
|
|
|
workspaceSlug && projectId && viewId
|
|
|
|
? () =>
|
|
|
|
viewsService.getViewDetails(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
viewId as string
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const setIssueViewToKanban = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: "SET_ISSUE_VIEW",
|
|
|
|
payload: {
|
|
|
|
issueView: "kanban",
|
|
|
|
},
|
|
|
|
});
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
dispatch({
|
|
|
|
type: "SET_GROUP_BY_PROPERTY",
|
|
|
|
payload: {
|
2023-03-15 06:14:44 +00:00
|
|
|
groupByProperty: "state",
|
2023-01-26 18:12:20 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
saveDataToServer(workspaceSlug as string, projectId as string, {
|
|
|
|
...state,
|
|
|
|
issueView: "kanban",
|
2023-03-15 06:14:44 +00:00
|
|
|
groupByProperty: "state",
|
2023-01-26 18:12:20 +00:00
|
|
|
});
|
|
|
|
}, [workspaceSlug, projectId, state]);
|
|
|
|
|
|
|
|
const setIssueViewToList = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: "SET_ISSUE_VIEW",
|
|
|
|
payload: {
|
|
|
|
issueView: "list",
|
|
|
|
},
|
|
|
|
});
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
dispatch({
|
|
|
|
type: "SET_GROUP_BY_PROPERTY",
|
|
|
|
payload: {
|
|
|
|
groupByProperty: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
mutateMyViewProps((prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
view_props: {
|
|
|
|
...state,
|
|
|
|
issueView: "list",
|
|
|
|
groupByProperty: null,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, false);
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
saveDataToServer(workspaceSlug as string, projectId as string, {
|
|
|
|
...state,
|
|
|
|
issueView: "list",
|
|
|
|
groupByProperty: null,
|
|
|
|
});
|
2023-03-15 06:14:44 +00:00
|
|
|
}, [workspaceSlug, projectId, state, mutateMyViewProps]);
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
const setGroupByProperty = useCallback(
|
2023-03-15 06:14:44 +00:00
|
|
|
(property: "state" | "priority" | "labels" | null) => {
|
2023-01-26 18:12:20 +00:00
|
|
|
dispatch({
|
|
|
|
type: "SET_GROUP_BY_PROPERTY",
|
|
|
|
payload: {
|
|
|
|
groupByProperty: property,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
|
|
|
mutateMyViewProps((prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
view_props: {
|
|
|
|
...state,
|
|
|
|
groupByProperty: property,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, false);
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
saveDataToServer(workspaceSlug as string, projectId as string, {
|
|
|
|
...state,
|
|
|
|
groupByProperty: property,
|
|
|
|
});
|
|
|
|
},
|
2023-03-15 06:14:44 +00:00
|
|
|
[projectId, workspaceSlug, state, mutateMyViewProps]
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const setOrderBy = useCallback(
|
2023-03-15 06:14:44 +00:00
|
|
|
(property: "created_at" | "updated_at" | "priority" | "sort_order") => {
|
2023-01-26 18:12:20 +00:00
|
|
|
dispatch({
|
|
|
|
type: "SET_ORDER_BY_PROPERTY",
|
|
|
|
payload: {
|
|
|
|
orderBy: property,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
|
|
|
mutateMyViewProps((prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
view_props: {
|
|
|
|
...state,
|
|
|
|
orderBy: property,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, false);
|
|
|
|
|
2023-02-23 12:20:47 +00:00
|
|
|
saveDataToServer(workspaceSlug as string, projectId as string, {
|
|
|
|
...state,
|
|
|
|
orderBy: property,
|
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
},
|
2023-03-15 06:14:44 +00:00
|
|
|
[projectId, workspaceSlug, state, mutateMyViewProps]
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
|
2023-03-15 06:14:44 +00:00
|
|
|
const setFilters = useCallback(
|
|
|
|
(property: Partial<IIssueFilterOptions>) => {
|
2023-03-21 11:41:04 +00:00
|
|
|
Object.keys(property).forEach((key) => {
|
|
|
|
if (property[key as keyof typeof property]?.length === 0) {
|
|
|
|
property[key as keyof typeof property] = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
dispatch({
|
2023-03-15 06:14:44 +00:00
|
|
|
type: "SET_FILTERS",
|
2023-01-26 18:12:20 +00:00
|
|
|
payload: {
|
2023-03-15 06:14:44 +00:00
|
|
|
filters: {
|
|
|
|
...state.filters,
|
|
|
|
...property,
|
|
|
|
},
|
2023-01-26 18:12:20 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
|
|
|
mutateMyViewProps((prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
view_props: {
|
|
|
|
...state,
|
|
|
|
filters: {
|
|
|
|
...state.filters,
|
|
|
|
...property,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, false);
|
|
|
|
|
2023-03-16 08:37:19 +00:00
|
|
|
if (viewId) {
|
|
|
|
mutateViewDetails((prevData: any) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
query_data: {
|
|
|
|
...state.filters,
|
|
|
|
...property,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, false);
|
|
|
|
sendFilterDataToServer(workspaceSlug as string, projectId as string, viewId as string, {
|
|
|
|
query_data: {
|
|
|
|
...state.filters,
|
|
|
|
...property,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else
|
|
|
|
saveDataToServer(workspaceSlug as string, projectId as string, {
|
|
|
|
...state,
|
|
|
|
filters: {
|
|
|
|
...state.filters,
|
|
|
|
...property,
|
|
|
|
},
|
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
},
|
2023-03-16 08:37:19 +00:00
|
|
|
[projectId, workspaceSlug, state, mutateMyViewProps, viewId, mutateViewDetails]
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const setNewDefaultView = useCallback(() => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
setNewDefault(workspaceSlug as string, projectId as string, state).then(() => {
|
|
|
|
mutateMyViewProps();
|
|
|
|
});
|
|
|
|
}, [projectId, workspaceSlug, state, mutateMyViewProps]);
|
|
|
|
|
|
|
|
const resetToDefault = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: "RESET_TO_DEFAULT",
|
|
|
|
payload: myViewProps?.default_props,
|
|
|
|
});
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
if (!workspaceSlug || !projectId) return;
|
2023-03-15 06:14:44 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
saveDataToServer(workspaceSlug as string, projectId as string, myViewProps?.default_props);
|
|
|
|
}, [projectId, workspaceSlug, myViewProps]);
|
|
|
|
|
2023-01-30 14:05:18 +00:00
|
|
|
useEffect(() => {
|
|
|
|
dispatch({
|
|
|
|
type: "REHYDRATE_THEME",
|
2023-03-16 08:37:19 +00:00
|
|
|
payload: {
|
|
|
|
...myViewProps?.view_props,
|
|
|
|
filters: {
|
|
|
|
...myViewProps?.view_props?.filters,
|
|
|
|
...viewDetails?.query_data,
|
|
|
|
} as any,
|
|
|
|
},
|
2023-01-30 14:05:18 +00:00
|
|
|
});
|
2023-03-16 08:37:19 +00:00
|
|
|
}, [myViewProps, viewDetails]);
|
2023-01-30 14:05:18 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return (
|
|
|
|
<issueViewContext.Provider
|
|
|
|
value={{
|
|
|
|
issueView: state.issueView,
|
|
|
|
groupByProperty: state.groupByProperty,
|
|
|
|
setGroupByProperty,
|
|
|
|
orderBy: state.orderBy,
|
|
|
|
setOrderBy,
|
2023-03-15 06:14:44 +00:00
|
|
|
filters: state.filters,
|
|
|
|
setFilters,
|
2023-01-26 18:12:20 +00:00
|
|
|
resetFilterToDefault: resetToDefault,
|
|
|
|
setNewFilterDefaultView: setNewDefaultView,
|
|
|
|
setIssueViewToKanban,
|
|
|
|
setIssueViewToList,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ToastAlert />
|
|
|
|
{children}
|
|
|
|
</issueViewContext.Provider>
|
|
|
|
);
|
|
|
|
};
|