Skip to content

Commit b230334

Browse files
committed
[dashboard, protocol] Move ProfileState to User.Profile
1 parent 7f63454 commit b230334

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

components/dashboard/src/settings/ProfileInformation.tsx

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,6 @@ import { UserContext } from "../user-context";
1515
import { isGitpodIo } from "../utils";
1616

1717
export namespace ProfileState {
18-
export interface ProfileState {
19-
name: string;
20-
email: string;
21-
company?: string;
22-
avatarURL?: string;
23-
}
24-
25-
export function getProfileState(user: User): ProfileState {
26-
return {
27-
name: User.getName(user!) || "",
28-
email: User.getPrimaryEmail(user!) || "",
29-
company: user?.additionalData?.profile?.companyName,
30-
avatarURL: user?.avatarUrl,
31-
};
32-
}
33-
34-
export function setProfileState(user: User, profileState: ProfileState): User {
35-
user.fullName = profileState.name;
36-
user.avatarUrl = profileState.avatarURL;
37-
38-
if (!user.additionalData) {
39-
user.additionalData = {};
40-
}
41-
if (!user.additionalData.profile) {
42-
user.additionalData.profile = {};
43-
}
44-
user.additionalData.profile.emailAddress = profileState.email;
45-
user.additionalData.profile.companyName = profileState.company;
46-
user.additionalData.profile.lastUpdatedDetailsNudge = new Date().toISOString();
47-
48-
return user;
49-
}
50-
51-
export function hasChanges(before: ProfileState, after: ProfileState) {
52-
return (
53-
before.name !== after.name ||
54-
before.email !== after.email ||
55-
before.company !== after.company ||
56-
before.avatarURL !== after.avatarURL
57-
);
58-
}
59-
6018
function shouldNudgeForUpdate(user: User): boolean {
6119
if (!isGitpodIo()) {
6220
return false;
@@ -79,7 +37,7 @@ export namespace ProfileState {
7937
* @param state
8038
* @returns error message or empty string when valid
8139
*/
82-
export function validate(state: ProfileState): string {
40+
export function validate(state: User.Profile): string {
8341
if (state.name.trim() === "") {
8442
return "Name must not be empty.";
8543
}
@@ -98,7 +56,7 @@ export namespace ProfileState {
9856

9957
export function NudgeForProfileUpdateModal() {
10058
const { user, setUser } = useContext(UserContext);
101-
const original = ProfileState.getProfileState(user!);
59+
const original = User.getProfile(user!);
10260
const [profileState, setProfileState] = useState(original);
10361
const [errorMessage, setErrorMessage] = useState("");
10462
const [visible, setVisible] = useState(shouldNudgeForUpdate(user!));
@@ -109,7 +67,7 @@ export namespace ProfileState {
10967
if (error) {
11068
return;
11169
}
112-
const updatedUser = ProfileState.setProfileState(user!, profileState);
70+
const updatedUser = User.setProfile(user!, profileState);
11371
setUser(updatedUser);
11472
getGitpodService().server.updateLoggedInUser(updatedUser);
11573
setVisible(shouldNudgeForUpdate(updatedUser!));
@@ -150,8 +108,8 @@ export namespace ProfileState {
150108
}
151109

152110
export default function ProfileInformation(props: {
153-
profileState: ProfileState.ProfileState;
154-
setProfileState: (newState: ProfileState.ProfileState) => void;
111+
profileState: User.Profile;
112+
setProfileState: (newState: User.Profile) => void;
155113
errorMessage: string;
156114
updated: boolean;
157115
children?: React.ReactChild[] | React.ReactChild;

components/gitpod-protocol/src/protocol.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,50 @@ export namespace User {
139139
}
140140
user.additionalData.ideSettings = newIDESettings;
141141
}
142+
143+
export function getProfile(user: User): Profile {
144+
return {
145+
name: User.getName(user!) || "",
146+
email: User.getPrimaryEmail(user!) || "",
147+
company: user?.additionalData?.profile?.companyName,
148+
avatarURL: user?.avatarUrl,
149+
};
150+
}
151+
152+
export function setProfile(user: User, profile: Profile): User {
153+
user.fullName = profile.name;
154+
user.avatarUrl = profile.avatarURL;
155+
156+
if (!user.additionalData) {
157+
user.additionalData = {};
158+
}
159+
if (!user.additionalData.profile) {
160+
user.additionalData.profile = {};
161+
}
162+
user.additionalData.profile.emailAddress = profile.email;
163+
user.additionalData.profile.companyName = profile.company;
164+
user.additionalData.profile.lastUpdatedDetailsNudge = new Date().toISOString();
165+
166+
return user;
167+
}
168+
169+
// The actual Profile of a User
170+
export interface Profile {
171+
name: string;
172+
email: string;
173+
company?: string;
174+
avatarURL?: string;
175+
}
176+
export namespace Profile {
177+
export function hasChanges(before: Profile, after: Profile) {
178+
return (
179+
before.name !== after.name ||
180+
before.email !== after.email ||
181+
before.company !== after.company ||
182+
before.avatarURL !== after.avatarURL
183+
);
184+
}
185+
}
142186
}
143187

144188
export interface AdditionalUserData {
@@ -163,6 +207,7 @@ export interface AdditionalUserData {
163207
profile?: ProfileDetails;
164208
}
165209

210+
// The format in which we store User Profiles in
166211
export interface ProfileDetails {
167212
// when was the last time the user updated their profile information or has been nudged to do so.
168213
lastUpdatedDetailsNudge?: string;

0 commit comments

Comments
 (0)