Skip to content

Commit aa02003

Browse files
author
Ahmad Khalid Butt
committed
implemented todolist
1 parent 4871cd0 commit aa02003

File tree

12 files changed

+167
-22
lines changed

12 files changed

+167
-22
lines changed

.DS_Store

6 KB
Binary file not shown.

client/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
REACT_APP_API_URL = "http:localhost:3000"
1+
REACT_APP_API_URL = "http://localhost:3000/api"

client/package-lock.json

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
"@types/react-dom": "^17.0.11",
1313
"@types/react-router-dom": "^5.3.3",
1414
"axios": "^0.26.0",
15+
"axios-hooks": "^3.0.1",
1516
"bootstrap": "^5.1.3",
1617
"email-validator": "^2.0.4",
1718
"formik": "^2.2.9",
1819
"react": "^17.0.2",
1920
"react-dom": "^17.0.2",
2021
"react-router-dom": "^6.2.1",
2122
"react-scripts": "5.0.0",
23+
"react-toastify": "^8.2.0",
2224
"reactstrap": "^9.0.1",
2325
"typescript": "^4.5.5",
2426
"web-vitals": "^2.1.4",

client/src/App.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import React, { Component } from 'react';
2+
import { ToastContainer } from 'react-toastify';
23
import Router from './router';
34

45
class App extends Component {
56
render() {
6-
return <Router/>
7+
return <>
8+
<Router />
9+
<ToastContainer
10+
position="top-right"
11+
autoClose={5000}
12+
hideProgressBar={false}
13+
newestOnTop={false}
14+
closeOnClick
15+
rtl={false}
16+
pauseOnFocusLoss
17+
draggable
18+
pauseOnHover
19+
/>
20+
<ToastContainer />
21+
</>
722
}
823
}
924

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { CardHeader, Col, Container, Input, ListGroup, ListGroupItem, Row } from "reactstrap"
2+
3+
interface ITodo {
4+
todo: string,
5+
status: boolean
6+
}
7+
8+
interface ITodoListProps {
9+
todos: ITodo[]
10+
}
11+
12+
const TodoList = (props: ITodoListProps) => {
13+
const todoList = props.todos.map((todo) => {
14+
return <ListGroupItem color="info">
15+
<Row>
16+
<Col>
17+
<Input type='checkbox' />
18+
</Col>
19+
<Col>
20+
<div>{todo.todo}</div>
21+
</Col>
22+
</Row>
23+
</ListGroupItem>
24+
})
25+
return <Container className="bg-light border">
26+
<CardHeader>Card title</CardHeader>
27+
<ListGroup>
28+
{todoList}
29+
</ListGroup>
30+
</Container>
31+
}
32+
33+
export default TodoList

client/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import './index.css';
44
import App from './App';
55
import reportWebVitals from './reportWebVitals';
66
import "bootstrap/dist/css/bootstrap.min.css";
7+
import 'react-toastify/dist/ReactToastify.css';
78

89
ReactDOM.render(
910
<React.StrictMode>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import useAxios from "axios-hooks";
2+
import { useEffect } from "react";
3+
import { useNavigate } from "react-router-dom";
4+
import { Spinner } from "reactstrap";
5+
import TodoList from "../../components/TodoList/todoList.component";
6+
7+
8+
const HomePage = () => {
9+
const {REACT_APP_API_URL} = process.env;
10+
const bearerToken = localStorage.getItem('bearerToken');
11+
const navigate = useNavigate();
12+
useEffect(() => {
13+
if(!bearerToken){
14+
navigate('/login');
15+
}
16+
})
17+
const [{ data: todos, loading, error }] = useAxios({url: `${REACT_APP_API_URL}/todos`, method: 'GET', headers: {
18+
Authorization: `bearer ${bearerToken}`
19+
}});
20+
if (loading) return <Spinner/>
21+
if (error) return <p>Error!</p>
22+
return <TodoList todos ={todos}/>
23+
}
24+
25+
export default HomePage;

client/src/pages/LoginPage/login.page.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { withFormik, Form, Field } from 'formik';
33
import * as Yup from 'yup';
44
import { Button, Col, Label, Row } from 'reactstrap';
55
import axios from 'axios';
6+
import { NavigateFunction, useNavigate } from 'react-router-dom';
7+
import { toast, ToastContainer } from 'react-toastify';
68

79
interface LoginProps {
810
username?: string,
@@ -16,6 +18,8 @@ interface ElementProps {
1618
password?: string
1719
}
1820

21+
let navigate: NavigateFunction;
22+
1923
const LoginPage = (props: LoginProps) => {
2024
const loginPageStyle = {
2125
margin: "32px auto 37px",
@@ -26,9 +30,10 @@ const LoginPage = (props: LoginProps) => {
2630
boxShadow: "0px 0px 10px 10px rgba(0,0,0,0.15)"
2731
};
2832
const { touched, errors } = props;
29-
console.log(process.env);
33+
navigate = useNavigate();
3034
return (
3135
<React.Fragment>
36+
<ToastContainer/>
3237
<div className="container">
3338
<div className="login-wrapper" style={loginPageStyle}>
3439
<h2>Login Page</h2>
@@ -76,8 +81,14 @@ const LoginFormik = withFormik({
7681
}),
7782
handleSubmit: (values) => {
7883
const {REACT_APP_API_URL} = process.env;
79-
axios.post(`http://localhost:3000/api/login`,values)
80-
.then(data => console.log)
84+
axios.post(`${REACT_APP_API_URL}/user/login`,values)
85+
.then(res => {
86+
if(res?.data?.token){
87+
localStorage.setItem('bearerToken', res.data.token);
88+
toast("Logged in successfully");
89+
navigate('/');
90+
}
91+
});
8192
}
8293
})(LoginPage);
8394

client/src/pages/home.page.tsx

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)