This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Emails page redesign #15778
Merged
Merged
Emails page redesign #15778
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a0c8424
cleanup before mergin main
artemruts 103c8aa
Merge branch 'main' into cloud-emails-redesign-15409
artemruts 5299228
Beta version
artemruts 476fcef
Remove unused imports
artemruts 91c4201
Ready for comments
artemruts 14c0772
Formatting and lint fixes
artemruts 92849c4
Addressing comments - 1
artemruts 1b65ca7
Addressing comments - 2
artemruts 01676bc
Addressing comments - 3
artemruts dc785a2
Minor tweaks to the select
artemruts ee20854
Adjust padding
artemruts 09863eb
Simplifying code
artemruts 4330f6a
Allow consumers to override useInputValidation state, use in AddUserE…
tjkandala 391ca02
Remove hook changes and simplify css
artemruts 9f7f08f
Replace custom css with utils
artemruts 3150b9e
Remove unneeded styles
artemruts f0ef192
fix stale onEmailRemove
tjkandala 8bd340b
fix loader input spinner
tjkandala 02ebdb3
Merge branch 'main' into cloud-emails-redesign-15409
tjkandala 3ed6dda
dedup storybook import
tjkandala 633650b
fix LoaderInput story
tjkandala edabd7c
Attempt to cast types
artemruts 6b6aa09
Update client/shared/src/util/useInputValidation.test.ts
artemruts 8760033
Addressing feedback
artemruts 4da6551
Merge branch 'cloud-emails-redesign-15409' of https://github.com/sour…
artemruts 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
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
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
193 changes: 99 additions & 94 deletions
193
client/web/src/user/settings/emails/AddUserEmailForm.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 |
---|---|---|
@@ -1,119 +1,124 @@ | ||
import * as React from 'react' | ||
import { merge, Observable, of, Subject, Subscription } from 'rxjs' | ||
import { catchError, map, switchMap, tap } from 'rxjs/operators' | ||
import { gql } from '../../../../../shared/src/graphql/graphql' | ||
import * as GQL from '../../../../../shared/src/graphql/schema' | ||
import { createAggregateError, ErrorLike } from '../../../../../shared/src/util/errors' | ||
import { mutateGraphQL } from '../../../backend/graphql' | ||
import { Form } from '../../../../../branded/src/components/Form' | ||
import React, { FunctionComponent, useMemo, useState } from 'react' | ||
import classNames from 'classnames' | ||
import * as H from 'history' | ||
|
||
import { AddUserEmailResult, AddUserEmailVariables } from '../../../graphql-operations' | ||
import { gql, dataOrThrowErrors } from '../../../../../shared/src/graphql/graphql' | ||
import { requestGraphQL } from '../../../backend/graphql' | ||
import { asError, isErrorLike, ErrorLike } from '../../../../../shared/src/util/errors' | ||
import { useInputValidation, deriveInputClassName } from '../../../../../shared/src/util/useInputValidation' | ||
|
||
import { eventLogger } from '../../../tracking/eventLogger' | ||
import { ErrorAlert } from '../../../components/alerts' | ||
import * as H from 'history' | ||
import { LoaderButton } from '../../../components/LoaderButton' | ||
import { LoaderInput } from '../../../../../branded/src/components/LoaderInput' | ||
|
||
interface Props { | ||
/** The GraphQL ID of the user with whom the new emails are associated. */ | ||
user: GQL.ID | ||
|
||
/** Called after successfully adding an email to the user. */ | ||
user: string | ||
onDidAdd: () => void | ||
history: H.History | ||
|
||
className?: string | ||
history: H.History | ||
} | ||
|
||
interface State { | ||
email: string | ||
error?: ErrorLike | null | ||
} | ||
type Status = undefined | 'loading' | ErrorLike | ||
|
||
export class AddUserEmailForm extends React.PureComponent<Props, State> { | ||
public state: State = { email: '', error: null } | ||
export const AddUserEmailForm: FunctionComponent<Props> = ({ user, className, onDidAdd, history }) => { | ||
const [statusOrError, setStatusOrError] = useState<Status>() | ||
|
||
private submits = new Subject<React.FormEvent<HTMLFormElement>>() | ||
private subscriptions = new Subscription() | ||
const [emailState, nextEmailFieldChange, emailInputReference, overrideEmailState] = useInputValidation( | ||
useMemo( | ||
() => ({ | ||
synchronousValidators: [], | ||
asynchronousValidators: [], | ||
}), | ||
[] | ||
) | ||
) | ||
|
||
public componentDidMount(): void { | ||
this.subscriptions.add( | ||
this.submits | ||
.pipe( | ||
tap(event => event.preventDefault()), | ||
switchMap(() => | ||
merge( | ||
of<Pick<State, 'error'>>({ error: undefined }), | ||
this.addUserEmail(this.state.email).pipe( | ||
tap(() => this.props.onDidAdd()), | ||
map(() => ({ error: null, email: '' })), | ||
catchError(error => [{ error, email: this.state.email }]) | ||
) | ||
) | ||
) | ||
) | ||
.subscribe( | ||
stateUpdate => this.setState(stateUpdate), | ||
error => console.error(error) | ||
const onSubmit: React.FormEventHandler<HTMLFormElement> = async event => { | ||
event.preventDefault() | ||
|
||
if (emailState.kind === 'VALID') { | ||
setStatusOrError('loading') | ||
|
||
try { | ||
dataOrThrowErrors( | ||
await requestGraphQL<AddUserEmailResult, AddUserEmailVariables>( | ||
gql` | ||
mutation AddUserEmail($user: ID!, $email: String!) { | ||
addUserEmail(user: $user, email: $email) { | ||
alwaysNil | ||
} | ||
} | ||
`, | ||
{ user, email: emailState.value } | ||
).toPromise() | ||
) | ||
) | ||
} | ||
|
||
public componentWillUnmount(): void { | ||
this.subscriptions.unsubscribe() | ||
eventLogger.log('NewUserEmailAddressAdded') | ||
overrideEmailState({ value: '' }) | ||
setStatusOrError(undefined) | ||
|
||
if (onDidAdd) { | ||
onDidAdd() | ||
} | ||
} catch (error) { | ||
setStatusOrError(asError(error)) | ||
} | ||
} | ||
} | ||
|
||
public render(): JSX.Element | null { | ||
const loading = this.state.error === undefined | ||
return ( | ||
<div className={`add-user-email-form ${this.props.className || ''}`}> | ||
<h3>Add email address</h3> | ||
<Form className="form-inline" onSubmit={this.onSubmit}> | ||
<label className="sr-only" htmlFor="AddUserEmailForm-email"> | ||
Email address | ||
</label> | ||
return ( | ||
<div className={`add-user-email-form ${className || ''}`}> | ||
<label | ||
htmlFor="AddUserEmailForm-email" | ||
className={classNames('align-self-start', { | ||
'text-danger font-weight-bold': emailState.kind === 'INVALID', | ||
})} | ||
> | ||
Email address | ||
</label> | ||
{/* eslint-disable-next-line react/forbid-elements */} | ||
<form className="form-inline" onSubmit={onSubmit} noValidate={true}> | ||
<LoaderInput | ||
className={(deriveInputClassName(emailState), 'mr-sm-2')} | ||
loading={emailState.kind === 'LOADING'} | ||
> | ||
<input | ||
id="AddUserEmailForm-email" | ||
type="email" | ||
name="email" | ||
className="form-control mr-sm-2 test-user-email-add-input" | ||
id="AddUserEmailForm-email" | ||
onChange={this.onChange} | ||
className={classNames( | ||
'form-control test-user-email-add-input', | ||
deriveInputClassName(emailState) | ||
)} | ||
onChange={nextEmailFieldChange} | ||
size={32} | ||
value={this.state.email} | ||
value={emailState.value} | ||
ref={emailInputReference} | ||
required={true} | ||
autoComplete="email" | ||
autoCorrect="off" | ||
spellCheck={false} | ||
autoCapitalize="off" | ||
readOnly={loading} | ||
placeholder="Email" | ||
/>{' '} | ||
<button type="submit" className="btn btn-primary" disabled={loading}> | ||
{loading ? 'Adding...' : 'Add'} | ||
</button> | ||
</Form> | ||
{this.state.error && ( | ||
<ErrorAlert className="mt-2" error={this.state.error} history={this.props.history} /> | ||
spellCheck={false} | ||
readOnly={false} | ||
/> | ||
</LoaderInput>{' '} | ||
<LoaderButton | ||
loading={statusOrError === 'loading'} | ||
label="Add" | ||
type="submit" | ||
disabled={statusOrError === 'loading' || emailState.kind !== 'VALID'} | ||
className="btn btn-primary" | ||
/> | ||
{emailState.kind === 'INVALID' && ( | ||
<small className="invalid-feedback" role="alert"> | ||
{emailState.reason} | ||
</small> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
private onChange: React.ChangeEventHandler<HTMLInputElement> = event => | ||
this.setState({ email: event.currentTarget.value }) | ||
private onSubmit: React.FormEventHandler<HTMLFormElement> = event => this.submits.next(event) | ||
|
||
private addUserEmail = (email: string): Observable<void> => | ||
mutateGraphQL( | ||
gql` | ||
mutation AddUserEmail($user: ID!, $email: String!) { | ||
addUserEmail(user: $user, email: $email) { | ||
alwaysNil | ||
} | ||
} | ||
`, | ||
{ user: this.props.user, email } | ||
).pipe( | ||
map(({ data, errors }) => { | ||
if (!data || (errors && errors.length > 0)) { | ||
throw createAggregateError(errors) | ||
} | ||
eventLogger.log('NewUserEmailAddressAdded') | ||
}) | ||
) | ||
</form> | ||
{isErrorLike(statusOrError) && <ErrorAlert className="mt-2" error={statusOrError} history={history} />} | ||
</div> | ||
) | ||
} |
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.
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.
There's a false-positive with
act
calls in@testing-library/react-hooks