-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
refactor: use solid simple modals (@fehmer) #8044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fehmer
wants to merge
18
commits into
master
Choose a base branch
from
feature/migrate-simple-modals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c9e733e
refactor: use solid simple modals (@fehmer)
fehmer 08b6a6f
ape key table
fehmer c0a7231
friends
fehmer ec16050
UpdateNameModal, move authentication utils(circle)
fehmer 74719e7
forgot a file
fehmer bc313ed
update email, extract getPasswordSchema, revert handlers for account-…
fehmer a3ad507
Merge branch 'master' into feature/migrate-simple-modals
fehmer 7c26ada
updatePassword
fehmer 6292501
remove auth modal
fehmer 489886f
review comments
fehmer dce531c
add password auth modal
fehmer 313e006
delete/reset account
fehmer 5433731
move modals with reauth+confirm, last old modals
fehmer f8246bd
review comments
fehmer 7c5b7f4
cleanup
fehmer 5980131
Merge branch 'master' into feature/migrate-simple-modals
fehmer cec5391
style fixes, remove obsolete modals and css
fehmer ada44bb
fix datepicker color on dark themes, extract zod helpers, unwrap sche…
fehmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
frontend/src/ts/components/modals/account-settings/RemoveAuthMethodModal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { unlink } from "firebase/auth"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { isAuthenticated } from "../../../states/core"; | ||
| import { showNoticeNotification } from "../../../states/notifications"; | ||
| import { showSimpleModal } from "../../../states/simple-modal"; | ||
| import { createErrorMessage } from "../../../utils/error"; | ||
| import { | ||
| AuthMethod, | ||
| getPasswordSchema, | ||
| isUsingGithubAuthentication, | ||
| isUsingGoogleAuthentication, | ||
| isUsingPasswordAuthentication, | ||
| reauthenticate, | ||
| } from "../../../utils/firebase-auth"; | ||
| import { reloadAfter } from "../../../utils/misc"; | ||
|
|
||
| export function showRemoveAuthMethodModal(options: { | ||
| authMethod: AuthMethod; | ||
| callback: () => void; | ||
| }): void { | ||
| if (!isAuthenticated()) return; | ||
|
|
||
| //check there is at least one authentication remaining | ||
| const hasRemainingAuth = [ | ||
| isUsingPasswordAuthentication() && options.authMethod !== "password", | ||
| isUsingGithubAuthentication() && options.authMethod !== "github.com", | ||
| isUsingGoogleAuthentication() && options.authMethod !== "google.com", | ||
| ].find((it) => it); | ||
|
|
||
| if (!hasRemainingAuth) { | ||
| showNoticeNotification("No remaining authentication enabled"); | ||
| return; | ||
| } | ||
|
|
||
| const methodDisplay = | ||
|
fehmer marked this conversation as resolved.
Outdated
|
||
| options.authMethod === "password" | ||
| ? "Password" | ||
| : options.authMethod === "github.com" | ||
| ? "GitHub" | ||
| : "Google"; | ||
|
|
||
| showSimpleModal({ | ||
| title: `Remove ${methodDisplay} authentication`, | ||
| buttonText: "remove", | ||
| buttonAlwaysEnabled: options.authMethod !== "password", | ||
| schema: z.object({ | ||
| password: getPasswordSchema(), | ||
| checked: z.literal(true), | ||
| }), | ||
| inputs: { | ||
| password: { | ||
| placeholder: "Password", | ||
| type: "password", | ||
| hidden: | ||
| !isUsingPasswordAuthentication() || options.authMethod === "password", | ||
| }, | ||
| checked: { | ||
| type: "checkbox", | ||
| label: `I understand I will lose access to my Monkeytype account if my Google/GitHub account is lost or disabled.`, | ||
| hidden: options.authMethod !== "password", | ||
| }, | ||
| }, | ||
|
|
||
| execFn: async ({ password }) => { | ||
| const reauth = await reauthenticate({ | ||
| password, | ||
| excludeMethod: options.authMethod, | ||
| }); | ||
| if (reauth.status !== "success") { | ||
| return { | ||
| status: reauth.status, | ||
| message: reauth.message, | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| await unlink(reauth.user, options.authMethod); | ||
| } catch (e) { | ||
| const message = createErrorMessage( | ||
| e, | ||
| options.authMethod === "password" | ||
| ? "Failed to remove password authentication" | ||
| : `Failed to unlink ${methodDisplay} account`, | ||
| ); | ||
| return { | ||
| status: "error", | ||
| message, | ||
| }; | ||
| } | ||
|
|
||
| options.callback(); | ||
|
|
||
| reloadAfter(3); | ||
| return { | ||
| status: "success", | ||
| message: `${methodDisplay} authentication removed`, | ||
| }; | ||
| }, | ||
| }); | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
frontend/src/ts/components/modals/account-settings/UpdateEmailModal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { UserEmailSchema } from "@monkeytype/schemas/users"; | ||
| import { z } from "zod"; | ||
|
|
||
| import Ape from "../../../ape"; | ||
| import { signOut } from "../../../auth"; | ||
| import { isAuthenticated } from "../../../states/core"; | ||
| import { showNoticeNotification } from "../../../states/notifications"; | ||
| import { showSimpleModal } from "../../../states/simple-modal"; | ||
| import { | ||
| getPasswordSchema, | ||
| isUsingPasswordAuthentication, | ||
| reauthenticate, | ||
| } from "../../../utils/firebase-auth"; | ||
|
|
||
| export function showUpdateEmailModal(): void { | ||
| if (!isAuthenticated()) return; | ||
| if (!isUsingPasswordAuthentication()) { | ||
| showNoticeNotification("Password authentication is not enabled"); | ||
| return; | ||
| } | ||
|
|
||
| showSimpleModal({ | ||
| title: "Update email", | ||
| buttonText: "update", | ||
| schema: z.object({ | ||
| password: getPasswordSchema(), | ||
| email: UserEmailSchema, | ||
| emailConfirm: UserEmailSchema, | ||
| }), | ||
| inputs: { | ||
| password: { | ||
| placeholder: "Password", | ||
| type: "password", | ||
| initVal: "", | ||
| }, | ||
| email: { | ||
| type: "text", | ||
| placeholder: "New email", | ||
| initVal: "", | ||
| }, | ||
| emailConfirm: { | ||
| type: "text", | ||
| placeholder: "Confirm new email", | ||
| initVal: "", | ||
| }, | ||
| }, | ||
|
|
||
| execFn: async ({ password, email, emailConfirm }) => { | ||
| if (email !== emailConfirm) { | ||
| return { | ||
| status: "notice", | ||
| message: "Emails don't match", | ||
| }; | ||
| } | ||
|
|
||
| const reauth = await reauthenticate({ password }); | ||
| if (reauth.status !== "success") { | ||
| return { | ||
| status: reauth.status, | ||
| message: reauth.message, | ||
| }; | ||
| } | ||
|
|
||
| const response = await Ape.users.updateEmail({ | ||
| body: { | ||
| newEmail: email, | ||
| previousEmail: reauth.user.email as string, | ||
| }, | ||
| }); | ||
|
|
||
| if (response.status !== 200) { | ||
| return { | ||
| status: "error", | ||
| message: "Failed to update email", | ||
| notificationOptions: { response }, | ||
| }; | ||
| } | ||
|
|
||
| signOut(); | ||
|
|
||
| return { | ||
| status: "success", | ||
| message: "Email updated", | ||
| }; | ||
| }, | ||
| }); | ||
| } |
86 changes: 86 additions & 0 deletions
86
frontend/src/ts/components/modals/account-settings/UpdateNameModal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { UserNameSchema } from "@monkeytype/schemas/users"; | ||
| import { z } from "zod"; | ||
|
|
||
| import Ape from "../../../ape"; | ||
| import * as DB from "../../../db"; | ||
| import { isAuthenticated } from "../../../states/core"; | ||
| import { showSimpleModal } from "../../../states/simple-modal"; | ||
| import { | ||
| getPasswordSchema, | ||
| isUsingPasswordAuthentication, | ||
| reauthenticate, | ||
| } from "../../../utils/firebase-auth"; | ||
| import { reloadAfter } from "../../../utils/misc"; | ||
| import { remoteValidation } from "../../../utils/remote-validation"; | ||
|
|
||
| export function showUpdateNameModal(): void { | ||
| const snapshot = DB.getSnapshot(); | ||
| if (!isAuthenticated() || !snapshot) return; | ||
|
|
||
| showSimpleModal({ | ||
| title: "Update name", | ||
| buttonText: isUsingPasswordAuthentication() | ||
| ? "reauthenticate to update" | ||
| : "update", | ||
| text: DB.getSnapshot()?.needsToChangeName | ||
| ? "You need to change your account name. This might be because you have a duplicate name, no account name or your name is not allowed (contains whitespace or invalid characters). Sorry for the inconvenience." | ||
| : undefined, | ||
| schema: z.object({ | ||
| password: getPasswordSchema(), | ||
| newName: UserNameSchema, | ||
| }), | ||
| inputs: { | ||
| password: { | ||
| placeholder: "password", | ||
| type: "password", | ||
| initVal: "", | ||
| hidden: !isUsingPasswordAuthentication(), | ||
| }, | ||
| newName: { | ||
| placeholder: "new name", | ||
| type: "text", | ||
| initVal: "", | ||
| validation: { | ||
| isValid: remoteValidation( | ||
| async (name: string) => | ||
| Ape.users.getNameAvailability({ params: { name } }), | ||
| { check: (data) => data.available || "Name not available" }, | ||
| ), | ||
| debounceDelay: 1000, | ||
| }, | ||
| }, | ||
| }, | ||
|
|
||
| execFn: async ({ password, newName }) => { | ||
| const reauth = await reauthenticate({ password }); | ||
| if (reauth.status !== "success") { | ||
| return { | ||
| status: reauth.status, | ||
| message: reauth.message, | ||
| }; | ||
| } | ||
|
|
||
| const response = await Ape.users.updateName({ | ||
| body: { name: newName }, | ||
| }); | ||
| if (response.status !== 200) { | ||
| return { | ||
| status: "error", | ||
| message: "Failed to update name", | ||
| notificationOptions: { response }, | ||
| }; | ||
| } | ||
|
|
||
| snapshot.name = newName; | ||
| DB.setSnapshot(snapshot); | ||
| if (snapshot.needsToChangeName) { | ||
| reloadAfter(2); | ||
| } | ||
|
|
||
| return { | ||
| status: "success", | ||
| message: "Name updated", | ||
| }; | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion should be addressed after the previous review comment.
Also need to export
isUsingAuthenticationandauthMethods, and removeisUsingGithubAuthenticationandisUsingGoogleAuthentication.