refactor: project estimates store (#2801)
* refactor: remove estimates from project store * chore: update all the instances of the old store * chore: update store declaration structure
This commit is contained in:
parent
d3c85b1336
commit
af8804eb12
13 changed files with 134 additions and 123 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { observable, action, makeObservable, runInAction } from "mobx";
|
||||
import { observable, action, makeObservable, runInAction, computed } from "mobx";
|
||||
// types
|
||||
import { RootStore } from "../root";
|
||||
import { IEstimate, IEstimateFormData } from "types";
|
||||
|
|
@ -9,7 +9,14 @@ export interface IProjectEstimateStore {
|
|||
loader: boolean;
|
||||
error: any | null;
|
||||
|
||||
// estimates
|
||||
// observables
|
||||
estimates: {
|
||||
[projectId: string]: IEstimate[] | null; // project_id: members
|
||||
} | null;
|
||||
|
||||
// actions
|
||||
getProjectEstimateById: (estimateId: string) => IEstimate | null;
|
||||
fetchProjectEstimates: (workspaceSlug: string, projectId: string) => Promise<void>;
|
||||
createEstimate: (workspaceSlug: string, projectId: string, data: IEstimateFormData) => Promise<IEstimate>;
|
||||
updateEstimate: (
|
||||
workspaceSlug: string,
|
||||
|
|
@ -18,14 +25,23 @@ export interface IProjectEstimateStore {
|
|||
data: IEstimateFormData
|
||||
) => Promise<IEstimate>;
|
||||
deleteEstimate: (workspaceSlug: string, projectId: string, estimateId: string) => Promise<void>;
|
||||
|
||||
// computed
|
||||
projectEstimates: IEstimate[] | undefined;
|
||||
}
|
||||
|
||||
export class ProjectEstimatesStore implements IProjectEstimateStore {
|
||||
loader: boolean = false;
|
||||
error: any | null = null;
|
||||
|
||||
// observables
|
||||
estimates: {
|
||||
[projectId: string]: IEstimate[]; // projectId: estimates
|
||||
} | null = {};
|
||||
|
||||
// root store
|
||||
rootStore;
|
||||
|
||||
// service
|
||||
projectService;
|
||||
estimateService;
|
||||
|
|
@ -36,10 +52,17 @@ export class ProjectEstimatesStore implements IProjectEstimateStore {
|
|||
loader: observable,
|
||||
error: observable,
|
||||
|
||||
// estimates
|
||||
estimates: observable.ref,
|
||||
|
||||
// actions
|
||||
getProjectEstimateById: action,
|
||||
fetchProjectEstimates: action,
|
||||
createEstimate: action,
|
||||
updateEstimate: action,
|
||||
deleteEstimate: action,
|
||||
|
||||
// computed
|
||||
projectEstimates: computed,
|
||||
});
|
||||
|
||||
this.rootStore = _rootStore;
|
||||
|
|
@ -47,6 +70,43 @@ export class ProjectEstimatesStore implements IProjectEstimateStore {
|
|||
this.estimateService = new ProjectEstimateService();
|
||||
}
|
||||
|
||||
get projectEstimates() {
|
||||
const projectId = this.rootStore.project.projectId;
|
||||
|
||||
if (!projectId) return undefined;
|
||||
return this.estimates?.[projectId] || undefined;
|
||||
}
|
||||
|
||||
getProjectEstimateById = (estimateId: string) => {
|
||||
const estimates = this.projectEstimates;
|
||||
if (!estimates) return null;
|
||||
const estimateInfo: IEstimate | null = estimates.find((estimate) => estimate.id === estimateId) || null;
|
||||
return estimateInfo;
|
||||
};
|
||||
|
||||
fetchProjectEstimates = async (workspaceSlug: string, projectId: string) => {
|
||||
try {
|
||||
this.loader = true;
|
||||
this.error = null;
|
||||
|
||||
const estimatesResponse = await this.estimateService.getEstimatesList(workspaceSlug, projectId);
|
||||
const _estimates = {
|
||||
...this.estimates,
|
||||
[projectId]: estimatesResponse,
|
||||
};
|
||||
|
||||
runInAction(() => {
|
||||
this.estimates = _estimates;
|
||||
this.loader = false;
|
||||
this.error = null;
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
this.loader = false;
|
||||
this.error = error;
|
||||
}
|
||||
};
|
||||
|
||||
createEstimate = async (workspaceSlug: string, projectId: string, data: IEstimateFormData) => {
|
||||
try {
|
||||
const response = await this.estimateService.createEstimate(workspaceSlug, projectId, data);
|
||||
|
|
@ -57,9 +117,9 @@ export class ProjectEstimatesStore implements IProjectEstimateStore {
|
|||
};
|
||||
|
||||
runInAction(() => {
|
||||
this.rootStore.project.estimates = {
|
||||
...this.rootStore.project.estimates,
|
||||
[projectId]: [responseEstimate, ...(this.rootStore.project.estimates?.[projectId] || [])],
|
||||
this.estimates = {
|
||||
...this.estimates,
|
||||
[projectId]: [responseEstimate, ...(this.estimates?.[projectId] || [])],
|
||||
};
|
||||
});
|
||||
|
||||
|
|
@ -71,12 +131,12 @@ export class ProjectEstimatesStore implements IProjectEstimateStore {
|
|||
};
|
||||
|
||||
updateEstimate = async (workspaceSlug: string, projectId: string, estimateId: string, data: IEstimateFormData) => {
|
||||
const originalEstimates = this.rootStore.project.getProjectEstimateById(estimateId);
|
||||
const originalEstimates = this.getProjectEstimateById(estimateId);
|
||||
|
||||
runInAction(() => {
|
||||
this.rootStore.project.estimates = {
|
||||
...this.rootStore.project.estimates,
|
||||
[projectId]: (this.rootStore.project.estimates?.[projectId] || [])?.map((estimate) =>
|
||||
this.estimates = {
|
||||
...this.estimates,
|
||||
[projectId]: (this.estimates?.[projectId] || [])?.map((estimate) =>
|
||||
estimate.id === estimateId ? { ...estimate, ...data.estimate } : estimate
|
||||
),
|
||||
};
|
||||
|
|
@ -84,15 +144,15 @@ export class ProjectEstimatesStore implements IProjectEstimateStore {
|
|||
|
||||
try {
|
||||
const response = await this.estimateService.patchEstimate(workspaceSlug, projectId, estimateId, data);
|
||||
await this.rootStore.project.fetchProjectEstimates(workspaceSlug, projectId);
|
||||
await this.fetchProjectEstimates(workspaceSlug, projectId);
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.log("Failed to update estimate from project store");
|
||||
runInAction(() => {
|
||||
this.rootStore.project.estimates = {
|
||||
...this.rootStore.project.estimates,
|
||||
[projectId]: (this.rootStore.project.estimates?.[projectId] || [])?.map((estimate) =>
|
||||
this.estimates = {
|
||||
...this.estimates,
|
||||
[projectId]: (this.estimates?.[projectId] || [])?.map((estimate) =>
|
||||
estimate.id === estimateId ? { ...estimate, ...originalEstimates } : estimate
|
||||
),
|
||||
};
|
||||
|
|
@ -102,14 +162,12 @@ export class ProjectEstimatesStore implements IProjectEstimateStore {
|
|||
};
|
||||
|
||||
deleteEstimate = async (workspaceSlug: string, projectId: string, estimateId: string) => {
|
||||
const originalEstimateList = this.rootStore.project.projectEstimates || [];
|
||||
const originalEstimateList = this.projectEstimates || [];
|
||||
|
||||
runInAction(() => {
|
||||
this.rootStore.project.estimates = {
|
||||
...this.rootStore.project.estimates,
|
||||
[projectId]: (this.rootStore.project.estimates?.[projectId] || [])?.filter(
|
||||
(estimate) => estimate.id !== estimateId
|
||||
),
|
||||
this.estimates = {
|
||||
...this.estimates,
|
||||
[projectId]: (this.estimates?.[projectId] || [])?.filter((estimate) => estimate.id !== estimateId),
|
||||
};
|
||||
});
|
||||
|
||||
|
|
@ -120,8 +178,8 @@ export class ProjectEstimatesStore implements IProjectEstimateStore {
|
|||
console.log("Failed to delete estimate from project store");
|
||||
// reverting back to original estimate list
|
||||
runInAction(() => {
|
||||
this.rootStore.project.estimates = {
|
||||
...this.rootStore.project.estimates,
|
||||
this.estimates = {
|
||||
...this.estimates,
|
||||
[projectId]: originalEstimateList,
|
||||
};
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { observable, action, computed, makeObservable, runInAction } from "mobx";
|
||||
// types
|
||||
import { RootStore } from "../root";
|
||||
import { IProject, IEstimate } from "types";
|
||||
import { IProject } from "types";
|
||||
// services
|
||||
import { ProjectService, ProjectStateService, ProjectEstimateService } from "services/project";
|
||||
import { ProjectService, ProjectStateService } from "services/project";
|
||||
import { IssueService, IssueLabelService } from "services/issue";
|
||||
|
||||
export interface IProjectStore {
|
||||
|
|
@ -16,14 +16,10 @@ export interface IProjectStore {
|
|||
project_details: {
|
||||
[projectId: string]: IProject; // projectId: project Info
|
||||
};
|
||||
estimates: {
|
||||
[projectId: string]: IEstimate[] | null; // project_id: members
|
||||
} | null;
|
||||
|
||||
// computed
|
||||
searchedProjects: IProject[];
|
||||
workspaceProjects: IProject[] | null;
|
||||
projectEstimates: IEstimate[] | null;
|
||||
joinedProjects: IProject[];
|
||||
favoriteProjects: IProject[];
|
||||
currentProjectDetails: IProject | undefined;
|
||||
|
|
@ -34,10 +30,8 @@ export interface IProjectStore {
|
|||
|
||||
getProjectById: (workspaceSlug: string, projectId: string) => IProject | null;
|
||||
|
||||
getProjectEstimateById: (estimateId: string) => IEstimate | null;
|
||||
fetchProjects: (workspaceSlug: string) => Promise<void>;
|
||||
fetchProjectDetails: (workspaceSlug: string, projectId: string) => Promise<any>;
|
||||
fetchProjectEstimates: (workspaceSlug: string, projectId: string) => Promise<any>;
|
||||
|
||||
addProjectToFavorites: (workspaceSlug: string, projectId: string) => Promise<any>;
|
||||
removeProjectFromFavorites: (workspaceSlug: string, projectId: string) => Promise<any>;
|
||||
|
|
@ -62,9 +56,6 @@ export class ProjectStore implements IProjectStore {
|
|||
project_details: {
|
||||
[projectId: string]: IProject; // projectId: project
|
||||
} = {};
|
||||
estimates: {
|
||||
[projectId: string]: IEstimate[]; // projectId: estimates
|
||||
} | null = {};
|
||||
|
||||
// root store
|
||||
rootStore;
|
||||
|
|
@ -73,7 +64,6 @@ export class ProjectStore implements IProjectStore {
|
|||
issueLabelService;
|
||||
issueService;
|
||||
stateService;
|
||||
estimateService;
|
||||
|
||||
constructor(_rootStore: RootStore) {
|
||||
makeObservable(this, {
|
||||
|
|
@ -86,14 +76,10 @@ export class ProjectStore implements IProjectStore {
|
|||
projects: observable.ref,
|
||||
project_details: observable.ref,
|
||||
|
||||
estimates: observable.ref,
|
||||
|
||||
// computed
|
||||
searchedProjects: computed,
|
||||
workspaceProjects: computed,
|
||||
|
||||
projectEstimates: computed,
|
||||
|
||||
currentProjectDetails: computed,
|
||||
|
||||
joinedProjects: computed,
|
||||
|
|
@ -106,9 +92,6 @@ export class ProjectStore implements IProjectStore {
|
|||
fetchProjectDetails: action,
|
||||
|
||||
getProjectById: action,
|
||||
getProjectEstimateById: action,
|
||||
|
||||
fetchProjectEstimates: action,
|
||||
|
||||
addProjectToFavorites: action,
|
||||
removeProjectFromFavorites: action,
|
||||
|
|
@ -125,7 +108,6 @@ export class ProjectStore implements IProjectStore {
|
|||
this.issueService = new IssueService();
|
||||
this.issueLabelService = new IssueLabelService();
|
||||
this.stateService = new ProjectStateService();
|
||||
this.estimateService = new ProjectEstimateService();
|
||||
}
|
||||
|
||||
get searchedProjects() {
|
||||
|
|
@ -164,11 +146,6 @@ export class ProjectStore implements IProjectStore {
|
|||
return this.projects?.[this.rootStore.workspace.workspaceSlug]?.filter((p) => p.is_favorite);
|
||||
}
|
||||
|
||||
get projectEstimates() {
|
||||
if (!this.projectId) return null;
|
||||
return this.estimates?.[this.projectId] || null;
|
||||
}
|
||||
|
||||
// actions
|
||||
setProjectId = (projectId: string | null) => {
|
||||
this.projectId = projectId;
|
||||
|
|
@ -223,37 +200,6 @@ export class ProjectStore implements IProjectStore {
|
|||
return projectInfo;
|
||||
};
|
||||
|
||||
getProjectEstimateById = (estimateId: string) => {
|
||||
if (!this.projectId) return null;
|
||||
const estimates = this.projectEstimates;
|
||||
if (!estimates) return null;
|
||||
const estimateInfo: IEstimate | null = estimates.find((estimate) => estimate.id === estimateId) || null;
|
||||
return estimateInfo;
|
||||
};
|
||||
|
||||
fetchProjectEstimates = async (workspaceSlug: string, projectId: string) => {
|
||||
try {
|
||||
this.loader = true;
|
||||
this.error = null;
|
||||
|
||||
const estimatesResponse = await this.estimateService.getEstimatesList(workspaceSlug, projectId);
|
||||
const _estimates = {
|
||||
...this.estimates,
|
||||
[projectId]: estimatesResponse,
|
||||
};
|
||||
|
||||
runInAction(() => {
|
||||
this.estimates = _estimates;
|
||||
this.loader = false;
|
||||
this.error = null;
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
this.loader = false;
|
||||
this.error = error;
|
||||
}
|
||||
};
|
||||
|
||||
addProjectToFavorites = async (workspaceSlug: string, projectId: string) => {
|
||||
try {
|
||||
runInAction(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue