chore: minor optimizations

This commit is contained in:
sriram veeraghanta 2023-11-01 12:56:06 +05:30
parent 80e6d7e1ea
commit dcf04b21f9
3 changed files with 47 additions and 75 deletions

View File

@ -62,7 +62,7 @@ export class UserService extends APIService {
}); });
} }
async updateUser(data: Partial<IUser>): Promise<any> { async updateUser(data: Partial<IUser>): Promise<IUser> {
return this.patch("/api/users/me/", data) return this.patch("/api/users/me/", data)
.then((response) => response?.data) .then((response) => response?.data)
.catch((error) => { .catch((error) => {

View File

@ -3,7 +3,7 @@ import { action, observable, runInAction, makeAutoObservable } from "mobx";
// services // services
import { IssueDraftService } from "services/issue"; import { IssueDraftService } from "services/issue";
// types // types
import type { IIssue, IUser } from "types"; import type { IIssue } from "types";
export class DraftIssuesStore { export class DraftIssuesStore {
issues: { [key: string]: IIssue } = {}; issues: { [key: string]: IIssue } = {};
@ -58,23 +58,15 @@ export class DraftIssuesStore {
getIssueById = async (workspaceSlug: string, projectId: string, issueId: string): Promise<IIssue> => { getIssueById = async (workspaceSlug: string, projectId: string, issueId: string): Promise<IIssue> => {
if (this.issues[issueId]) return this.issues[issueId]; if (this.issues[issueId]) return this.issues[issueId];
const issueResponse: IIssue = await this.issueDraftService.getDraftIssueById(workspaceSlug, projectId, issueId);
try { const issues = {
const issueResponse: IIssue = await this.issueDraftService.getDraftIssueById(workspaceSlug, projectId, issueId); ...this.issues,
[issueId]: { ...issueResponse },
const issues = { };
...this.issues, runInAction(() => {
[issueId]: { ...issueResponse }, this.issues = issues;
}; });
return issueResponse;
runInAction(() => {
this.issues = issues;
});
return issueResponse;
} catch (error) {
throw error;
}
}; };
/** /**
@ -86,37 +78,18 @@ export class DraftIssuesStore {
* @returns {IIssue} * @returns {IIssue}
*/ */
createDraftIssue = async ( createDraftIssue = async (workspaceSlug: string, projectId: string, issueForm: IIssue): Promise<IIssue> => {
workspaceSlug: string, const issueResponse = await this.issueDraftService.createDraftIssue(workspaceSlug, projectId, issueForm);
projectId: string, runInAction(() => {
issueForm: IIssue, this.issues = {
user: IUser
): Promise<IIssue> => {
try {
const issueResponse = await this.issueDraftService.createDraftIssue(workspaceSlug, projectId, issueForm);
const issues = {
...this.issues, ...this.issues,
[issueResponse.id]: { ...issueResponse }, [issueResponse.id]: { ...issueResponse },
}; };
});
runInAction(() => { return issueResponse;
this.issues = issues;
});
return issueResponse;
} catch (error) {
console.error("Creating issue error", error);
throw error;
}
}; };
updateDraftIssue = async ( updateDraftIssue = async (workspaceSlug: string, projectId: string, issueId: string, issueForm: Partial<IIssue>) => {
workspaceSlug: string,
projectId: string,
issueId: string,
issueForm: Partial<IIssue>,
user: IUser
) => {
// keep a copy of the issue in the store // keep a copy of the issue in the store
const originalIssue = { ...this.issues[issueId] }; const originalIssue = { ...this.issues[issueId] };
@ -152,7 +125,7 @@ export class DraftIssuesStore {
} }
}; };
deleteDraftIssue = async (workspaceSlug: string, projectId: string, issueId: string, user: IUser) => { deleteDraftIssue = async (workspaceSlug: string, projectId: string, issueId: string) => {
const issues = { ...this.issues }; const issues = { ...this.issues };
delete issues[issueId]; delete issues[issueId];

View File

@ -100,29 +100,19 @@ class UserStore implements IUserStore {
}; };
fetchCurrentUserSettings = async () => { fetchCurrentUserSettings = async () => {
try { const response = await this.userService.currentUserSettings();
const response = await this.userService.currentUserSettings(); runInAction(() => {
if (response) { this.currentUserSettings = response;
runInAction(() => { });
this.currentUserSettings = response; return response;
});
}
return response;
} catch (error) {
throw error;
}
}; };
fetchUserDashboardInfo = async (workspaceSlug: string, month: number) => { fetchUserDashboardInfo = async (workspaceSlug: string, month: number) => {
try { const response = await this.userService.userWorkspaceDashboard(workspaceSlug, month);
const response = await this.userService.userWorkspaceDashboard(workspaceSlug, month); runInAction(() => {
runInAction(() => { this.dashboardInfo = response;
this.dashboardInfo = response; });
}); return response;
return response;
} catch (error) {
throw error;
}
}; };
fetchUserWorkspaceInfo = async (workspaceSlug: string) => { fetchUserWorkspaceInfo = async (workspaceSlug: string) => {
@ -176,20 +166,22 @@ class UserStore implements IUserStore {
return response; return response;
} }
} catch (error) { } catch (error) {
runInAction(() => {
this.currentUser = {
...this.currentUser,
is_tour_completed: false,
} as IUser;
});
throw error; throw error;
} }
}; };
updateCurrentUser = async (data: Partial<IUser>) => { updateCurrentUser = async (data: Partial<IUser>) => {
try { const response = await this.userService.updateUser(data);
const response = await this.userService.updateUser(data); runInAction(() => {
runInAction(() => { this.currentUser = response;
this.currentUser = response; });
}); return response;
return response;
} catch (error) {
throw error;
}
}; };
updateCurrentUserTheme = async (theme: string) => { updateCurrentUserTheme = async (theme: string) => {
@ -208,6 +200,13 @@ class UserStore implements IUserStore {
} as IUser); } as IUser);
return response; return response;
} catch (error) { } catch (error) {
const response = await this.userService.currentUser();
runInAction(() => {
this.currentUser = {
...this.currentUser,
theme: response.theme,
} as IUser;
});
throw error; throw error;
} }
}; };