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

[#449] added login ability from GuestMenu.jsx #467

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion frontend/src/components/Forms/SignInForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { object } from 'yup';

import { Link } from 'react-router-dom';
import { Link, useNavigate } from 'react-router-dom';
import Button from 'react-bootstrap/Button';
import Form from 'react-bootstrap/Form';

Expand All @@ -20,6 +20,7 @@ function SignInForm({ onSuccess = () => null }) {
const { t } = useTranslation();
const emailRef = useRef();
const auth = useAuth();
const navigate = useNavigate();

const initialFormState = { state: 'initial', message: '' };
const [formState, setFormState] = useState(initialFormState);
Expand All @@ -43,12 +44,23 @@ function SignInForm({ onSuccess = () => null }) {
onSubmit: async (values, actions) => {
setFormState(initialFormState);
const preparedValues = validationSchema.cast(values);
const guest = localStorage.getItem('guestUserData');
try {
actions.setSubmitting(true);
await axios.post(routes.signInPath(), {
email: preparedValues.email,
password: values.password,
});
if (guest) {
localStorage.removeItem('guestUserData');
const {
data: {
currentUser: { username },
},
} = await axios.get(routes.userProfilePath());
navigate(routes.profilePagePath(username));
actions.setSubmitting(false);
}
auth.signIn();
actions.setSubmitting(false);
onSuccess();
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/components/Navigation/GuestMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ function GuestMenu() {
const handleNewSnippet = () => {
dispatch(actions.openModal({ type: 'newSnippet' }));
};
const handleSignInButton = () => {
dispatch(actions.openModal({ type: 'signingIn' }));
};

const handleRegButton = () => {
const handleSignUpButton = () => {
dispatch(actions.openModal({ type: 'signingUp' }));
};

Expand All @@ -39,10 +42,16 @@ function GuestMenu() {
</li>
<Dropdown.Divider />
<li>
<Dropdown.Item as={Button} onClick={handleRegButton}>
<Dropdown.Item as={Button} onClick={handleSignUpButton}>
{t('signUp.registerButton')}
</Dropdown.Item>
</li>
<Dropdown.Divider />
<li>
<Dropdown.Item as={Button} onClick={handleSignInButton}>
{t('profileActions.signIn')}
</Dropdown.Item>
</li>
</Dropdown.Menu>
</Dropdown>
);
Expand Down
Loading