diff --git a/EGUI/lab3/data/db.json b/EGUI/lab3/data/db.json index 0d474f5c..45671f47 100644 --- a/EGUI/lab3/data/db.json +++ b/EGUI/lab3/data/db.json @@ -1,170 +1,207 @@ { "posts": [ { - "title": "JSON SERVER", - "author": "foo", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 1 }, { - "title": "REACT", - "author": "facebook", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 2 }, { - "title": "BOOTSTRAP", - "author": "css master", + "mail": "test@test", + "login": "test@test", + "blogTitle": "test@test", + "password": "test@test", "id": 3 }, { - "title": "MEDIUM", - "author": "medium", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 4 }, { - "title": "test", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 5 }, { - "title": "test", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 6 }, { - "title": "test", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 7 }, { - "title": "test", + "mail": "test@test", + "login": "", + "blogTitle": "", + "password": "", "id": 8 }, { - "title": "test", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 9 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 10 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 11 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 12 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 13 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 14 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 15 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 16 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 17 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 18 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 19 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 20 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 21 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 22 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 23 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 24 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 25 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 26 }, { - "title": "", - "author": "", + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 27 }, { + "mail": "", + "login": "", + "blogTitle": "", + "password": "", "id": 28 }, { + "mail": "", + "login": "test@test", + "blogTitle": "", + "password": "", "id": 29 - }, - { - "id": 30 - }, - { - "id": 31 - }, - { - "id": 32 - }, - { - "id": 33 - }, - { - "id": 34 - }, - { - "title": "test", - "author": "test", - "id": 35 - }, - { - "title": "test", - "author": "test", - "id": 36 - }, - { - "title": "test@test", - "author": "test", - "id": 37 } ] } \ No newline at end of file diff --git a/EGUI/lab3/src/routes/register.jsx b/EGUI/lab3/src/routes/register.jsx index 51c6c74c..e8b019c4 100644 --- a/EGUI/lab3/src/routes/register.jsx +++ b/EGUI/lab3/src/routes/register.jsx @@ -7,73 +7,115 @@ import React from 'react'; export default class Register extends React.Component { - constructor(props) { - super(props); - this.state = { - inputValue: 'test' - }; - } + constructor(props) { + super(props); + this.state = { + mail: "", + login: "", + blogTitle: "", + password: "", + }; + } - render() { - return ( -
-
- { this.state.inputValue } -
-
- - Email address - this.updateInputValue(evt)} - /> - - - - Login - - - - - Blog Title - - - - - Password - - - -
+ render() { + return ( +
+
+ { this.state.mail }
- ); - } +
+ + Email address + this.updateMail(evt)} + /> + + + + Login + this.updateLogin(evt)} + /> + + + + Blog Title + this.updateBlogTitle(evt)} + /> + + + + Password + this.updatePassword(evt)} + /> + + +
+
+ ); + } - updateInputValue(evt) { - const val = evt.target.value; - // ... - this.setState({ - inputValue: val - }); - } + updateMail(evt) { + const val = evt.target.value; + // ... + this.setState({ + mail: val + }); + } - registerUser() { - alert(this.state.inputValue); - - console.log("anything"); - // console.log(this.state.inputValue); - const test = { - title: this.state.inputValue, - author: "test" - }; - fetch("http://localhost:8000/posts", { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(test) - }).then(data => console.log(data)); - }; + updateLogin(evt) { + const val = evt.target.value; + // ... + this.setState({ + login: val + }); + } + + updateBlogTitle(evt) { + const val = evt.target.value; + // ... + this.setState({ + blogTitle: val + }); + } + + updatePassword(evt) { + const val = evt.target.value; + // ... + this.setState({ + password: val + }); + } + + async registerUser() { + const fetchAddress = "http://localhost:8000/posts/?login=" + this.state.login; + // console.log(this.state.inputValue); + const response = await fetch(fetchAddress, + { + method: "GET", + }); + const allData = await response.json(); + console.log(allData.length); + if(allData.length !== 0) + { + alert("This login: \"" + this.state.login + "\" is already taken!"); + return; + } + await fetch("http://localhost:8000/posts", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(this.state) + }).then(data => console.log(data)); + + }; } \ No newline at end of file