Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/client": "^3.6.9",
"@babel/plugin-proposal-decorators": "^7.16.7",
"@fortawesome/fontawesome-svg-core": "^6.1.1",
"@fortawesome/free-brands-svg-icons": "^6.1.2",
Expand All @@ -11,12 +12,14 @@
"@mdx-js/loader": "^1.6.22",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/user-event": "^12.1.10",
"apollo-link-error": "^1.1.13",
"axios": "^0.27.2",
"bootstrap": "^5.0.1",
"date-fns": "^2.29.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-markdown": "^2.2.1",
"eslint-plugin-react-hooks": "^4.3.0",
"graphql": "^16.6.0",
"react": "^17.0.2",
"react-bootstrap": "^1.6.1",
"react-dom": "^17.0.2",
Expand Down
85 changes: 53 additions & 32 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/* eslint-disable array-callback-return */
/* eslint-disable no-alert */
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable arrow-body-style */
/* eslint-disable max-len */
/* eslint-disable no-unused-vars */
import { React, useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { ApolloClient, InMemoryCache, ApolloProvider, HttpLink, from } from '@apollo/client';
import { onError } from '@apollo/client/link/error';
import './index.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { BrowserRouter as Router, Switch, Route, Redirect, useHistory } from 'react-router-dom';
Expand All @@ -16,40 +20,57 @@ import Edit from './components/EditJournal/Edit';
import useAxiosFetch from './hooks/useAxiosFetch';
import { DataProvider } from './context/DataContext';

const errorLink = onError(({ graphqlErrors, networkError }) => {
if (graphqlErrors) {
graphqlErrors.map(({ message, location, path }) => {
alert(`Graphql error ${message}`);
});
}
});

const link = from([errorLink, new HttpLink({ uri: 'http://localhost:4000/graphql' })]);

const client = new ApolloClient({
cache: new InMemoryCache(),
link,
});

function App() {
return (
<Layout>
<DataProvider>
<Switch>
<Route exact path='/'>
<Home />
</Route>
<Route path='/manifesto'>
<Manifesto />
</Route>
<Route path='/journal'>
<Journal />
</Route>
<Route exact path='/addjournal'>
<AddJournal />
</Route>
<Route path='/edit/:id'>
<Edit />
</Route>
<Route path='/policy/:id'>
<JournalDetails />
</Route>
<Route path='/Signup'>
<Auth />
</Route>
<Route path='/Login'>
<Login />
</Route>
<Redirect to='/' />
</Switch>
<Footer />
</DataProvider>
</Layout>
<ApolloProvider client={client}>
<Layout>
<DataProvider>
<Switch>
<Route exact path='/'>
<Home />
</Route>
<Route path='/manifesto'>
<Manifesto />
</Route>
<Route path='/journal'>
<Journal />
</Route>
<Route exact path='/addjournal'>
<AddJournal />
</Route>
<Route path='/edit/:id'>
<Edit />
</Route>
<Route path='/policy/:id'>
<JournalDetails />
</Route>
<Route path='/Signup'>
<Auth />
</Route>
<Route path='/Login'>
<Login />
</Route>
<Redirect to='/' />
</Switch>
<Footer />
</DataProvider>
</Layout>
</ApolloProvider>
);
}

Expand Down
79 changes: 56 additions & 23 deletions src/components/Authentication/Login/Login.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable no-unused-vars */
/* eslint-disable max-len */
/* eslint-disable import/extensions */
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable react/function-component-definition */
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import { Link } from 'react-router-dom';
import React, { useState } from 'react';
import { Link, useHistory } from 'react-router-dom';
import { useMutation } from '@apollo/client';
import useFormLogin from './useFormLogin';
import validateLoginInfo from './validateLoginInfo.js';
import {
Expand All @@ -20,12 +22,57 @@ import {
FormH2,
} from './styles';
import { signup } from '../../../config/content';
import LOGIN from '../../../graphql/mutation/login';
import { toErrorMap } from '../../../utils/toErrorMap';

const FormLogin = ({ submitForm }) => {
const { handleChange, values, handleSubmit, errors } = useFormLogin(
submitForm,
validateLoginInfo,
);
const FormLogin = () => {
const [login, { loading, error }] = useMutation(LOGIN);
const history = useHistory();

const [isUsernameOrEmailError, setIsUsernameOrEmailError] = useState(false);
const [isPasswordError, setIsPasswordError] = useState(false);
const [usernameOrEmailErrorMessage, setUsernameOrEmailErrorMessage] = useState('');
const [passwordErrorMessage, setPasswordErrorMessage] = useState('');

const resetErrorStateValues = () => {
setIsUsernameOrEmailError(false);
setIsPasswordError(false);
setUsernameOrEmailErrorMessage('');
setPasswordErrorMessage('');
};

const handleSubmit = async (event) => {
event.preventDefault();
resetErrorStateValues();
const data = new FormData(event.currentTarget);

const usernameOrEmail = data.get('email');
const password = data.get('password');

const response = await login({
variables: {
userInfo: { usernameOrEmail, password },
},
});

if (response.data?.login.errors) {
const errorMapped = toErrorMap(response.data.login.errors);
// console.log(toErrorMap(response.data.login.errors));
if (errorMapped.usernameOrEmail) {
setUsernameOrEmailErrorMessage(errorMapped.usernameOrEmail);
setIsUsernameOrEmailError(true);
}

if (errorMapped.password) {
setPasswordErrorMessage(errorMapped.password);
setIsPasswordError(true);
}
} else if (response.data?.login.user) {
// console.log('Login Successful');
resetErrorStateValues();
history.push('/journal');
}
};

return (
<FormContentRight>
Expand All @@ -34,25 +81,11 @@ const FormLogin = ({ submitForm }) => {
<FormH2>{signup.head2}</FormH2>
<FormInputs>
<FormLabel htmlFor='email'>{signup.labelEmail}</FormLabel>
<FormInput
id='email'
type='email'
name='email'
value={values.email}
onChange={handleChange}
/>
{errors.email && <FormInputsP>{errors.email}</FormInputsP>}
<FormInput id='email' label='Email Address' type='email' name='email' />
</FormInputs>
<FormInputs>
<FormLabel htmlFor='password'>{signup.labelPassword}</FormLabel>
<FormInput
id='password'
type='password'
name='password'
value={values.password}
onChange={handleChange}
/>
{errors.password && <FormInputsP>{errors.password}</FormInputsP>}
<FormInput id='password' label='Password' type='password' name='password' />
</FormInputs>
<ButtonContainer>
<FormInputBtn type='submit'>{signup.buttonLogin}</FormInputBtn>
Expand Down
Loading