Skip to content
Open
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
2,486 changes: 2,486 additions & 0 deletions homework_3/blog/README.md

Large diffs are not rendered by default.

11,253 changes: 11,253 additions & 0 deletions homework_3/blog/package-lock.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions homework_3/blog/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "blog",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.1.3",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-scripts": "1.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Binary file added homework_3/blog/public/favicon.ico
Binary file not shown.
40 changes: 40 additions & 0 deletions homework_3/blog/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions homework_3/blog/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
29 changes: 29 additions & 0 deletions homework_3/blog/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.App {
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}

.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}

.App-title {
font-size: 1.5em;
}

.App-intro {
font-size: large;
}

@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}

246 changes: 246 additions & 0 deletions homework_3/blog/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import React, {Component} from 'react';

import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';

import {SignIn} from './components/sign-in-page';
import {SignUp} from './components/sign-up-page';
import {Users} from './components/users-page';

class App extends Component {

constructor() {
super();

this.state = {
activePage: 'usersPage',
name: null,
password: null,
confirmPassword: null,
email: null,
errors: {},
users: {},
ifLogIn: false
}


}

handleInputChange = (event) => {

const target = event.target;
const value = target.value;
const name = target.name;

this.setState({
[name]: value
});
}

changePageToSignIn = () => {
this.setState({
activePage: 'signInPage'
});
}
changePageToSignUp = () => {
this.setState({
activePage: 'signUpPage'
});
}

changePageToUsers = () => {
this.setState({
activePage: 'usersPage'
});
}

signOut = () => {
this.setState({
name: null,
password: null,
confirmPassword: null,
email: null,
errors: {},
ifLogIn: false
});
}

validate = (key) => {
let errors = {};

if (!this.state.name)
errors.name = "Name field is empty";
else if (/[^a-zA-Z0-9_-]/.test(this.state.name))
errors.name = "You can use only a-z, A-Z, 0-9, - и _.";


if (!this.state.password)
errors.password = "Password field is empty";
else if (this.state.password.length < 4)
errors.password = "Password should be more than 4 simbols";

if (key === 'signup') {
if (!this.state.confirmPassword)
errors.confirmPassword = "confirmPassword field is empty";
else if (this.state.confirmPassword !== this.state.password)
errors.confirmPassword = "confirmPassword isn't the same as password";

if (!this.state.email)
errors.email = "Email field is empty";
else if (!((this.state.email.indexOf(".") > 0) && (this.state.email.indexOf("@") > 0)) || /[^a-zA-Z0-9.@_-]/.test(this.state.email))
errors.email = "Email is incorrect";
}

return errors;

}

sendRequest = (arg) => {

let errors = this.validate(arg);
let params;
if (Object.keys(errors).length) {
this.setState({
errors: errors
});
return;
} else {
this.setState({
errors: ''
});
if (arg === 'signup') {
params = {
email: this.state.email,
user: this.state.name,
password: this.state.password
}
} else {
params = {
user: this.state.name,
password: this.state.password
}
}
}

fetch('https://flatearth-api.herokuapp.com/api/v1/auth/' + arg, {
method: 'POST',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify(
params
)
}).then(res => res.json())
.then(res => {
console.log(res)
if(res.status === 'error'){
this.setState({
errors: {
requestErrors: res.message
}
});
}else if (arg === 'signup'){
this.setState({
activePage: 'signInPage',
errors: {}
});
}else {
this.setState({
token: res.message.token,
errors: {},
ifLogIn: true
});
this.sendSecretRequest();
}
});

}


sendSecretRequest = () => {

fetch('https://flatearth-api.herokuapp.com/api/v1/auth/secret', {
method: 'GET',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.state.token
}
}).then(res => res.json())
.then(res => {
console.log(res);
if(res.status === 'error'){
this.setState({
errors: {
requestErrors: res.message
}
});
}else{
this.setState({
activePage: 'usersPage',
errors: {},
users: res.message
});
}
});

}

render() {

const {
activePage,
} = this.state;

let buttons = this.state.ifLogIn ? <div className="input-group mb-3"><button type="btn" className="btn btn-primary" onClick={this.signOut}>Sign Out</button></div> : <div className="input-group mb-3">
<button type="btn" className="btn btn-primary" onClick={this.changePageToSignIn}>Sign In</button>
<button type="btn" className="btn btn-primary" onClick={this.changePageToSignUp}>Sign Up</button>
</div>

let nameInHeader = this.state.ifLogIn ? <span>{this.state.name}</span> : ''

if (activePage === 'usersPage') {
return (
<div>
<div>
{nameInHeader}
{buttons}
</div>

<Users users={this.state.users}/>
</div>
)
}

if (activePage === 'signInPage') {
return (
<div>
{buttons}
<SignIn
handleInputChange={this.handleInputChange}
sendRequest={this.sendRequest}
errors={this.state.errors}
/>
</div>
)
}

if (activePage === 'signUpPage') {
return (
<div>
{buttons}
<SignUp
handleInputChange={this.handleInputChange}
sendRequest={this.sendRequest}
errors={this.state.errors}
/>
</div>
)
}


}
}

export default App;
9 changes: 9 additions & 0 deletions homework_3/blog/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});
33 changes: 33 additions & 0 deletions homework_3/blog/src/components/sign-in-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, {Component} from 'react';


export class SignIn extends Component {
render() {
const style = {
color: 'red'
}
return (
<div className="container">
<form>

<div className="form-group">
<input type="text" name="name" className="form-control" placeholder="enter user name"
onChange={this.props.handleInputChange}/>
<span style={style}>{this.props.errors.name}</span>
</div>
<div className="form-group">
<input type="password" name="password" className="form-control" placeholder="enter password"
onChange={this.props.handleInputChange}/>
<span style={style}>{this.props.errors.password}</span>
</div>
<div className="form-group">
<button type="button" onClick={() => this.props.sendRequest('login')}
className="btn btn-primary">Log In
</button>
</div>
</form>
</div>
);
}
}

Loading