-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageContent.js
141 lines (128 loc) · 3.82 KB
/
ImageContent.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import _ from "lodash";
import React, { Component } from "react";
import { Field, reduxForm } from "redux-form";
import { connect } from "react-redux";
import { saveBook, deleteBook } from "../../actions";
import bookFormFields from '../../helpers/bookFormFields'
import renderDatePicker from '../../helpers/renderDatePicker'
import { confirmAlert } from 'react-confirm-alert'; // Import
import 'react-confirm-alert/src/react-confirm-alert.css' // Import css
class ImageContent extends Component {
componentWillMount () {
let {initialize, title, content, author, published, id } = this.props;
initialize({ title, content, author, published, id});
}
renderField(field) {
const { meta: { touched, error } } = field;
const className = `form-group ${touched && error ? "has-danger" : ""}`;
return (
<div className={className}>
<label className="text-primary">{field.label}</label>
<input className="form-control" type="text" {...field.input} />
<div className="error-text">
{touched ? error : ""}
</div>
</div>
);
}
onSubmit = async values => {
let {checkTitle, saveBook, id, img, modal: { close }} = this.props;
try {
let res = await checkTitle(values);
if (res.exists) {
confirmAlert({
title: 'Title already exists',
message: 'Please choose a different title.',
buttons: [
{
label: 'Ok',
}
]
})
} else {
saveBook({...values, id, img})
return close()
}
} catch(e) {
console.log(e)
}
}
onDeleteClick() {
let {deleteBook, id, modal: { close }} = this.props;
confirmAlert({
title: 'Confirm to delete',
message: 'Are you sure you want to delete this book?',
buttons: [
{
label: 'Yes',
onClick: () =>
deleteBook(id, () =>
close()
)
},
{
label: 'No'
}
]
})
}
render() {
const { handleSubmit, newBook } = this.props;
return (
<div className="container book-form" >
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<Field
label="Title For Book"
name="title"
component={this.renderField}
/>
<Field
label="Author"
name="author"
component={this.renderField}
/>
<Field
label="Summary"
name="content"
component={this.renderField}
/>
<label className="text-primary">Published</label>
<Field
label="Published"
name="published"
component={renderDatePicker}
format={(value, name) => value === '' ? null : value}
/>
<button type="submit" className="btn btn-primary mt-md-3">Save</button>
</form>
{!newBook ? <button className="btn btn-danger float-right delete-book"
onClick={this.onDeleteClick.bind(this)}
>Delete</button> : ""}
</div>
);
}
}
function validate(values) {
const errors = {};
_.each(bookFormFields, (type, field) => {
if(!values[field]){
if(field==='title'){
errors[field] = 'Please include a Title'
} else if(field==='author') {
errors[field] = 'Who wrote it?'
} else if(field==='published') {
errors[field] = 'When was it Published?'
} else {
errors[field] = 'Please include a Short Summary/Teaser'
}
}
})
return errors;
}
export default reduxForm({
enableReinitialize: true,
keepDirtyOnReinitialize: true,
validate,
form: "BooksNewForm",
fields: _.keys(bookFormFields)
})(connect(null, { saveBook,deleteBook })(ImageContent));