-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathprofile.store.ts
More file actions
140 lines (132 loc) · 3.77 KB
/
profile.store.ts
File metadata and controls
140 lines (132 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (c) 2023-present Plane Software, Inc. and contributors
* SPDX-License-Identifier: AGPL-3.0-only
* See the LICENSE file for details.
*/
import { set } from "lodash-es";
import { action, makeObservable, observable, runInAction } from "mobx";
// plane imports
import { UserService } from "@plane/services";
import type { TUserProfile } from "@plane/types";
import { EStartOfTheWeek } from "@plane/types";
// store
import type { RootStore } from "@/store/root.store";
type TError = {
status: string;
message: string;
};
export interface IProfileStore {
// observables
isLoading: boolean;
error: TError | undefined;
data: TUserProfile;
// actions
fetchUserProfile: () => Promise<TUserProfile | undefined>;
updateUserProfile: (data: Partial<TUserProfile>) => Promise<TUserProfile | undefined>;
}
export class ProfileStore implements IProfileStore {
isLoading: boolean = false;
error: TError | undefined = undefined;
data: TUserProfile = {
id: undefined,
user: undefined,
role: undefined,
last_workspace_id: undefined,
theme: {
theme: undefined,
primary: undefined,
background: undefined,
darkPalette: undefined,
},
onboarding_step: {
workspace_join: false,
profile_complete: false,
workspace_create: false,
workspace_invite: false,
},
is_onboarded: false,
is_tour_completed: false,
use_case: undefined,
billing_address_country: undefined,
billing_address: undefined,
has_billing_address: false,
has_marketing_email_consent: false,
created_at: "",
updated_at: "",
language: "",
start_of_the_week: EStartOfTheWeek.SUNDAY,
};
// services
userService: UserService;
constructor(public store: RootStore) {
makeObservable(this, {
// observables
isLoading: observable.ref,
error: observable,
data: observable,
// actions
fetchUserProfile: action,
updateUserProfile: action,
});
// services
this.userService = new UserService();
}
// actions
/**
* @description fetches user profile information
* @returns {Promise<TUserProfile | undefined>}
*/
fetchUserProfile = async () => {
try {
runInAction(() => {
this.isLoading = true;
this.error = undefined;
});
const userProfile = await this.userService.profile();
runInAction(() => {
this.isLoading = false;
this.data = userProfile;
});
return userProfile;
} catch (_error) {
runInAction(() => {
this.isLoading = false;
this.error = {
status: "user-profile-fetch-error",
message: "Failed to fetch user profile",
};
});
}
};
/**
* @description updated the user profile information
* @param {Partial<TUserProfile>} data
* @returns {Promise<TUserProfile | undefined>}
*/
updateUserProfile = async (data: Partial<TUserProfile>) => {
const currentUserProfileData = this.data;
try {
if (currentUserProfileData) {
Object.keys(data).forEach((key: string) => {
const userKey: keyof TUserProfile = key as keyof TUserProfile;
if (this.data) set(this.data, userKey, data[userKey]);
});
}
const userProfile = await this.userService.updateProfile(data);
return userProfile;
} catch (_error) {
if (currentUserProfileData) {
Object.keys(currentUserProfileData).forEach((key: string) => {
const userKey: keyof TUserProfile = key as keyof TUserProfile;
if (this.data) set(this.data, userKey, currentUserProfileData[userKey]);
});
}
runInAction(() => {
this.error = {
status: "user-profile-update-error",
message: "Failed to update user profile",
};
});
}
};
}