From dcf04b21f962e5ca40639be826007096ce6daae6 Mon Sep 17 00:00:00 2001 From: sriram veeraghanta Date: Wed, 1 Nov 2023 12:56:06 +0530 Subject: [PATCH] chore: minor optimizations --- web/services/user.service.ts | 2 +- web/store/issue/issue_draft.store.ts | 63 ++++++++-------------------- web/store/user.store.ts | 57 +++++++++++++------------ 3 files changed, 47 insertions(+), 75 deletions(-) diff --git a/web/services/user.service.ts b/web/services/user.service.ts index f5c4ac17e..2e1788a09 100644 --- a/web/services/user.service.ts +++ b/web/services/user.service.ts @@ -62,7 +62,7 @@ export class UserService extends APIService { }); } - async updateUser(data: Partial): Promise { + async updateUser(data: Partial): Promise { return this.patch("/api/users/me/", data) .then((response) => response?.data) .catch((error) => { diff --git a/web/store/issue/issue_draft.store.ts b/web/store/issue/issue_draft.store.ts index 76faacb7e..333d3dd38 100644 --- a/web/store/issue/issue_draft.store.ts +++ b/web/store/issue/issue_draft.store.ts @@ -3,7 +3,7 @@ import { action, observable, runInAction, makeAutoObservable } from "mobx"; // services import { IssueDraftService } from "services/issue"; // types -import type { IIssue, IUser } from "types"; +import type { IIssue } from "types"; export class DraftIssuesStore { issues: { [key: string]: IIssue } = {}; @@ -58,23 +58,15 @@ export class DraftIssuesStore { getIssueById = async (workspaceSlug: string, projectId: string, issueId: string): Promise => { if (this.issues[issueId]) return this.issues[issueId]; - - try { - const issueResponse: IIssue = await this.issueDraftService.getDraftIssueById(workspaceSlug, projectId, issueId); - - const issues = { - ...this.issues, - [issueId]: { ...issueResponse }, - }; - - runInAction(() => { - this.issues = issues; - }); - - return issueResponse; - } catch (error) { - throw error; - } + const issueResponse: IIssue = await this.issueDraftService.getDraftIssueById(workspaceSlug, projectId, issueId); + const issues = { + ...this.issues, + [issueId]: { ...issueResponse }, + }; + runInAction(() => { + this.issues = issues; + }); + return issueResponse; }; /** @@ -86,37 +78,18 @@ export class DraftIssuesStore { * @returns {IIssue} */ - createDraftIssue = async ( - workspaceSlug: string, - projectId: string, - issueForm: IIssue, - user: IUser - ): Promise => { - try { - const issueResponse = await this.issueDraftService.createDraftIssue(workspaceSlug, projectId, issueForm); - - const issues = { + createDraftIssue = async (workspaceSlug: string, projectId: string, issueForm: IIssue): Promise => { + const issueResponse = await this.issueDraftService.createDraftIssue(workspaceSlug, projectId, issueForm); + runInAction(() => { + this.issues = { ...this.issues, [issueResponse.id]: { ...issueResponse }, }; - - runInAction(() => { - this.issues = issues; - }); - return issueResponse; - } catch (error) { - console.error("Creating issue error", error); - throw error; - } + }); + return issueResponse; }; - updateDraftIssue = async ( - workspaceSlug: string, - projectId: string, - issueId: string, - issueForm: Partial, - user: IUser - ) => { + updateDraftIssue = async (workspaceSlug: string, projectId: string, issueId: string, issueForm: Partial) => { // keep a copy of the issue in the store 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 }; delete issues[issueId]; diff --git a/web/store/user.store.ts b/web/store/user.store.ts index cbb861288..d78e83edf 100644 --- a/web/store/user.store.ts +++ b/web/store/user.store.ts @@ -100,29 +100,19 @@ class UserStore implements IUserStore { }; fetchCurrentUserSettings = async () => { - try { - const response = await this.userService.currentUserSettings(); - if (response) { - runInAction(() => { - this.currentUserSettings = response; - }); - } - return response; - } catch (error) { - throw error; - } + const response = await this.userService.currentUserSettings(); + runInAction(() => { + this.currentUserSettings = response; + }); + return response; }; fetchUserDashboardInfo = async (workspaceSlug: string, month: number) => { - try { - const response = await this.userService.userWorkspaceDashboard(workspaceSlug, month); - runInAction(() => { - this.dashboardInfo = response; - }); - return response; - } catch (error) { - throw error; - } + const response = await this.userService.userWorkspaceDashboard(workspaceSlug, month); + runInAction(() => { + this.dashboardInfo = response; + }); + return response; }; fetchUserWorkspaceInfo = async (workspaceSlug: string) => { @@ -176,20 +166,22 @@ class UserStore implements IUserStore { return response; } } catch (error) { + runInAction(() => { + this.currentUser = { + ...this.currentUser, + is_tour_completed: false, + } as IUser; + }); throw error; } }; updateCurrentUser = async (data: Partial) => { - try { - const response = await this.userService.updateUser(data); - runInAction(() => { - this.currentUser = response; - }); - return response; - } catch (error) { - throw error; - } + const response = await this.userService.updateUser(data); + runInAction(() => { + this.currentUser = response; + }); + return response; }; updateCurrentUserTheme = async (theme: string) => { @@ -208,6 +200,13 @@ class UserStore implements IUserStore { } as IUser); return response; } catch (error) { + const response = await this.userService.currentUser(); + runInAction(() => { + this.currentUser = { + ...this.currentUser, + theme: response.theme, + } as IUser; + }); throw error; } };