-
Notifications
You must be signed in to change notification settings - Fork 0
/
createpost.js
110 lines (100 loc) · 4.56 KB
/
createpost.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
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { Component } from "react"
// import Paragraph from "./paragraph"
class CreatePost extends Component {
state={
main: {
title: '',
slug: '',
fimage: '',
introduction: '',
visible: true
},
counter: 0,
content: []
}
handleParagraphChange = (event) => {
event.preventDefault();
const content = [...this.state.content];
const target = event.target;
const value = target.value;
const id = target.id;
content[id-1].text=value;
this.setState({
content: [...content]
});
}
handleInputChangeMain = (event) => {
const main = {...this.state.main}
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
main: {...main, [name]: value}
});
}
// τα textarea να μπαινουν αυτόματα βαση του state.content --> OK
// τα input βαση του main
addParagraph = event => {
event.preventDefault();
const counter = this.state.counter + 1;
const content = this.state.content;
const paragraph = {ukey: counter, type:'paragraph', text:'', visible: true}
this.setState({
counter: counter,
content: [...content, paragraph]
});
}
addImage = event => {
event.preventDefault();
const counter = this.state.counter + 1;
const content = this.state.content;
const image = {ukey: counter, type:'image', text:'', visible: true}
this.setState({
counter: counter,
content: [...content, image]
});
}
createPost = e => {
e.preventDefault();
const main = {...this.state.main}
const post = {...main, ...this.state.content};
this.props.addPost(post);
e.currentTarget.reset();
this.setState({main: {title: '',slug: '',fimage: '', introduction: '', visible: true}, counter:0, content: []});
}
render() {
return(
<section className="createpost">
<h2>create a new post</h2>
<form onSubmit={this.createPost}>
<h4>this is form to add new posts</h4>
{
// πρώτα τα input του main
}
<label>Insert the title of the post:<input value={this.state.main.title} onChange={this.handleInputChangeMain} type="text" placeholder="title" name="title" />
</label><br/>
<label>Specify a slug for the url links:<input value={this.state.main.slug} onChange={this.handleInputChangeMain} type="text" placeholder="slug" name="slug" />
</label><br/>
<label>Specify an image as featured image:<input value={this.state.main.fimage} onChange={this.handleInputChangeMain} type="text" placeholder="featured image" name="fimage" />
</label><br/>
<label><textarea cols='50' rows='6' value={this.state.main.introduction} onChange={this.handleInputChangeMain} type="text" placeholder="introduction" name="introduction" />
</label><br/>
{
// και μετά παράγραφοι ή εικόνες
this.state.content
.filter(x => x.type==='paragraph' || x.type==='image')
.sort((x,y) => x.ukey - y.ukey)
.map(x => (x.type === 'image')?
<input type='text' id={x.ukey} key={x.ukey} value={this.state.content[x.ukey-1].text} onChange={this.handleParagraphChange} name={`image-${x.ukey}`} placeholder={x.ukey} />
:<textarea type='text' id={x.ukey} key={x.ukey} value={this.state.content[x.ukey-1].text} onChange={this.handleParagraphChange} name={`paragraph-${x.ukey}`} placeholder={x.ukey} cols='60' rows='5' />
)
}
<button onClick= {this.addParagraph} >Add paragraph</button>
<button onClick= {this.addImage} >Add image</button>
<br/><button disabled={!this.state.main.slug || !this.state.main.title} type="submit" value="Submit">Submit</button>
</form>
</section>
)
}
}
export default CreatePost