2022-03-21 11:09:01 -07:00
import * as React from 'react' ;
import { defineMessages , useIntl } from 'react-intl' ;
import { deleteAccount } from 'soapbox/actions/security' ;
import snackbar from 'soapbox/actions/snackbar' ;
2022-07-22 10:30:16 -07:00
import { Button , Card , CardBody , CardHeader , CardTitle , Form , FormActions , FormGroup , Input , Stack , Text } from 'soapbox/components/ui' ;
2022-05-20 23:42:39 -07:00
import { useAppDispatch , useFeatures } from 'soapbox/hooks' ;
2022-03-21 11:09:01 -07:00
const messages = defineMessages ( {
passwordFieldLabel : { id : 'security.fields.password.label' , defaultMessage : 'Password' } ,
deleteHeader : { id : 'security.headers.delete' , defaultMessage : 'Delete Account' } ,
deleteText : { id : 'security.text.delete' , defaultMessage : 'To delete your account, enter your password then click Delete Account. This is a permanent action that cannot be undone. Your account will be destroyed from this server, and a deletion request will be sent to other servers. It\'s not guaranteed that all servers will purge your account.' } ,
2022-05-20 23:42:39 -07:00
localDeleteText : { id : 'security.text.delete.local' , defaultMessage : 'To delete your account, enter your password then click Delete Account. This is a permanent action that cannot be undone.' } ,
2022-03-21 11:09:01 -07:00
deleteSubmit : { id : 'security.submit.delete' , defaultMessage : 'Delete Account' } ,
deleteAccountSuccess : { id : 'security.delete_account.success' , defaultMessage : 'Account successfully deleted.' } ,
deleteAccountFail : { id : 'security.delete_account.fail' , defaultMessage : 'Account deletion failed.' } ,
} ) ;
const DeleteAccount = ( ) = > {
const intl = useIntl ( ) ;
2022-04-14 09:10:46 -07:00
const dispatch = useAppDispatch ( ) ;
2022-05-20 23:42:39 -07:00
const features = useFeatures ( ) ;
2022-03-21 11:09:01 -07:00
const [ password , setPassword ] = React . useState ( '' ) ;
const [ isLoading , setLoading ] = React . useState ( false ) ;
2022-04-14 09:10:46 -07:00
const handleInputChange = React . useCallback ( ( event : React.ChangeEvent < HTMLInputElement > ) = > {
2022-03-21 11:09:01 -07:00
event . persist ( ) ;
setPassword ( event . target . value ) ;
} , [ ] ) ;
const handleSubmit = React . useCallback ( ( ) = > {
setLoading ( true ) ;
2022-06-19 11:38:51 -07:00
dispatch ( deleteAccount ( password ) ) . then ( ( ) = > {
2022-03-21 11:09:01 -07:00
setPassword ( '' ) ;
dispatch ( snackbar . success ( intl . formatMessage ( messages . deleteAccountSuccess ) ) ) ;
} ) . finally ( ( ) = > {
setLoading ( false ) ;
} ) . catch ( ( ) = > {
setPassword ( '' ) ;
dispatch ( snackbar . error ( intl . formatMessage ( messages . deleteAccountFail ) ) ) ;
} ) ;
} , [ password , dispatch , intl ] ) ;
return (
< Card variant = 'rounded' >
< CardHeader backHref = '/settings' >
< CardTitle title = { intl . formatMessage ( messages . deleteHeader ) } / >
< / CardHeader >
< CardBody >
2022-07-22 10:30:16 -07:00
< Stack space = { 4 } >
< Text theme = 'muted' >
2022-03-21 11:09:01 -07:00
2022-07-22 10:30:16 -07:00
{ intl . formatMessage ( features . federating ? messages.deleteText : messages.localDeleteText ) }
< / Text >
2022-03-21 11:09:01 -07:00
2022-07-22 10:30:16 -07:00
< Form onSubmit = { handleSubmit } >
< FormGroup labelText = { intl . formatMessage ( messages . passwordFieldLabel ) } >
< Input
type = 'password'
name = 'password'
onChange = { handleInputChange }
value = { password }
/ >
< / FormGroup >
2022-03-21 11:09:01 -07:00
2022-07-22 10:30:16 -07:00
< FormActions >
< Button type = 'submit' theme = 'danger' disabled = { isLoading } >
{ intl . formatMessage ( messages . deleteSubmit ) }
< / Button >
< / FormActions >
< / Form >
< / Stack >
2022-03-21 11:09:01 -07:00
< / CardBody >
2022-07-22 10:30:16 -07:00
< / C a r d >
2022-03-21 11:09:01 -07:00
) ;
} ;
export default DeleteAccount ;