Skip to content

Commit cd32c5d

Browse files
committed
add use context
1 parent ef0cf6d commit cd32c5d

File tree

9 files changed

+126
-41
lines changed

9 files changed

+126
-41
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
/.pnp
66
.pnp.js
77

8+
.idea/
9+
.idea/*
10+
811
# testing
912
/coverage
1013

.idea/inspectionProfiles/Project_Default.xml

+1-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@testing-library/user-event": "^7.2.1",
99
"axios": "^0.19.2",
1010
"bootstrap": "^4.4.1",
11+
"dotenv": "^8.2.0",
1112
"eslint": "^6.6.0",
1213
"formik": "^2.1.4",
1314
"google-map-react": "^1.1.7",
@@ -60,5 +61,6 @@
6061
"eslint-plugin-react": "^7.18.3",
6162
"eslint-plugin-react-hooks": "^1.7.0",
6263
"prettier": "^1.19.1"
63-
}
64+
},
65+
"proxy": "http://127.0.0.1:5000"
6466
}

src/components/NavBar/NavBar.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const Wrap = styled.div`
1717
position: fixed;
1818
top: 0;
1919
left: 0;
20+
z-index: 9999;
2021
`;
2122

2223
const NavBar = () => {
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
const UserContext = React.createContext({
4+
userContext: {
5+
username: '',
6+
id: '',
7+
role: '',
8+
},
9+
});
10+
11+
export default UserContext;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import { ThemeC ontext } from './theme-context';
3+
4+
export default class ThemeProvider extends React.Component {
5+
constructor() {
6+
super();
7+
this.state = {
8+
setColor: this.setColor.bind(this),
9+
color: 'yellow',
10+
};
11+
}
12+
setColor(color) {
13+
this.setState({ color });
14+
}
15+
render() {
16+
return (
17+
<ThemeContext.Provider
18+
value={{
19+
themeContext: {
20+
...this.state,
21+
},
22+
}}
23+
>
24+
{this.props.children}
25+
</ThemeContext.Provider>
26+
);
27+
}
28+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import UserContext from './UserContext';
3+
4+
export function withTheme(Component) {
5+
return function UserComponent(props) {
6+
return (
7+
<UserContext.Consumer>
8+
{contexts => <Component {...props} {...contexts} />}
9+
</UserContext.Consumer>
10+
);
11+
};
12+
}

src/templates/GlobalTemplate.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import GlobalStyle from 'theme/GlobalStyle';
44
import theme from 'theme/theme';
55
import { ThemeProvider } from 'styled-components/macro';
6-
// import Preloader from '../components/Preloader/Preloader';
76

8-
const GlobalTemplate = ({ children }) => (
9-
<>
10-
<ThemeProvider theme={theme}>
11-
<GlobalStyle />
12-
{/*<Preloader/>*/}
13-
{children}
14-
</ThemeProvider>
15-
</>
16-
);
7+
class GlobalTemplate extends Component {
8+
state = {
9+
user: {
10+
userName: '',
11+
id: '',
12+
role: '',
13+
},
14+
};
15+
16+
render() {
17+
const { children } = this.props;
18+
return (
19+
<ThemeProvider theme={theme}>
20+
<GlobalStyle />
21+
{children}
22+
</ThemeProvider>
23+
);
24+
}
25+
}
1726

1827
GlobalTemplate.propTypes = {
1928
children: PropTypes.element.isRequired,

src/views/LoginView.js

+47-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from 'react';
22
import styled from 'styled-components/macro';
3-
import { Col, Row, Button, Form, FormGroup, Label, Input, Container } from 'reactstrap';
3+
import { Col, Row, Button, FormGroup, Label, Container } from 'reactstrap';
4+
import { Formik, Form as FormikForm, Field } from 'formik';
5+
6+
import axios from 'axios';
7+
import context from 'react-router/modules/RouterContext';
48

59
const LoginWrap = styled.div`
610
width: 100%;
@@ -22,25 +26,49 @@ const LoginView = () => (
2226
<p className="lead d-block mb-0">Jakub Dolata</p>
2327
<p className="lead d-block">Filip Baumgart</p>
2428
</TitleContainer>
25-
<Row class="mt-5">
29+
<Row className="mt-5">
2630
<Col md={12}>
27-
<Form>
28-
<Row form>
29-
<Col md={12}>
30-
<FormGroup>
31-
<Label for="login">Login</Label>
32-
<Input type="text" name="login" id="login" />
33-
</FormGroup>
34-
</Col>
35-
<Col md={12}>
36-
<FormGroup>
37-
<Label for="password">Hasło</Label>
38-
<Input type="password" name="password" id="password" />
39-
</FormGroup>
40-
</Col>
41-
</Row>
42-
<Button>Zaloguj się</Button>
43-
</Form>
31+
<Formik
32+
initialValues={{ username: '', password: '' }}
33+
onSubmit={(values, { setSubmitting }) => {
34+
// const api = process.env.API_URL;
35+
const { username, password } = values;
36+
axios
37+
.post('http://127.0.0.1:5000/login', {
38+
username: username,
39+
password: password,
40+
})
41+
.then(function(response) {
42+
console.log(response);
43+
})
44+
.catch(function(error) {
45+
console.log(error);
46+
});
47+
setSubmitting(false);
48+
}}
49+
>
50+
{({ isSubmitting }) => (
51+
<FormikForm>
52+
<Row form>
53+
<Col md={12}>
54+
<FormGroup>
55+
<Label for="username">Login</Label>
56+
<Field type="text" className="form-control" name="username" required />
57+
</FormGroup>
58+
</Col>
59+
<Col md={12}>
60+
<FormGroup>
61+
<Label for="password">Hasło</Label>
62+
<Field type="password" className="form-control" name="password" required />
63+
</FormGroup>
64+
</Col>
65+
</Row>
66+
<Button type="submit" color="info" disabled={isSubmitting}>
67+
Zaloguj
68+
</Button>
69+
</FormikForm>
70+
)}
71+
</Formik>
4472
</Col>
4573
</Row>
4674
</LoginWrap>

0 commit comments

Comments
 (0)