From 7915121dd9193879527d560be5b40999ae169c9e Mon Sep 17 00:00:00 2001 From: sriram veeraghanta Date: Tue, 30 Jan 2024 19:10:33 +0530 Subject: [PATCH] fix: store changes --- packages/types/src/instance.d.ts | 1 + web/store/instance.store.ts | 59 ++++++++++++++++++++ web/store/root.store.ts | 7 ++- web/store/user/current-user.store.ts | 80 ++++++++++++++++++++++++++++ web/store/user/user.store.ts | 48 +++++++++++++++++ 5 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 web/store/instance.store.ts create mode 100644 web/store/user/current-user.store.ts create mode 100644 web/store/user/user.store.ts diff --git a/packages/types/src/instance.d.ts b/packages/types/src/instance.d.ts index e11b6add8..c8b94f770 100644 --- a/packages/types/src/instance.d.ts +++ b/packages/types/src/instance.d.ts @@ -14,6 +14,7 @@ export interface IInstance { namespace: string | null; is_telemetry_enabled: boolean; is_support_required: boolean; + is_signup_screen_visited: boolean; created_by: string | null; updated_by: string | null; is_activated: boolean; diff --git a/web/store/instance.store.ts b/web/store/instance.store.ts new file mode 100644 index 000000000..f5529fbf7 --- /dev/null +++ b/web/store/instance.store.ts @@ -0,0 +1,59 @@ +import { observable, action, makeObservable } from "mobx"; +import set from "lodash/set"; +// types +import { IInstance } from "@plane/types"; +// services +import { InstanceService } from "services/instance.service"; + +export interface IInstanceStore { + id: string | undefined; + instance_id: string | undefined; + instance_name: string | undefined; + is_activated: boolean | undefined; + is_setup_done: boolean | undefined; + is_signup_screen_visited: boolean | undefined; + is_support_required: boolean | undefined; + is_telemetry_enabled: boolean | undefined; + license_key: string | undefined; + namespace: string | undefined; + version: string | undefined; + // actions + fetchInstanceInfo: () => Promise; +} + +export class InstanceStore implements IInstanceStore { + id: string | undefined; + instance_id: string | undefined; + instance_name: string | undefined; + is_activated: boolean | undefined; + is_setup_done: boolean | undefined; + is_signup_screen_visited: boolean | undefined; + is_support_required: boolean | undefined; + is_telemetry_enabled: boolean | undefined; + license_key: string | undefined; + namespace: string | undefined; + version: string | undefined; + // service + instanceService; + + constructor() { + makeObservable(this, { + // actions + fetchInstanceInfo: action, + }); + + this.instanceService = new InstanceService(); + } + + updateInstanceInfo = async (data: Partial) => { + Object.keys(data).forEach((key) => { + set(this, key, data[key as keyof IInstance]); + }); + }; + + fetchInstanceInfo = async () => { + const instance = await this.instanceService.getInstanceInfo(); + this.updateInstanceInfo(instance); + return instance; + }; +} diff --git a/web/store/root.store.ts b/web/store/root.store.ts index b3aeeea04..b2404d492 100644 --- a/web/store/root.store.ts +++ b/web/store/root.store.ts @@ -17,6 +17,7 @@ import { IMentionStore, MentionStore } from "./mention.store"; import { DashboardStore, IDashboardStore } from "./dashboard.store"; import { IProjectPageStore, ProjectPageStore } from "./project-page.store"; import { ILabelStore, LabelStore } from "./label.store"; +import { IInstanceStore, InstanceStore } from "./instance.store"; enableStaticRendering(typeof window === "undefined"); @@ -38,10 +39,14 @@ export class RootStore { mention: IMentionStore; dashboard: IDashboardStore; projectPages: IProjectPageStore; + // new store implementation + instance: IInstanceStore; constructor() { - this.app = new AppRootStore(this); + this.instance = new InstanceStore(); this.user = new UserRootStore(this); + // old implementation + this.app = new AppRootStore(this); this.workspaceRoot = new WorkspaceRootStore(this); this.projectRoot = new ProjectRootStore(this); this.memberRoot = new MemberRootStore(this); diff --git a/web/store/user/current-user.store.ts b/web/store/user/current-user.store.ts new file mode 100644 index 000000000..237f8bcf8 --- /dev/null +++ b/web/store/user/current-user.store.ts @@ -0,0 +1,80 @@ +import { makeObservable, runInAction, observable } from "mobx"; +import set from "lodash/set"; +// types +import { IUser } from "@plane/types"; +// store +import { UserStore, IUserStore } from "./user.store"; +// services +import { UserService } from "services/user.service"; + +interface ICurrentUserStore extends IUserStore { + // fetch actions + fetchCurrentUser: () => Promise; + fetchUserInstanceAdminStatus: () => Promise; + updateCurrentUser: (data: Partial) => Promise; +} + +export class CurrentUserStore extends UserStore implements ICurrentUserStore { + is_instance_admin: boolean = false; + + userService; + + constructor(user: IUser) { + super(user); + makeObservable(this, { + is_instance_admin: observable.ref, + }); + this.userService = new UserService(); + } + + /** + * Fetches the current user + * @returns Promise + */ + fetchCurrentUser = async () => { + const response = await this.userService.currentUser(); + this.updateUserInfo(response); + return response; + }; + + updateUserInfo = async (data: Partial) => { + runInAction(() => { + Object.keys(data).forEach((key) => { + set(this, key, data[key as keyof IUser]); + }); + }); + }; + + /** + * Fetches the current user instance admin status + * @returns Promise + */ + fetchUserInstanceAdminStatus = async () => + await this.userService.currentUserInstanceAdminStatus().then((response) => { + runInAction(() => { + this.is_instance_admin = response.is_instance_admin; + }); + return response.is_instance_admin; + }); + + /** + * Updates the current user + * @param data + * @returns Promise + */ + updateCurrentUser = async (data: Partial) => { + try { + runInAction(() => { + this.updateUserInfo(data); + }); + const response = await this.userService.updateUser(data); + runInAction(() => { + this.updateUserInfo(response); + }); + return response; + } catch (error) { + this.fetchCurrentUser(); + throw error; + } + }; +} diff --git a/web/store/user/user.store.ts b/web/store/user/user.store.ts new file mode 100644 index 000000000..cef0f6004 --- /dev/null +++ b/web/store/user/user.store.ts @@ -0,0 +1,48 @@ +import { IUser } from "@plane/types"; +import { makeObservable, observable } from "mobx"; + +export interface IUserStore { + avatar: string | undefined; + cover_image: string | undefined; + date_joined: string | undefined; + display_name: string | undefined; + email: string | undefined; + first_name: string | undefined; + id: string | undefined; + is_active: boolean | undefined; + is_bot: boolean | undefined; + is_email_verified: boolean | undefined; + is_managed: boolean | undefined; + last_name: string | undefined; +} + +export class UserStore implements IUserStore { + avatar: string | undefined; + cover_image: string | undefined; + date_joined: string | undefined; + display_name: string | undefined; + email: string | undefined; + first_name: string | undefined; + id: string | undefined; + is_active: boolean | undefined; + is_bot: boolean | undefined; + is_email_verified: boolean | undefined; + is_managed: boolean | undefined; + last_name: string | undefined; + + constructor(user: IUser) { + makeObservable(this); + this.avatar = user?.avatar; + this.cover_image = user?.cover_image || undefined; + this.date_joined = user?.date_joined; + this.display_name = user?.display_name; + this.email = user?.email; + this.first_name = user?.first_name; + this.id = user?.id; + this.is_active = user?.is_active; + this.is_bot = user?.is_bot; + this.is_email_verified = user?.is_email_verified; + this.is_managed = user?.is_managed; + this.last_name = user?.last_name; + } +}