Skip to content

Commit

Permalink
feat: separate filters for cycles and modules (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryan610 committed Apr 19, 2023
1 parent 390b837 commit c5206a7
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 21 deletions.
152 changes: 134 additions & 18 deletions apps/app/contexts/issue-view.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import useSWR, { mutate } from "swr";
import ToastAlert from "components/toast-alert";
// services
import projectService from "services/project.service";
import cyclesService from "services/cycles.service";
import modulesService from "services/modules.service";
import viewsService from "services/views.service";
// types
import {
Expand All @@ -18,7 +20,12 @@ import {
TIssueOrderByOptions,
} from "types";
// fetch-keys
import { USER_PROJECT_VIEW, VIEW_DETAILS } from "constants/fetch-keys";
import {
CYCLE_DETAILS,
MODULE_DETAILS,
USER_PROJECT_VIEW,
VIEW_DETAILS,
} from "constants/fetch-keys";

export const issueViewContext = createContext<ContextType>({} as ContextType);

Expand Down Expand Up @@ -171,7 +178,29 @@ const saveDataToServer = async (workspaceSlug: string, projectID: string, state:
});
};

const sendFilterDataToServer = async (
const saveCycleFilters = async (
workspaceSlug: string,
projectId: string,
cycleId: string,
state: any
) => {
await cyclesService.patchCycle(workspaceSlug, projectId, cycleId, {
...state,
});
};

const saveModuleFilters = async (
workspaceSlug: string,
projectId: string,
moduleId: string,
state: any
) => {
await modulesService.patchModule(workspaceSlug, projectId, moduleId, {
...state,
});
};

const saveViewFilters = async (
workspaceSlug: string,
projectId: string,
viewId: string,
Expand Down Expand Up @@ -206,7 +235,7 @@ export const IssueViewContextProvider: React.FC<{ children: React.ReactNode }> =
const [state, dispatch] = useReducer(reducer, initialState);

const router = useRouter();
const { workspaceSlug, projectId, viewId } = router.query;
const { workspaceSlug, projectId, cycleId, moduleId, viewId } = router.query;

const { data: myViewProps, mutate: mutateMyViewProps } = useSWR(
workspaceSlug && projectId ? USER_PROJECT_VIEW(projectId as string) : null,
Expand All @@ -227,6 +256,30 @@ export const IssueViewContextProvider: React.FC<{ children: React.ReactNode }> =
: null
);

const { data: cycleDetails, mutate: mutateCycleDetails } = useSWR(
workspaceSlug && projectId && cycleId ? CYCLE_DETAILS(cycleId as string) : null,
workspaceSlug && projectId && cycleId
? () =>
cyclesService.getCycleDetails(
workspaceSlug.toString(),
projectId.toString(),
cycleId.toString()
)
: null
);

const { data: moduleDetails, mutate: mutateModuleDetails } = useSWR(
workspaceSlug && projectId && moduleId ? MODULE_DETAILS(moduleId.toString()) : null,
workspaceSlug && projectId && moduleId
? () =>
modulesService.getModuleDetails(
workspaceSlug.toString(),
projectId.toString(),
moduleId.toString()
)
: null
);

const setIssueView = useCallback(
(property: TIssueViewOptions) => {
dispatch({
Expand Down Expand Up @@ -352,9 +405,8 @@ export const IssueViewContextProvider: React.FC<{ children: React.ReactNode }> =
const setFilters = useCallback(
(property: Partial<IIssueFilterOptions>, saveToServer = true) => {
Object.keys(property).forEach((key) => {
if (property[key as keyof typeof property]?.length === 0) {
if (property[key as keyof typeof property]?.length === 0)
property[key as keyof typeof property] = null;
}
});

dispatch({
Expand All @@ -369,22 +421,53 @@ export const IssueViewContextProvider: React.FC<{ children: React.ReactNode }> =

if (!workspaceSlug || !projectId) return;

mutateMyViewProps((prevData) => {
if (!prevData) return prevData;
if (cycleId) {
mutateCycleDetails((prevData: any) => {
if (!prevData) return prevData;

return {
...prevData,
return {
...prevData,
view_props: {
filters: {
...state.filters,
...property,
},
},
};
}, false);

saveCycleFilters(workspaceSlug.toString(), projectId.toString(), cycleId.toString(), {
view_props: {
...state,
filters: {
...state.filters,
...property,
},
},
};
}, false);
});
} else if (moduleId) {
mutateModuleDetails((prevData: any) => {
if (!prevData) return prevData;

return {
...prevData,
view_props: {
filters: {
...state.filters,
...property,
},
},
};
}, false);

if (viewId) {
saveModuleFilters(workspaceSlug.toString(), projectId.toString(), moduleId.toString(), {
view_props: {
filters: {
...state.filters,
...property,
},
},
});
} else if (viewId) {
mutateViewDetails((prevData: any) => {
if (!prevData) return prevData;
return {
Expand All @@ -396,22 +479,49 @@ export const IssueViewContextProvider: React.FC<{ children: React.ReactNode }> =
};
}, false);
if (saveToServer)
sendFilterDataToServer(workspaceSlug as string, projectId as string, viewId as string, {
saveViewFilters(workspaceSlug as string, projectId as string, viewId as string, {
query_data: {
...state.filters,
...property,
},
});
} else if (saveToServer)
} else {
mutateMyViewProps((prevData) => {
if (!prevData) return prevData;

return {
...prevData,
view_props: {
...state,
filters: {
...state.filters,
...property,
},
},
};
}, false);

saveDataToServer(workspaceSlug as string, projectId as string, {
...state,
filters: {
...state.filters,
...property,
},
});
}
},
[projectId, workspaceSlug, state, mutateMyViewProps, viewId, mutateViewDetails]
[
projectId,
workspaceSlug,
state,
mutateMyViewProps,
cycleId,
mutateCycleDetails,
moduleId,
mutateModuleDetails,
viewId,
mutateViewDetails,
]
);

const setNewDefaultView = useCallback(() => {
Expand Down Expand Up @@ -439,11 +549,17 @@ export const IssueViewContextProvider: React.FC<{ children: React.ReactNode }> =
payload: {
...myViewProps?.view_props,
filters: {
...(viewId ? viewDetails?.query_data : myViewProps?.view_props?.filters),
...(cycleId
? cycleDetails?.view_props.filters
: moduleId
? moduleDetails?.view_props.filters
: viewId
? viewDetails?.query_data
: myViewProps?.view_props?.filters),
} as any,
},
});
}, [myViewProps, viewDetails, viewId]);
}, [myViewProps, cycleId, cycleDetails, moduleId, moduleDetails, viewId, viewDetails]);

return (
<issueViewContext.Provider
Expand Down
2 changes: 1 addition & 1 deletion apps/app/services/cycles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ProjectCycleServices extends APIService {
workspaceSlug: string,
projectId: string,
cycleId: string,
data: any
data: Partial<ICycle>
): Promise<any> {
return this.patch(
`/api/workspaces/${workspaceSlug}/projects/${projectId}/cycles/${cycleId}/`,
Expand Down
6 changes: 5 additions & 1 deletion apps/app/services/modules.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class ProjectIssuesServices extends APIService {
});
}

async getModuleDetails(workspaceSlug: string, projectId: string, moduleId: string): Promise<any> {
async getModuleDetails(
workspaceSlug: string,
projectId: string,
moduleId: string
): Promise<IModule> {
return this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/modules/${moduleId}/`)
.then((response) => response?.data)
.catch((error) => {
Expand Down
13 changes: 12 additions & 1 deletion apps/app/types/cycles.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { IUser, IIssue, IProject, IProjectLite, IWorkspace, IWorkspaceLite } from "types";
import type {
IUser,
IIssue,
IProject,
IProjectLite,
IWorkspace,
IWorkspaceLite,
IIssueFilterOptions,
} from "types";

export interface ICycle {
backlog_issues: number;
Expand All @@ -21,6 +29,9 @@ export interface ICycle {
unstarted_issues: number;
updated_at: Date;
updated_by: string;
view_props: {
filters: IIssueFilterOptions;
};
workspace: string;
workspace_detail: IWorkspaceLite;
}
Expand Down
4 changes: 4 additions & 0 deletions apps/app/types/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IWorkspace,
IWorkspaceLite,
IProjectLite,
IIssueFilterOptions,
} from "types";

export interface IModule {
Expand Down Expand Up @@ -45,6 +46,9 @@ export interface IModule {
unstarted_issues: number;
updated_at: Date;
updated_by: string;
view_props: {
filters: IIssueFilterOptions;
};
workspace: string;
workspace_detail: IWorkspaceLite;
}
Expand Down

1 comment on commit c5206a7

@vercel
Copy link

@vercel vercel bot commented on c5206a7 Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

plane-dev – ./apps/app

plane-dev-caravel.vercel.app
plane-dev-git-develop-caravel.vercel.app
plane-dev.vercel.app

Please sign in to comment.