forked from den1s0v/dvcs-lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ibrahim.js
97 lines (75 loc) · 2.68 KB
/
Ibrahim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React, { Component } from "react";
import Container from 'react-bootstrap/lib/Container'
import Col from 'react-bootstrap/lib/Col'
import Row from 'react-bootstrap/lib/Row'
import Alert from 'react-bootstrap/lib/Alert'
import Button from 'react-bootstrap/lib/Button'
import ButtonToolbar from 'react-bootstrap/lib/ButtonToolbar';
import Form from 'react-bootstrap/lib/Form'
import { Redirect } from "react-router-dom"
export class Sample extends Component {
constructor(props) {
super(props);
this.state = {
show: false,
canSolve: true
};
this.checkAnswer = this.checkAnswer.bind(this);
}
checkAnswer(event) {
//console.log(event.target.parentNode.previousSibling.lastChild.value);
const value = event.target.parentNode.previousSibling.lastChild.value;
if ( !this.state.canSolve ) {
alert("You already solve this task");
}
else if ( value === this.props.sample.answer ) {
//сохранить в у usera в список решенных
fetch('/api/samples/addSolvedSample', {
method: 'POST',
body: JSON.stringify({ sampleId: this.props.sample._id}),
headers: new Headers({"Content-Type": "application/json", "authorization":localStorage.userToken})
});
fetch('/api/samples/addSolvedUser', {
method: 'POST',
body: JSON.stringify({ sampleId: this.props.sample._id}),
headers: new Headers({"Content-Type": "application/json", "authorization":localStorage.userToken})
});
this.setState( { canSolve:false } );
} else {
alert("It's wrong answer");
}
}
render () {
const handleHide = () => this.setState({ show: false });
return (
<div>
<Alert show={this.state.show} variant="dark">
<Alert.Heading>{this.props.sample.title}</Alert.Heading>
<Form>
<Form.Label>Right lines:</Form.Label>
<Form.Group>
<Form.Label>
{this.props.sample.greenLines[0]}<br />
{this.props.sample.greenLines[1]}
</Form.Label>
</Form.Group>
<Form.Label>Wrong lines:</Form.Label>
<Form.Group>
<Form.Label>
{this.props.sample.redLines[0]}<br />
{this.props.sample.redLines[1]}
</Form.Label>
</Form.Group>
<Form.Group>
</Form.Group>
<ButtonToolbar>
{ ( this.props.canSolve && this.state.canSolve ) && <Button onClick={ (event) => this.checkAnswer(event) } variant="info">Check answer</Button>}
<Button onClick={handleHide} variant="light">Close</Button>
</ButtonToolbar>
</Form>
</Alert>
{!this.state.show && <Button onClick={handleShow}>{this.props.sample.title}</Button>}
</div>
);
}
}