|
| 1 | +/** |
| 2 | + * Copyright (c) 2021 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { User } from "@gitpod/gitpod-protocol"; |
| 8 | +import { hoursBefore, isDateSmaller } from "@gitpod/gitpod-protocol/lib/util/timeutil"; |
| 9 | +import React, { useContext, useState } from "react"; |
| 10 | +import CodeText from "../components/CodeText"; |
| 11 | +import Modal from "../components/Modal"; |
| 12 | +import { getGitpodService } from "../service/service"; |
| 13 | +import { UserContext } from "../user-context"; |
| 14 | + |
| 15 | +export namespace ProfileState { |
| 16 | + export interface ProfileState { |
| 17 | + name: string; |
| 18 | + email: string; |
| 19 | + company?: string; |
| 20 | + avatarURL?: string; |
| 21 | + } |
| 22 | + |
| 23 | + export function getProfileState(user: User): ProfileState { |
| 24 | + return { |
| 25 | + name: User.getName(user!) || "", |
| 26 | + email: User.getPrimaryEmail(user!) || "", |
| 27 | + company: user?.additionalData?.companyName, |
| 28 | + avatarURL: user?.avatarUrl, |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + export function setProfileState(user: User, profileState: ProfileState): User { |
| 33 | + user.fullName = profileState.name; |
| 34 | + user.avatarUrl = profileState.avatarURL; |
| 35 | + |
| 36 | + if (!user.additionalData) { |
| 37 | + user.additionalData = {}; |
| 38 | + } |
| 39 | + user.additionalData.emailAddress = profileState.email; |
| 40 | + user.additionalData.companyName = profileState.company; |
| 41 | + user.additionalData.lastUpdatedDetailsNudge = new Date().toISOString(); |
| 42 | + |
| 43 | + return user; |
| 44 | + } |
| 45 | + |
| 46 | + export function hasChanges(before: ProfileState, after: ProfileState) { |
| 47 | + return ( |
| 48 | + before.name !== after.name || |
| 49 | + before.email !== after.email || |
| 50 | + before.company !== after.company || |
| 51 | + before.avatarURL !== after.avatarURL |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + function shouldNudgeForUpdate(user: User): boolean { |
| 56 | + if (!user.additionalData) { |
| 57 | + // never updated profile information and account is older tha 24 hours (i.e. ask on second day). |
| 58 | + return !isDateSmaller(hoursBefore(new Date().toISOString(), 24), user.creationDate); |
| 59 | + } |
| 60 | + // if the profile wasn't updated for 12 months ask again. |
| 61 | + return !( |
| 62 | + !!user.additionalData.lastUpdatedDetailsNudge && |
| 63 | + isDateSmaller(hoursBefore(new Date().toISOString(), 24 * 365), user.additionalData.lastUpdatedDetailsNudge) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param state |
| 69 | + * @returns error message or empty string when valid |
| 70 | + */ |
| 71 | + export function validate(state: ProfileState): string { |
| 72 | + if (state.name.trim() === "") { |
| 73 | + return "Name must not be empty."; |
| 74 | + } |
| 75 | + if (state.email.trim() === "") { |
| 76 | + return "Email must not be empty."; |
| 77 | + } |
| 78 | + if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(state.email.trim())) { |
| 79 | + return "Please enter a valid email."; |
| 80 | + } |
| 81 | + return ""; |
| 82 | + } |
| 83 | + |
| 84 | + export function NudgeForProfileUpdateModal() { |
| 85 | + const { user, setUser } = useContext(UserContext); |
| 86 | + const original = ProfileState.getProfileState(user!); |
| 87 | + const [profileState, setProfileState] = useState(original); |
| 88 | + const [errorMessage, setErrorMessage] = useState(""); |
| 89 | + const [visible, setVisible] = useState(shouldNudgeForUpdate(user!)); |
| 90 | + |
| 91 | + const saveProfileState = () => { |
| 92 | + const error = ProfileState.validate(profileState); |
| 93 | + setErrorMessage(error); |
| 94 | + if (error) { |
| 95 | + return; |
| 96 | + } |
| 97 | + const updatedUser = ProfileState.setProfileState(user!, profileState); |
| 98 | + setUser(updatedUser); |
| 99 | + getGitpodService().server.updateLoggedInUser(updatedUser); |
| 100 | + setVisible(shouldNudgeForUpdate(updatedUser!)); |
| 101 | + }; |
| 102 | + |
| 103 | + const cancelProfileUpdate = () => { |
| 104 | + setProfileState(original); |
| 105 | + saveProfileState(); |
| 106 | + }; |
| 107 | + |
| 108 | + return ( |
| 109 | + <Modal |
| 110 | + title="Update Your Profile Information" |
| 111 | + visible={visible} |
| 112 | + onClose={cancelProfileUpdate} |
| 113 | + closeable={true} |
| 114 | + className="_max-w-xl" |
| 115 | + buttons={ |
| 116 | + <div> |
| 117 | + <button className="secondary" onClick={cancelProfileUpdate}> |
| 118 | + Cancel |
| 119 | + </button> |
| 120 | + <button onClick={saveProfileState}>Save</button> |
| 121 | + </div> |
| 122 | + } |
| 123 | + > |
| 124 | + <ProfileInformation |
| 125 | + profileState={profileState} |
| 126 | + errorMessage={errorMessage} |
| 127 | + updated={false} |
| 128 | + setProfileState={setProfileState} |
| 129 | + /> |
| 130 | + </Modal> |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export default function ProfileInformation(props: { |
| 136 | + profileState: ProfileState.ProfileState; |
| 137 | + setProfileState: (newState: ProfileState.ProfileState) => void; |
| 138 | + errorMessage: string; |
| 139 | + updated: boolean; |
| 140 | + children?: React.ReactChild[] | React.ReactChild; |
| 141 | +}) { |
| 142 | + return ( |
| 143 | + <div> |
| 144 | + <h3>Profile</h3> |
| 145 | + <p className="text-base text-gray-500 pb-4 max-w-2xl"> |
| 146 | + The following information will be used to set up Git configuration. You can override Git author name and |
| 147 | + email per project by using the default environment variables <CodeText>GIT_AUTHOR_NAME</CodeText>,{" "} |
| 148 | + <CodeText>GIT_COMMITTER_NAME</CodeText>, <CodeText>GIT_AUTHOR_EMAIL</CodeText> and{" "} |
| 149 | + <CodeText>GIT_COMMITTER_EMAIL</CodeText>. |
| 150 | + </p> |
| 151 | + {props.errorMessage.length > 0 ? ( |
| 152 | + <div className="dark:bg-gray-800 bg-gitpod-kumquat-light rounded-md p-3 text-gitpod-red text-sm mb-2"> |
| 153 | + {props.errorMessage} |
| 154 | + </div> |
| 155 | + ) : null} |
| 156 | + {props.updated ? ( |
| 157 | + <div className="dark:bg-gray-800 bg-gray-300 rounded-md p-3 text-gray-700 dark:text-gray-200 text-sm mb-2"> |
| 158 | + Profile information has been updated. |
| 159 | + </div> |
| 160 | + ) : null} |
| 161 | + <div className="flex flex-col lg:flex-row"> |
| 162 | + <div> |
| 163 | + <div className="mt-4"> |
| 164 | + <h4>Name</h4> |
| 165 | + <input |
| 166 | + type="text" |
| 167 | + value={props.profileState.name} |
| 168 | + onChange={(e) => props.setProfileState({ ...props.profileState, name: e.target.value })} |
| 169 | + /> |
| 170 | + </div> |
| 171 | + <div className="mt-4"> |
| 172 | + <h4>Email</h4> |
| 173 | + <input |
| 174 | + type="text" |
| 175 | + value={props.profileState.email} |
| 176 | + onChange={(e) => props.setProfileState({ ...props.profileState, email: e.target.value })} |
| 177 | + /> |
| 178 | + </div> |
| 179 | + <div className="mt-4"> |
| 180 | + <h4>Company</h4> |
| 181 | + <input |
| 182 | + type="text" |
| 183 | + value={props.profileState.company} |
| 184 | + onChange={(e) => props.setProfileState({ ...props.profileState, company: e.target.value })} |
| 185 | + /> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + <div className="lg:pl-14"> |
| 189 | + <div className="mt-4"> |
| 190 | + <h4>Avatar</h4> |
| 191 | + <img |
| 192 | + className="rounded-full w-24 h-24" |
| 193 | + src={props.profileState.avatarURL} |
| 194 | + alt={props.profileState.name} |
| 195 | + /> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + {props.children || null} |
| 200 | + </div> |
| 201 | + ); |
| 202 | +} |
0 commit comments