Skip to content
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

closed #419 #424

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions frontend/src/components/Modals/RemoveAccount.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import axios from 'axios';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useState } from 'react';

import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import { PersonFillDash } from 'react-bootstrap-icons';

import { useAuth } from '../../hooks';
import routes from '../../routes';
import FormAlert from '../Forms/FormAlert.jsx';

function RemoveAccount({ handleClose, isOpen }) {
const { t } = useTranslation();
const auth = useAuth();

const [isSubmitting, setSubmitting] = useState(false);
const userInfo = useSelector((state) => state.user.userInfo);
const initialFormState = { state: 'initial', message: '' };
const [formState, setFormState] = useState(initialFormState);

const handleRemoveAccount = async (e) => {
e.preventDefault();
setSubmitting(true);
setFormState(initialFormState);
try {
await axios.delete(routes.deleteUserPath(userInfo.id));
auth.signOut();
handleClose();
} catch (err) {
if (!err.isAxiosError) {
setFormState({
state: 'failed',
message: 'errors.unknown',
});
throw err;
} else {
setFormState({
state: 'failed',
message: 'errors.network',
});
throw err;
}
}
setSubmitting(false);
};

return (
<Modal centered onHide={handleClose} show={isOpen} size="md">
<Modal.Body>
<div className="d-flex flex-column gap-3 text-center">
<FormAlert
onClose={() => setFormState(initialFormState)}
state={formState.state}
>
{t(formState.message)}
</FormAlert>
<PersonFillDash className="bi fs-1 align-self-center text-danger m-3" />
<p>
{t('modals.removeAccount.message')}{' '}
<b>{t('modals.removeAccount.messageBold')}</b>
</p>
<div className="d-flex justify-content-end">
<Button
className="me-2"
disabled={isSubmitting}
onClick={handleRemoveAccount}
type="button"
variant="danger"
>
{t('modals.removeAccount.okButton')}
</Button>
<Button
onClick={handleClose}
type="button"
variant="outline-danger"
>
{t('modals.removeAccount.cancelButton')}
</Button>
</div>
</div>
</Modal.Body>
</Modal>
);
}

export default RemoveAccount;
2 changes: 2 additions & 0 deletions frontend/src/components/Modals/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { actions } from '../../slices';

import SignUpModal from './SignUp.jsx';
import SignInModal from './SignIn.jsx';
import RemoveAccount from './RemoveAccount.jsx';
import NewSnippet from './NewSnippet';
import ShareSnippet from './ShareSnippet.jsx';
import InDevelopment from './InDevelopment.jsx';
Expand All @@ -13,6 +14,7 @@ import DuplicateSnippetModal from './DuplicateSnippetModal';
const modals = {
signingUp: SignUpModal,
signingIn: SignInModal,
removeAccount: RemoveAccount,
newSnippet: NewSnippet,
sharingSnippet: ShareSnippet,
inDevelopment: InDevelopment,
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@
"message": "Эта функция пока недоступна",
"okButton": "Понятно"
},
"removeAccount": {
"message": "Вы действительно хотите",
"messageBold": "удалить учетную запись?",
"okButton": "Удалить",
"cancelButton": "Отмена"
},
"share": {
"snippetEmbedLabel": "Код сниппета для вставки",
"snippetLinkLabel": "Ссылка на сниппет"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function SettingsPage() {
const dispatch = useDispatch();

const handleRemoveAccount = () => {
dispatch(modalActions.openModal({ type: 'inDevelopment' }));
dispatch(modalActions.openModal({ type: 'removeAccount' }));
};

return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {
signOutPath: () => [apiPath, 'signout'].join('/'),

// delete user
deleteUserPath: (id) => [apiPath, 'users', `:${id}`].join('/'),
deleteUserPath: (id) => [apiPath, 'users', `${id}`].join('/'),

// get - shows all snippets
snippetsPath: () => [apiPath, 'snippets'].join('/'),
Expand Down