2023-12-12 13:05:17 +00:00
|
|
|
import set from "lodash/set";
|
2023-12-11 14:54:46 +00:00
|
|
|
import { observable, action, computed, makeObservable, runInAction } from "mobx";
|
2023-12-12 12:38:04 +00:00
|
|
|
//types
|
|
|
|
import { RootStore } from "../root.store";
|
|
|
|
import { IProject } from "types";
|
|
|
|
//services
|
2023-12-11 14:54:46 +00:00
|
|
|
import { IssueLabelService, IssueService } from "services/issue";
|
|
|
|
import { ProjectService, ProjectStateService } from "services/project";
|
|
|
|
|
|
|
|
export interface IProjectsStore {
|
|
|
|
loader: boolean;
|
|
|
|
error: any | null;
|
|
|
|
|
|
|
|
searchQuery: string;
|
|
|
|
projectId: string | null;
|
2023-12-12 12:38:04 +00:00
|
|
|
projectMap: {
|
2023-12-11 14:54:46 +00:00
|
|
|
[workspaceSlug: string]: {
|
|
|
|
[projectId: string]: IProject; // projectId: project Info
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// computed
|
|
|
|
searchedProjects: string[];
|
|
|
|
workspaceProjects: string[] | null;
|
|
|
|
joinedProjects: string[];
|
|
|
|
favoriteProjects: string[];
|
|
|
|
currentProjectDetails: IProject | undefined;
|
|
|
|
|
|
|
|
// actions
|
|
|
|
setSearchQuery: (query: string) => void;
|
|
|
|
getProjectById: (workspaceSlug: string, projectId: string) => IProject | null;
|
|
|
|
|
|
|
|
fetchProjects: (workspaceSlug: string) => Promise<void>;
|
|
|
|
fetchProjectDetails: (workspaceSlug: string, projectId: string) => Promise<any>;
|
|
|
|
|
|
|
|
addProjectToFavorites: (workspaceSlug: string, projectId: string) => Promise<any>;
|
|
|
|
removeProjectFromFavorites: (workspaceSlug: string, projectId: string) => Promise<any>;
|
|
|
|
|
|
|
|
orderProjectsWithSortOrder: (sourceIndex: number, destinationIndex: number, projectId: string) => number;
|
|
|
|
updateProjectView: (workspaceSlug: string, projectId: string, viewProps: any) => Promise<any>;
|
|
|
|
|
|
|
|
createProject: (workspaceSlug: string, data: any) => Promise<any>;
|
|
|
|
updateProject: (workspaceSlug: string, projectId: string, data: Partial<IProject>) => Promise<any>;
|
|
|
|
deleteProject: (workspaceSlug: string, projectId: string) => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ProjectsStore implements IProjectsStore {
|
|
|
|
loader: boolean = false;
|
|
|
|
error: any | null = null;
|
|
|
|
|
|
|
|
projectId: string | null = null;
|
|
|
|
searchQuery: string = "";
|
2023-12-12 12:38:04 +00:00
|
|
|
projectMap: {
|
2023-12-11 14:54:46 +00:00
|
|
|
[workspaceSlug: string]: {
|
|
|
|
[projectId: string]: IProject; // projectId: project Info
|
|
|
|
};
|
2023-12-12 13:05:17 +00:00
|
|
|
} = {};
|
2023-12-11 14:54:46 +00:00
|
|
|
|
|
|
|
// root store
|
|
|
|
rootStore: RootStore;
|
|
|
|
// service
|
|
|
|
projectService;
|
|
|
|
issueLabelService;
|
|
|
|
issueService;
|
|
|
|
stateService;
|
|
|
|
|
|
|
|
constructor(_rootStore: RootStore) {
|
|
|
|
makeObservable(this, {
|
|
|
|
// observable
|
2023-12-12 12:38:04 +00:00
|
|
|
loader: observable.ref,
|
|
|
|
error: observable.ref,
|
2023-12-11 14:54:46 +00:00
|
|
|
|
|
|
|
searchQuery: observable.ref,
|
|
|
|
projectId: observable.ref,
|
2023-12-12 12:38:04 +00:00
|
|
|
projectMap: observable,
|
2023-12-11 14:54:46 +00:00
|
|
|
|
|
|
|
// computed
|
|
|
|
searchedProjects: computed,
|
|
|
|
workspaceProjects: computed,
|
|
|
|
|
|
|
|
currentProjectDetails: computed,
|
|
|
|
|
|
|
|
joinedProjects: computed,
|
|
|
|
favoriteProjects: computed,
|
|
|
|
|
|
|
|
// action
|
|
|
|
setSearchQuery: action,
|
|
|
|
fetchProjects: action,
|
|
|
|
fetchProjectDetails: action,
|
|
|
|
|
|
|
|
addProjectToFavorites: action,
|
|
|
|
removeProjectFromFavorites: action,
|
|
|
|
|
|
|
|
orderProjectsWithSortOrder: action,
|
|
|
|
updateProjectView: action,
|
|
|
|
createProject: action,
|
|
|
|
updateProject: action,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.rootStore = _rootStore;
|
|
|
|
|
|
|
|
this.projectService = new ProjectService();
|
|
|
|
this.issueService = new IssueService();
|
|
|
|
this.issueLabelService = new IssueLabelService();
|
|
|
|
this.stateService = new ProjectStateService();
|
|
|
|
}
|
|
|
|
|
|
|
|
get searchedProjects() {
|
2023-12-12 12:38:04 +00:00
|
|
|
if (!this.rootStore.app.router.workspaceSlug) return [];
|
2023-12-11 14:54:46 +00:00
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProjectMap = this.projectMap[this.rootStore.app.router.workspaceSlug];
|
|
|
|
const projectIds = Object.keys(currentProjectMap);
|
2023-12-11 14:54:46 +00:00
|
|
|
return this.searchQuery === ""
|
|
|
|
? projectIds
|
|
|
|
: projectIds?.filter((projectId) => {
|
2023-12-12 12:38:04 +00:00
|
|
|
currentProjectMap[projectId].name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
|
|
|
|
currentProjectMap[projectId].identifier.toLowerCase().includes(this.searchQuery.toLowerCase());
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get workspaceProjects() {
|
2023-12-12 07:39:30 +00:00
|
|
|
if (!this.rootStore.app.router.workspaceSlug) return null;
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProjectMap = this.projectMap[this.rootStore.app.router.workspaceSlug];
|
2023-12-11 14:54:46 +00:00
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
const projectIds = Object.keys(currentProjectMap);
|
2023-12-11 14:54:46 +00:00
|
|
|
if (!projectIds) return null;
|
|
|
|
return projectIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentProjectDetails() {
|
2023-12-12 12:38:04 +00:00
|
|
|
if (!this.rootStore.app.router.projectId || !this.rootStore.app.router.workspaceSlug) return;
|
2023-12-12 13:05:17 +00:00
|
|
|
return this.projectMap[this.rootStore.app.router.workspaceSlug][this.rootStore.app.router.projectId];
|
2023-12-11 14:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get joinedProjects() {
|
2023-12-12 12:38:04 +00:00
|
|
|
if (!this.rootStore.app.router.workspaceSlug) return [];
|
2023-12-11 14:54:46 +00:00
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProjectMap = this.projectMap[this.rootStore.app.router.workspaceSlug];
|
|
|
|
const projectIds = Object.keys(currentProjectMap);
|
2023-12-11 14:54:46 +00:00
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
return projectIds?.filter((projectId) => currentProjectMap[projectId].is_member);
|
2023-12-11 14:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get favoriteProjects() {
|
2023-12-12 12:38:04 +00:00
|
|
|
if (!this.rootStore.app.router.workspaceSlug) return [];
|
2023-12-11 14:54:46 +00:00
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProjectMap = this.projectMap[this.rootStore.app.router.workspaceSlug];
|
|
|
|
const projectIds = Object.keys(currentProjectMap);
|
2023-12-11 14:54:46 +00:00
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
return projectIds?.filter((projectId) => currentProjectMap[projectId].is_favorite);
|
2023-12-11 14:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setSearchQuery = (query: string) => {
|
|
|
|
this.searchQuery = query;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get Workspace projects using workspace slug
|
|
|
|
* @param workspaceSlug
|
|
|
|
* @returns
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fetchProjects = async (workspaceSlug: string) => {
|
|
|
|
try {
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProjectMap = await this.projectService.getProjects(workspaceSlug);
|
|
|
|
|
2023-12-11 14:54:46 +00:00
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug], currentProjectMap);
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to fetch project from workspace store");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchProjectDetails = async (workspaceSlug: string, projectId: string) => {
|
|
|
|
try {
|
|
|
|
const response = await this.projectService.getProject(workspaceSlug, projectId);
|
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, projectId], response);
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error while fetching project details", error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getProjectById = (workspaceSlug: string, projectId: string) => {
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProjectMap = this.projectMap?.[workspaceSlug];
|
|
|
|
if (!currentProjectMap) return null;
|
2023-12-11 14:54:46 +00:00
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
const projectInfo: IProject | null = currentProjectMap[projectId] || null;
|
2023-12-11 14:54:46 +00:00
|
|
|
return projectInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
addProjectToFavorites = async (workspaceSlug: string, projectId: string) => {
|
|
|
|
try {
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProject = this.projectMap?.[workspaceSlug]?.[projectId];
|
|
|
|
|
|
|
|
if (currentProject.is_favorite) return;
|
2023-12-11 14:54:46 +00:00
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, projectId, "is_favorite"], true);
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const response = await this.projectService.addProjectToFavorites(workspaceSlug, projectId);
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to add project to favorite");
|
2023-12-12 12:38:04 +00:00
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, projectId, "is_favorite"], false);
|
2023-12-12 12:38:04 +00:00
|
|
|
});
|
|
|
|
|
2023-12-11 14:54:46 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
removeProjectFromFavorites = async (workspaceSlug: string, projectId: string) => {
|
|
|
|
try {
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProject = this.projectMap?.[workspaceSlug]?.[projectId];
|
|
|
|
|
|
|
|
if (!currentProject.is_favorite) return;
|
2023-12-11 14:54:46 +00:00
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, projectId, "is_favorite"], false);
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const response = await this.projectService.removeProjectFromFavorites(workspaceSlug, projectId);
|
|
|
|
await this.fetchProjects(workspaceSlug);
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to add project to favorite");
|
2023-12-12 12:38:04 +00:00
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, projectId, "is_favorite"], true);
|
2023-12-12 12:38:04 +00:00
|
|
|
});
|
2023-12-11 14:54:46 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
orderProjectsWithSortOrder = (sortIndex: number, destinationIndex: number, projectId: string) => {
|
|
|
|
try {
|
2023-12-12 12:38:04 +00:00
|
|
|
const workspaceSlug = this.rootStore.app.router.workspaceSlug;
|
2023-12-11 14:54:46 +00:00
|
|
|
if (!workspaceSlug) return 0;
|
|
|
|
|
2023-12-12 12:38:04 +00:00
|
|
|
const projectsList = Object.values(this.projectMap[workspaceSlug] || {}) || [];
|
2023-12-11 14:54:46 +00:00
|
|
|
let updatedSortOrder = projectsList[sortIndex].sort_order;
|
|
|
|
|
|
|
|
if (destinationIndex === 0) updatedSortOrder = (projectsList[0].sort_order as number) - 1000;
|
|
|
|
else if (destinationIndex === projectsList.length - 1)
|
|
|
|
updatedSortOrder = (projectsList[projectsList.length - 1].sort_order as number) + 1000;
|
|
|
|
else {
|
|
|
|
const destinationSortingOrder = projectsList[destinationIndex].sort_order as number;
|
|
|
|
const relativeDestinationSortingOrder =
|
|
|
|
sortIndex < destinationIndex
|
|
|
|
? (projectsList[destinationIndex + 1].sort_order as number)
|
|
|
|
: (projectsList[destinationIndex - 1].sort_order as number);
|
|
|
|
|
|
|
|
updatedSortOrder = (destinationSortingOrder + relativeDestinationSortingOrder) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, projectId, "sort_order"], updatedSortOrder);
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return updatedSortOrder;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("failed to update sort order of the projects");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateProjectView = async (workspaceSlug: string, projectId: string, viewProps: any) => {
|
|
|
|
try {
|
|
|
|
const response = await this.projectService.setProjectView(workspaceSlug, projectId, viewProps);
|
|
|
|
await this.fetchProjects(workspaceSlug);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to update sort order of the projects");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
createProject = async (workspaceSlug: string, data: any) => {
|
|
|
|
try {
|
|
|
|
const response = await this.projectService.createProject(workspaceSlug, data);
|
2023-12-12 12:38:04 +00:00
|
|
|
|
2023-12-11 14:54:46 +00:00
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, response.id], response);
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
2023-12-12 12:38:04 +00:00
|
|
|
|
2023-12-11 14:54:46 +00:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to create project from project store");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateProject = async (workspaceSlug: string, projectId: string, data: Partial<IProject>) => {
|
|
|
|
try {
|
2023-12-12 12:38:04 +00:00
|
|
|
const currentProject = this.projectMap?.[workspaceSlug]?.[projectId];
|
2023-12-11 14:54:46 +00:00
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
set(this.projectMap, [workspaceSlug, projectId], { ...currentProject, ...data });
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const response = await this.projectService.updateProject(workspaceSlug, projectId, data);
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to create project from project store");
|
|
|
|
|
|
|
|
this.fetchProjects(workspaceSlug);
|
|
|
|
this.fetchProjectDetails(workspaceSlug, projectId);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
deleteProject = async (workspaceSlug: string, projectId: string) => {
|
|
|
|
try {
|
2023-12-13 07:47:13 +00:00
|
|
|
if (!this.projectMap?.[workspaceSlug]?.[projectId]) return;
|
2023-12-11 14:54:46 +00:00
|
|
|
|
|
|
|
runInAction(() => {
|
2023-12-13 07:47:13 +00:00
|
|
|
delete this.projectMap[workspaceSlug][projectId];
|
2023-12-11 14:54:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await this.projectService.deleteProject(workspaceSlug, projectId);
|
|
|
|
await this.fetchProjects(workspaceSlug);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to delete project from project store");
|
|
|
|
this.fetchProjects(workspaceSlug);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|