Normalize convetions
This commit is contained in:
@@ -19,35 +19,35 @@ import {Check_Stroke2_Corner0_Rounded as Check} from '#/components/icons/Check'
|
|||||||
import {Envelope_Stroke2_Corner0_Rounded as Envelope} from '#/components/icons/Envelope'
|
import {Envelope_Stroke2_Corner0_Rounded as Envelope} from '#/components/icons/Envelope'
|
||||||
import {createStaticClick, InlineLinkText} from '#/components/Link'
|
import {createStaticClick, InlineLinkText} from '#/components/Link'
|
||||||
import {Loader} from '#/components/Loader'
|
import {Loader} from '#/components/Loader'
|
||||||
import {Span,Text} from '#/components/Typography'
|
import {Span, Text} from '#/components/Typography'
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
error: string
|
error: string
|
||||||
stage: 'email' | 'token'
|
step: 'email' | 'token'
|
||||||
emailStatus: 'pending' | 'success' | 'error' | 'default'
|
emailStatus: 'pending' | 'success' | 'error' | 'default'
|
||||||
tokenStatus: 'pending' | 'success' | 'error' | 'default'
|
tokenStatus: 'pending' | 'success' | 'error' | 'default'
|
||||||
}
|
}
|
||||||
|
|
||||||
type Action =
|
type Action =
|
||||||
| {
|
| {
|
||||||
id: 'setError'
|
type: 'setError'
|
||||||
error: string
|
error: string
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
id: 'setStage'
|
type: 'setStep'
|
||||||
stage: 'email' | 'token'
|
step: 'email' | 'token'
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
id: 'setEmailStatus'
|
type: 'setEmailStatus'
|
||||||
status: State['emailStatus']
|
status: State['emailStatus']
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
id: 'setTokenStatus'
|
type: 'setTokenStatus'
|
||||||
status: State['tokenStatus']
|
status: State['tokenStatus']
|
||||||
}
|
}
|
||||||
|
|
||||||
function reducer(state: State, action: Action): State {
|
function reducer(state: State, action: Action): State {
|
||||||
switch (action.id) {
|
switch (action.type) {
|
||||||
case 'setError': {
|
case 'setError': {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -56,11 +56,11 @@ function reducer(state: State, action: Action): State {
|
|||||||
tokenStatus: 'error',
|
tokenStatus: 'error',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 'setStage': {
|
case 'setStep': {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
error: '',
|
error: '',
|
||||||
stage: action.stage,
|
step: action.step,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 'setEmailStatus': {
|
case 'setEmailStatus': {
|
||||||
@@ -94,18 +94,18 @@ export function Disable() {
|
|||||||
const [token, setToken] = useState('')
|
const [token, setToken] = useState('')
|
||||||
const [state, dispatch] = useReducer(reducer, {
|
const [state, dispatch] = useReducer(reducer, {
|
||||||
error: '',
|
error: '',
|
||||||
stage: 'email',
|
step: 'email',
|
||||||
emailStatus: 'default',
|
emailStatus: 'default',
|
||||||
tokenStatus: 'default',
|
tokenStatus: 'default',
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleSendEmail = async () => {
|
const handleSendEmail = async () => {
|
||||||
dispatch({id: 'setEmailStatus', status: 'pending'})
|
dispatch({type: 'setEmailStatus', status: 'pending'})
|
||||||
try {
|
try {
|
||||||
await wait(1000, requestEmailUpdate())
|
await wait(1000, requestEmailUpdate())
|
||||||
dispatch({id: 'setEmailStatus', status: 'success'})
|
dispatch({type: 'setEmailStatus', status: 'success'})
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
dispatch({id: 'setStage', stage: 'token'})
|
dispatch({type: 'setStep', step: 'token'})
|
||||||
}, 1000)
|
}, 1000)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Manage2FA: email update code request failed', {
|
logger.error('Manage2FA: email update code request failed', {
|
||||||
@@ -113,25 +113,25 @@ export function Disable() {
|
|||||||
})
|
})
|
||||||
// TODO rate limit
|
// TODO rate limit
|
||||||
dispatch({
|
dispatch({
|
||||||
id: 'setError',
|
type: 'setError',
|
||||||
error: _(msg`Failed to send email, please try again.`),
|
error: _(msg`Failed to send email, please try again.`),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleManageEmail2FA = async () => {
|
const handleManageEmail2FA = async () => {
|
||||||
dispatch({id: 'setTokenStatus', status: 'pending'})
|
dispatch({type: 'setTokenStatus', status: 'pending'})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await wait(1000, manageEmail2FA({enabled: false, token}))
|
await wait(1000, manageEmail2FA({enabled: false, token}))
|
||||||
dispatch({id: 'setTokenStatus', status: 'success'})
|
dispatch({type: 'setTokenStatus', status: 'success'})
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
control.close()
|
control.close()
|
||||||
}, 1000)
|
}, 1000)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Manage2FA: disable email 2FA failed', {safeMessage: e})
|
logger.error('Manage2FA: disable email 2FA failed', {safeMessage: e})
|
||||||
dispatch({
|
dispatch({
|
||||||
id: 'setError',
|
type: 'setError',
|
||||||
error: _(msg`Update to email 2FA settings failed`),
|
error: _(msg`Update to email 2FA settings failed`),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -143,7 +143,7 @@ export function Disable() {
|
|||||||
<Trans>Disable email 2FA</Trans>
|
<Trans>Disable email 2FA</Trans>
|
||||||
</Text>
|
</Text>
|
||||||
|
|
||||||
{state.stage === 'email' ? (
|
{state.step === 'email' ? (
|
||||||
<>
|
<>
|
||||||
<Text
|
<Text
|
||||||
style={[a.text_sm, a.leading_snug, t.atoms.text_contrast_medium]}>
|
style={[a.text_sm, a.leading_snug, t.atoms.text_contrast_medium]}>
|
||||||
@@ -186,7 +186,7 @@ export function Disable() {
|
|||||||
<InlineLinkText
|
<InlineLinkText
|
||||||
label={_(msg`Enter code`)}
|
label={_(msg`Enter code`)}
|
||||||
{...createStaticClick(() => {
|
{...createStaticClick(() => {
|
||||||
dispatch({id: 'setStage', stage: 'token'})
|
dispatch({type: 'setStep', step: 'token'})
|
||||||
})}>
|
})}>
|
||||||
Click here.
|
Click here.
|
||||||
</InlineLinkText>
|
</InlineLinkText>
|
||||||
|
|||||||
@@ -22,16 +22,16 @@ type State = {
|
|||||||
|
|
||||||
type Action =
|
type Action =
|
||||||
| {
|
| {
|
||||||
id: 'setError'
|
type: 'setError'
|
||||||
error: string
|
error: string
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
id: 'setStatus'
|
type: 'setStatus'
|
||||||
status: State['status']
|
status: State['status']
|
||||||
}
|
}
|
||||||
|
|
||||||
function reducer(state: State, action: Action): State {
|
function reducer(state: State, action: Action): State {
|
||||||
switch (action.id) {
|
switch (action.type) {
|
||||||
case 'setError': {
|
case 'setError': {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -65,18 +65,18 @@ export function Enable() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const handleManageEmail2FA = async () => {
|
const handleManageEmail2FA = async () => {
|
||||||
dispatch({id: 'setStatus', status: 'pending'})
|
dispatch({type: 'setStatus', status: 'pending'})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await wait(1000, manageEmail2FA({enabled: true}))
|
await wait(1000, manageEmail2FA({enabled: true}))
|
||||||
dispatch({id: 'setStatus', status: 'success'})
|
dispatch({type: 'setStatus', status: 'success'})
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
control.close()
|
control.close()
|
||||||
}, 1000)
|
}, 1000)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Manage2FA: enable email 2FA failed', {safeMessage: e})
|
logger.error('Manage2FA: enable email 2FA failed', {safeMessage: e})
|
||||||
dispatch({
|
dispatch({
|
||||||
id: 'setError',
|
type: 'setError',
|
||||||
error: _(msg`Update to email 2FA settings failed`),
|
error: _(msg`Update to email 2FA settings failed`),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user