plane/web/store/state.store.ts

218 lines
6.3 KiB
TypeScript
Raw Normal View History

2023-12-11 19:12:59 +00:00
import { makeObservable, observable, computed, action, runInAction } from "mobx";
import groupBy from "lodash/groupBy";
import keyBy from "lodash/keyBy";
2023-12-12 13:05:17 +00:00
import set from "lodash/set";
2023-12-11 19:12:59 +00:00
// store
import { RootStore } from "./root.store";
// types
import { IState } from "types";
// services
import { ProjectStateService } from "services/project";
export interface IStateStore {
stateMap: Record<string, IState>;
2023-12-11 19:12:59 +00:00
projectStates: IState[] | undefined;
groupedProjectStates: Record<string, IState[]> | undefined;
}
export class StateStore implements IStateStore {
stateMap: Record<string, IState> = {};
2023-12-11 19:12:59 +00:00
router;
stateService;
constructor(_rootStore: RootStore) {
makeObservable(this, {
// observables
stateMap: observable,
2023-12-11 19:12:59 +00:00
// computed
projectStates: computed,
groupedProjectStates: computed,
// actions
getProjectStates: action,
fetchProjectStates: action,
createState: action,
updateState: action,
deleteState: action,
});
this.stateService = new ProjectStateService();
this.router = _rootStore.app.router;
}
/**
* Returns the stateMap belongs to a specific project
2023-12-11 19:12:59 +00:00
*/
get projectStates() {
if (!this.router.query?.projectId) return;
return Object.values(this.stateMap).filter((state) => state.project === this.router.query.projectId);
2023-12-11 19:12:59 +00:00
}
/**
* Returns the stateMap belongs to a specific project grouped by group
2023-12-11 19:12:59 +00:00
*/
get groupedProjectStates() {
if (!this.router.query?.projectId) return;
return groupBy(this.projectStates, "group") as Record<string, IState[]>;
}
/**
* Returns the stateMap belongs to a project by projectId
2023-12-11 19:12:59 +00:00
* @param projectId
* @returns IState[]
*/
getProjectStates(projectId: string) {
return Object.values(this.stateMap).filter((state) => state.project === projectId);
2023-12-11 19:12:59 +00:00
}
/**
* fetches the stateMap of a project
2023-12-11 19:12:59 +00:00
* @param workspaceSlug
* @param projectId
* @returns
*/
fetchProjectStates = async (workspaceSlug: string, projectId: string) => {
const stateMap = await this.stateService.getStates(workspaceSlug, projectId);
2023-12-11 19:12:59 +00:00
runInAction(() => {
//todo add iteratively without modifying original reference
this.stateMap = {
...this.stateMap,
...keyBy(stateMap, "id"),
2023-12-11 19:12:59 +00:00
};
});
return stateMap;
2023-12-11 19:12:59 +00:00
};
/**
* creates a new state in a project and adds it to the store
* @param workspaceSlug
* @param projectId
* @param data
* @returns
*/
createState = async (workspaceSlug: string, projectId: string, data: Partial<IState>) => {
const response = await this.stateService.createState(workspaceSlug, projectId, data);
2023-12-11 19:12:59 +00:00
runInAction(() => {
set(this.stateMap, [response?.id], response);
2023-12-11 19:12:59 +00:00
});
return response;
};
/**
* Updates the state details in the store, in case of failure reverts back to original state
* @param workspaceSlug
* @param projectId
* @param stateId
* @param data
* @returns
*/
updateState = async (workspaceSlug: string, projectId: string, stateId: string, data: Partial<IState>) => {
const originalState = this.stateMap[stateId];
2023-12-11 19:12:59 +00:00
try {
runInAction(() => {
set(this.stateMap, [stateId], { ...this.stateMap?.[stateId], ...data });
2023-12-11 19:12:59 +00:00
});
const response = await this.stateService.patchState(workspaceSlug, projectId, stateId, data);
return response;
} catch (error) {
runInAction(() => {
this.stateMap = {
...this.stateMap,
2023-12-11 19:12:59 +00:00
[stateId]: originalState,
};
});
throw error;
}
};
/**
* deletes the state from the store, incase of failure reverts back to original state
* @param workspaceSlug
* @param projectId
* @param stateId
*/
deleteState = async (workspaceSlug: string, projectId: string, stateId: string) => {
const originalStates = this.stateMap;
2023-12-11 19:12:59 +00:00
try {
if (!this.stateMap?.[stateId]) return;
2023-12-11 19:12:59 +00:00
runInAction(() => {
delete this.stateMap[stateId];
2023-12-11 19:12:59 +00:00
});
2023-12-11 19:12:59 +00:00
await this.stateService.deleteState(workspaceSlug, projectId, stateId);
} catch (error) {
runInAction(() => {
this.stateMap = originalStates;
2023-12-11 19:12:59 +00:00
});
throw error;
}
};
/**
* marks a state as default in a project
* @param workspaceSlug
* @param projectId
* @param stateId
*/
markStateAsDefault = async (workspaceSlug: string, projectId: string, stateId: string) => {
const originalStates = this.stateMap;
2023-12-11 19:12:59 +00:00
try {
runInAction(() => {
set(this.stateMap, [stateId, "default"], true);
2023-12-11 19:12:59 +00:00
});
2023-12-11 19:12:59 +00:00
await this.stateService.markDefault(workspaceSlug, projectId, stateId);
} catch (error) {
runInAction(() => {
this.stateMap = originalStates;
2023-12-11 19:12:59 +00:00
});
throw error;
}
};
/**
* updates the sort order of a state and updates the state information using API, in case of failure reverts back to original state
* @param workspaceSlug
* @param projectId
* @param stateId
* @param direction
* @param groupIndex
*/
moveStatePosition = async (
workspaceSlug: string,
projectId: string,
stateId: string,
direction: "up" | "down",
groupIndex: number
) => {
const SEQUENCE_GAP = 15000;
const originalStates = this.stateMap;
2023-12-11 19:12:59 +00:00
try {
let newSequence = SEQUENCE_GAP;
const stateMap = this.projectStates || [];
const selectedState = stateMap?.find((state) => state.id === stateId);
const groupStates = stateMap?.filter((state) => state.group === selectedState?.group);
2023-12-11 19:12:59 +00:00
const groupLength = groupStates.length;
if (direction === "up") {
if (groupIndex === 1) newSequence = groupStates[0].sequence - SEQUENCE_GAP;
else newSequence = (groupStates[groupIndex - 2].sequence + groupStates[groupIndex - 1].sequence) / 2;
} else {
if (groupIndex === groupLength - 2) newSequence = groupStates[groupLength - 1].sequence + SEQUENCE_GAP;
else newSequence = (groupStates[groupIndex + 2].sequence + groupStates[groupIndex + 1].sequence) / 2;
}
2023-12-11 19:12:59 +00:00
runInAction(() => {
set(this.stateMap, [stateId, "sequence"], newSequence);
2023-12-11 19:12:59 +00:00
});
// updating using api
2023-12-11 19:12:59 +00:00
await this.stateService.patchState(workspaceSlug, projectId, stateId, { sequence: newSequence });
} catch (err) {
// reverting back to old state group if api fails
runInAction(() => {
this.stateMap = originalStates;
2023-12-11 19:12:59 +00:00
});
}
};
}