plane/web/store/view/view.store.ts

286 lines
8.1 KiB
TypeScript
Raw Normal View History

2024-01-30 11:15:14 +00:00
import { action, computed, makeObservable, observable, runInAction } from "mobx";
import set from "lodash/set";
2024-01-30 08:56:59 +00:00
// store
import { RootStore } from "store/root.store";
// types
import { TViewService } from "services/view/types";
2024-01-30 11:15:14 +00:00
import {
TView,
TFilters,
TDisplayFilters,
TDisplayProperties,
TFilterProps,
TFilterPartialProps,
2024-01-30 11:15:14 +00:00
TViewAccess,
} from "@plane/types";
// helpers
import { FiltersHelper } from "./filters_helpers";
2024-01-30 11:15:14 +00:00
type TLoader = "submitting" | "submit" | undefined;
export type TViewsStore = TView & {
// observables
loader: TLoader;
filtersToUpdate: TFilterPartialProps;
2024-01-30 08:56:59 +00:00
// computed
appliedFilters: TFilterProps | undefined;
appliedFiltersQueryParams: string | undefined;
2024-01-30 11:15:14 +00:00
// helper actions
updateFilters: (filters: Partial<TFilters>) => void;
updateDisplayFilters: (display_filters: Partial<TDisplayFilters>) => void;
updateDisplayProperties: (display_properties: Partial<TDisplayProperties>) => void;
2024-01-30 11:15:14 +00:00
resetFilterChanges: () => void;
saveFilterChanges: () => void;
2024-01-30 08:56:59 +00:00
// actions
lockView: () => Promise<void>;
unlockView: () => Promise<void>;
2024-01-30 11:15:14 +00:00
makeFavorite: () => Promise<void>;
removeFavorite: () => Promise<void>;
update: (viewData: Partial<TView>) => Promise<void>;
2024-01-30 08:56:59 +00:00
};
export class ViewsStore extends FiltersHelper implements TViewsStore {
2024-01-30 08:56:59 +00:00
id: string;
workspace: string;
project: string | undefined;
name: string;
description: string;
2024-01-30 08:56:59 +00:00
query: string;
filters: TFilters;
display_filters: TDisplayFilters;
display_properties: TDisplayProperties;
2024-01-30 08:56:59 +00:00
access: TViewAccess;
owned_by: string;
sort_order: number;
is_locked: boolean;
is_pinned: boolean;
2024-01-30 11:15:14 +00:00
is_favorite: boolean;
2024-01-30 08:56:59 +00:00
created_by: string;
updated_by: string;
created_at: Date;
updated_at: Date;
2024-01-30 11:15:14 +00:00
loader: TLoader = undefined;
filtersToUpdate: TFilterPartialProps = {
2024-01-30 11:15:14 +00:00
filters: {},
display_filters: {},
display_properties: {},
};
2024-01-30 08:56:59 +00:00
constructor(private store: RootStore, _view: TView, private service: TViewService) {
super();
2024-01-30 08:56:59 +00:00
this.id = _view.id;
this.workspace = _view.workspace;
this.project = _view.project;
this.name = _view.name;
this.description = _view.description;
this.query = _view.query;
this.filters = this.computedFilters(_view.filters);
this.display_filters = this.computedDisplayFilters(_view.display_filters);
this.display_properties = this.computedDisplayProperties(_view.display_properties);
2024-01-30 08:56:59 +00:00
this.access = _view.access;
this.owned_by = _view.owned_by;
this.sort_order = _view.sort_order;
this.is_locked = _view.is_locked;
this.is_pinned = _view.is_pinned;
2024-01-30 11:15:14 +00:00
this.is_favorite = _view.is_favorite;
2024-01-30 08:56:59 +00:00
this.created_by = _view.created_by;
this.updated_by = _view.updated_by;
this.created_at = _view.created_at;
this.updated_at = _view.updated_at;
makeObservable(this, {
2024-01-30 11:15:14 +00:00
// observables
loader: observable,
filtersToUpdate: observable.ref,
2024-01-30 08:56:59 +00:00
// computed
2024-01-30 11:15:14 +00:00
appliedFilters: computed,
appliedFiltersQueryParams: computed,
// helper actions
2024-01-30 08:56:59 +00:00
updateFilters: action,
updateDisplayFilters: action,
updateDisplayProperties: action,
2024-01-30 11:15:14 +00:00
resetFilterChanges: action,
saveFilterChanges: action,
// actions
update: action,
2024-01-30 08:56:59 +00:00
lockView: action,
unlockView: action,
});
}
// computed
2024-01-30 11:15:14 +00:00
get appliedFilters() {
return {
filters: this.computedFilters(this.filters, this.filtersToUpdate.filters),
display_filters: this.computedDisplayFilters(this.display_filters, this.filtersToUpdate.display_filters),
display_properties: this.computedDisplayProperties(
this.display_properties,
this.filtersToUpdate.display_properties
),
};
2024-01-30 08:56:59 +00:00
}
2024-01-30 11:15:14 +00:00
get appliedFiltersQueryParams() {
const filters = this.appliedFilters;
return this.computeAppliedFiltersQueryParameters(filters, [])?.query || undefined;
2024-01-30 11:15:14 +00:00
}
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
// helper actions
/**
* @description This method is used to update the filters of the view
* @param filters: Partial<TFilters>
*/
updateFilters = (filters: Partial<TFilters>) => {
2024-01-30 11:15:14 +00:00
runInAction(() => {
this.loader = "submit";
this.filtersToUpdate.filters = filters;
});
2024-01-30 08:56:59 +00:00
};
/**
* @description This method is used to update the display filters of the view
* @param display_filters: Partial<TDisplayFilters>
*/
updateDisplayFilters = async (display_filters: Partial<TDisplayFilters>) => {
const appliedFilters = this.appliedFilters;
const layout = appliedFilters.display_filters.layout;
const sub_group_by = appliedFilters.display_filters.sub_group_by;
const group_by = appliedFilters.display_filters.group_by;
const sub_issue = appliedFilters.display_filters.sub_issue;
if (group_by === undefined) display_filters.sub_group_by = undefined;
if (layout === "kanban") {
if (sub_group_by === group_by) display_filters.group_by = undefined;
if (group_by === null) display_filters.group_by = "state";
}
if (layout === "spreadsheet" && sub_issue === true) display_filters.sub_issue = false;
2024-01-30 11:15:14 +00:00
runInAction(() => {
this.loader = "submit";
this.filtersToUpdate.display_filters = display_filters;
});
};
2024-01-30 08:56:59 +00:00
/**
* @description This method is used to update the display properties of the view
* @param display_properties: Partial<TDisplayProperties>
*/
updateDisplayProperties = async (display_properties: Partial<TDisplayProperties>) => {
2024-01-30 11:15:14 +00:00
runInAction(() => {
this.loader = "submit";
this.filtersToUpdate.display_properties = display_properties;
});
};
2024-01-30 08:56:59 +00:00
/**
* @description This method is used to reset the changes made to the filters
*/
2024-01-30 11:15:14 +00:00
resetFilterChanges = () => {
runInAction(() => {
this.loader = undefined;
this.filtersToUpdate = {
filters: {},
display_filters: {},
display_properties: {},
};
});
2024-01-30 08:56:59 +00:00
};
/**
* @description This method is used to save the changes made to the filters
*/
2024-01-30 11:15:14 +00:00
saveFilterChanges = async () => {
this.loader = "submitting";
if (this.appliedFilters) await this.update(this.appliedFilters);
this.loader = undefined;
};
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
// actions
/**
* @description This method is used to update the view lock
* @returns
*/
2024-01-30 11:15:14 +00:00
lockView = async () => {
const { workspaceSlug, projectId } = this.store.app.router;
if (!workspaceSlug) return;
2024-01-30 11:15:14 +00:00
const view = await this.service.lock(workspaceSlug, this.id, projectId);
if (!view) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
this.is_locked = view.is_locked;
});
2024-01-30 08:56:59 +00:00
};
/**
* @description This method is used to remove the view lock
* @returns
*/
2024-01-30 11:15:14 +00:00
unlockView = async () => {
const { workspaceSlug, projectId } = this.store.app.router;
if (!workspaceSlug) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
const view = await this.service.unlock(workspaceSlug, this.id, projectId);
if (!view) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
this.is_locked = view.is_locked;
});
2024-01-30 08:56:59 +00:00
};
/**
* @description This method is used to update the view favorite
* @returns
*/
2024-01-30 11:15:14 +00:00
makeFavorite = async () => {
const { workspaceSlug, projectId } = this.store.app.router;
if (!workspaceSlug) return;
2024-01-30 11:15:14 +00:00
const view = await this.service.makeFavorite(workspaceSlug, this.id, projectId);
if (!view) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
this.is_favorite = view.is_locked;
});
2024-01-30 08:56:59 +00:00
};
/**
* @description This method is used to remove the view favorite
* @returns
*/
2024-01-30 11:15:14 +00:00
removeFavorite = async () => {
const { workspaceSlug, projectId } = this.store.app.router;
if (!workspaceSlug) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
const view = await this.service.removeFavorite(workspaceSlug, this.id, projectId);
if (!view) return;
2024-01-30 08:56:59 +00:00
2024-01-30 11:15:14 +00:00
runInAction(() => {
this.is_favorite = view.is_locked;
});
2024-01-30 08:56:59 +00:00
};
/**
* @description This method is used to update the view
* @param viewData
*/
2024-01-30 11:15:14 +00:00
update = async (viewData: Partial<TView>) => {
const { workspaceSlug, projectId } = this.store.app.router;
if (!workspaceSlug) return;
const view = await this.service.update(workspaceSlug, this.id, viewData, projectId);
if (!view) return;
runInAction(() => {
Object.keys(viewData).forEach((key) => {
const _key = key as keyof TView;
set(this, _key, viewData[_key]);
2024-01-30 11:15:14 +00:00
});
});
2024-01-30 08:56:59 +00:00
};
}