-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0ed19d
commit 6afce84
Showing
5 changed files
with
239 additions
and
81 deletions.
There are no files selected for viewing
79 changes: 0 additions & 79 deletions
79
app/assets/javascripts/preferences/panes/account/ChangeEmail.tsx
This file was deleted.
Oops, something went wrong.
This file contains 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
37 changes: 37 additions & 0 deletions
37
app/assets/javascripts/preferences/panes/account/changeEmail/ChangeEmailForm.tsx
This file contains 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,37 @@ | ||
import { DecoratedInput } from '@/components/DecoratedInput'; | ||
import { StateUpdater } from 'preact/hooks'; | ||
import { FunctionalComponent } from 'preact'; | ||
import { HtmlInputTypes } from '@/enums'; | ||
|
||
type Props = { | ||
setNewEmail: StateUpdater<string> | ||
setCurrentPassword: StateUpdater<string> | ||
} | ||
export const ChangeEmailForm: FunctionalComponent<Props> = ({ | ||
setNewEmail, | ||
setCurrentPassword | ||
}) => { | ||
return ( | ||
( | ||
<> | ||
<div className={'mt-2 mb-3'}> | ||
<DecoratedInput | ||
onChange={(newEmail) => { | ||
setNewEmail(newEmail); | ||
}} | ||
placeholder={'New Email'} | ||
/> | ||
</div> | ||
<div className={'mt-2 mb-3'}> | ||
<DecoratedInput | ||
type={HtmlInputTypes.Password} | ||
onChange={(currentPassword) => { | ||
setCurrentPassword(currentPassword); | ||
}} | ||
placeholder={'Current Password'} | ||
/> | ||
</div> | ||
</> | ||
) | ||
); | ||
}; |
12 changes: 12 additions & 0 deletions
12
app/assets/javascripts/preferences/panes/account/changeEmail/ChangeEmailSuccess.tsx
This file contains 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,12 @@ | ||
import { FunctionalComponent } from 'preact'; | ||
|
||
export const ChangeEmailSuccess: FunctionalComponent = () => { | ||
return ( | ||
<> | ||
<div className={'sk-label sk-bold info'}>Your email has been successfully changed.</div> | ||
<p className={'sk-p'}> | ||
Please ensure you are running the latest version of Standard Notes on all platforms to ensure maximum compatibility. | ||
</p> | ||
</> | ||
); | ||
}; |
188 changes: 188 additions & 0 deletions
188
app/assets/javascripts/preferences/panes/account/changeEmail/index.tsx
This file contains 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,188 @@ | ||
import { useState } from '@node_modules/preact/hooks'; | ||
import { | ||
ModalDialog, | ||
ModalDialogButtons, | ||
ModalDialogDescription, | ||
ModalDialogLabel | ||
} from '@/components/shared/ModalDialog'; | ||
import { Button } from '@/components/Button'; | ||
import { FunctionalComponent } from 'preact'; | ||
import { WebApplication } from '@/ui_models/application'; | ||
import { useBeforeUnload } from '@/hooks/useBeforeUnload'; | ||
import { ChangeEmailForm } from './ChangeEmailForm'; | ||
import { ChangeEmailSuccess } from './ChangeEmailSuccess'; | ||
import { isEmailValid } from '@/utils'; | ||
|
||
enum SubmitButtonTitles { | ||
Default = 'Continue', | ||
GeneratingKeys = 'Generating Keys...', | ||
Finish = 'Finish' | ||
} | ||
|
||
enum Steps { | ||
InitialStep, | ||
FinishStep | ||
} | ||
|
||
type Props = { | ||
onCloseDialog: () => void; | ||
application: WebApplication; | ||
} | ||
|
||
export const ChangeEmail: FunctionalComponent<Props> = ({ | ||
onCloseDialog, | ||
application | ||
}) => { | ||
const [currentPassword, setCurrentPassword] = useState(''); | ||
const [newEmail, setNewEmail] = useState(''); | ||
const [isContinuing, setIsContinuing] = useState(false); | ||
const [lockContinue, setLockContinue] = useState(false); | ||
const [submitButtonTitle, setSubmitButtonTitle] = useState(SubmitButtonTitles.Default); | ||
const [currentStep, setCurrentStep] = useState(Steps.InitialStep); | ||
|
||
useBeforeUnload(); | ||
|
||
const applicationAlertService = application.alertService; | ||
|
||
const validateCurrentPassword = async () => { | ||
if (!currentPassword || currentPassword.length === 0) { | ||
applicationAlertService.alert( | ||
'Please enter your current password.' | ||
); | ||
|
||
return false; | ||
} | ||
|
||
const success = await application.validateAccountPassword(currentPassword); | ||
if (!success) { | ||
applicationAlertService.alert( | ||
'The current password you entered is not correct. Please try again.' | ||
); | ||
|
||
return false; | ||
} | ||
|
||
return success; | ||
}; | ||
|
||
const validateNewEmail = async () => { | ||
if (!isEmailValid(newEmail)) { | ||
applicationAlertService.alert('The email you entered has an invalid format. Please review your input and try again.'); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const resetProgressState = () => { | ||
setSubmitButtonTitle(SubmitButtonTitles.Default); | ||
setIsContinuing(false); | ||
}; | ||
|
||
const processEmailChange = async () => { | ||
await application.downloadBackup(); | ||
|
||
setLockContinue(true); | ||
|
||
const response = await application.changeEmail( | ||
newEmail, | ||
currentPassword, | ||
); | ||
|
||
const success = !response.error; | ||
|
||
setLockContinue(false); | ||
|
||
return success; | ||
}; | ||
|
||
const dismiss = () => { | ||
if (lockContinue) { | ||
applicationAlertService.alert( | ||
'Cannot close window until pending tasks are complete.' | ||
); | ||
} else { | ||
onCloseDialog(); | ||
} | ||
}; | ||
|
||
const handleSubmit = async () => { | ||
if (lockContinue || isContinuing) { | ||
return; | ||
} | ||
|
||
if (currentStep === Steps.FinishStep) { | ||
dismiss(); | ||
|
||
return; | ||
} | ||
|
||
setIsContinuing(true); | ||
setSubmitButtonTitle(SubmitButtonTitles.GeneratingKeys); | ||
|
||
const valid = await validateCurrentPassword() && await validateNewEmail(); | ||
|
||
if (!valid) { | ||
resetProgressState(); | ||
|
||
return; | ||
} | ||
|
||
const success = await processEmailChange(); | ||
if (!success) { | ||
resetProgressState(); | ||
|
||
return; | ||
} | ||
|
||
setIsContinuing(false); | ||
setSubmitButtonTitle(SubmitButtonTitles.Finish); | ||
setCurrentStep(Steps.FinishStep); | ||
}; | ||
|
||
const handleDialogClose = () => { | ||
if (lockContinue) { | ||
applicationAlertService.alert( | ||
'Cannot close window until pending tasks are complete.' | ||
); | ||
} else { | ||
onCloseDialog(); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<ModalDialog> | ||
<ModalDialogLabel closeDialog={handleDialogClose}> | ||
Change Email | ||
</ModalDialogLabel> | ||
<ModalDialogDescription> | ||
{currentStep === Steps.InitialStep && ( | ||
<ChangeEmailForm | ||
setNewEmail={setNewEmail} | ||
setCurrentPassword={setCurrentPassword} | ||
/> | ||
)} | ||
{currentStep === Steps.FinishStep && <ChangeEmailSuccess />} | ||
</ModalDialogDescription> | ||
<ModalDialogButtons> | ||
{currentStep === Steps.InitialStep && ( | ||
<Button | ||
className='min-w-20' | ||
type='normal' | ||
label='Cancel' | ||
onClick={handleDialogClose} | ||
/> | ||
)} | ||
<Button | ||
className='min-w-20' | ||
type='primary' | ||
label={submitButtonTitle} | ||
onClick={handleSubmit} | ||
/> | ||
</ModalDialogButtons> | ||
</ModalDialog> | ||
</div> | ||
); | ||
}; |