This repository has been archived by the owner on Aug 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddBook.js
233 lines (194 loc) · 8.12 KB
/
AddBook.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _reactRedux = require('react-redux');
var _reactRouter = require('react-router');
var _react3 = require('muicss/react');
var _APIS = require('constants/APIS');
var _actions = require('actions');
var _utils = require('utils');
var _Branding = require('components/Branding');
var _Branding2 = _interopRequireDefault(_Branding);
var _Colophon = require('components/Colophon');
var _Colophon2 = _interopRequireDefault(_Colophon);
var _Notification = require('components/Notification');
var _Notification2 = _interopRequireDefault(_Notification);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var AddBook = function (_Component) {
_inherits(AddBook, _Component);
function AddBook(props) {
_classCallCheck(this, AddBook);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(AddBook).call(this, props));
_this.defaultState = {
searchQuery: "",
currentBook: -1,
previewIndex: -1,
conformed: false
};
_this.state = _this.defaultState;
return _this;
}
_createClass(AddBook, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.props.fetchUserAuthInfo();
}
}, {
key: 'handleAddBook',
value: function handleAddBook(event) {
var _this2 = this;
event.preventDefault();
var currentBook = this.state.currentBook;
var dataToPost = {};
var bookContent = _reactDom2.default.findDOMNode(this.refs.bookContent).childNodes[0].value;
var isValid = false;
while (true) {
if (currentBook !== -1) {
dataToPost.bookInfo = this.props.book.searchResults.books[currentBook];
} else {
this.props.handleNotification('未选择书籍!');
break;
}
if (bookContent) {
dataToPost.bookContent = bookContent;
} else {
this.props.handleNotification('请输入书籍内容!');
break;
}
isValid = true;
break;
}
dataToPost.bookInfo = JSON.stringify(dataToPost.bookInfo);
if (isValid) {
(0, _utils.callApi)(_APIS.API_ROOT + 'books', 'post', dataToPost).then(function (res) {
_this2.props.handleNotification('添加成功');
}).catch(function (err) {
console.error(err);
_this2.props.handleNotification(err.message);
});
}
}
}, {
key: 'search',
value: function search(event) {
this.setState({ searchQuery: event.target.value });
this.props.fetchDoubanBookSearchResults('search?count=5&q=' + event.target.value);
}
}, {
key: 'conformResult',
value: function conformResult(index) {
this.setState({
currentBook: index,
conformed: true
});
}
}, {
key: 'showBookCover',
value: function showBookCover(index) {
this.setState({
previewIndex: index
});
}
}, {
key: 'removeResult',
value: function removeResult() {
this.setState(this.defaultState);
this.props.clearBookSearch();
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var book = null;
if (this.state.currentBook !== -1) {
book = this.props.book.searchResults.books[this.state.currentBook];
}
return _react2.default.createElement(
'div',
{ className: 'page-add-book' },
_react2.default.createElement(
_react3.Form,
{ className: 'content-container', method: 'post' },
_react2.default.createElement(_Notification2.default, { notification: this.props.notification }),
_react2.default.createElement(
'h1',
{ className: 'page-title' },
'添加书籍'
),
!this.state.conformed ? _react2.default.createElement(_react3.Input, { onChange: this.search.bind(this), value: this.state.searchQuery, hint: '输入书名或其他书籍相关信息' }) : null,
!this.state.conformed && this.props.book.searchResults ? _react2.default.createElement(
'div',
{ className: 'drop-down' },
_react2.default.createElement(
'ul',
null,
this.props.book.searchResults.books.map(function (item, index) {
return _react2.default.createElement(
'li',
{ onMouseOver: _this3.showBookCover.bind(_this3, index), onClick: _this3.conformResult.bind(_this3, index), key: index },
item.title,
' (',
item.author,
')',
_this3.state.previewIndex === index ? _react2.default.createElement(
'div',
null,
_react2.default.createElement('img', { src: item.image })
) : null
);
})
)
) : null,
book ? _react2.default.createElement(
'div',
{ className: 'book' },
_react2.default.createElement(
'div',
null,
_react2.default.createElement('img', { src: book.image })
),
_react2.default.createElement(
'h2',
{ className: 'book-name' },
book.title
),
_react2.default.createElement(
'div',
{ className: 'book-author' },
'作者:',
book.author[0]
),
_react2.default.createElement(
_react3.Button,
{ onClick: this.removeResult.bind(this), className: 'mui--z1' },
'重新选择'
)
) : null,
_react2.default.createElement(_react3.Textarea, { hint: '粘贴书籍的全部文本内容', style: { height: 200 }, name: 'book_content', ref: 'bookContent' }),
_react2.default.createElement(
_react3.Button,
{ onClick: this.handleAddBook.bind(this), variant: 'raised' },
'确认添加'
)
)
);
}
}]);
return AddBook;
}(_react.Component);
exports.default = (0, _reactRedux.connect)(function (state) {
return {
user: state.user,
book: state.book,
notification: state.notification
};
}, { fetchUserAuthInfo: _actions.fetchUserAuthInfo, fetchDoubanBookSearchResults: _actions.fetchDoubanBookSearchResults, clearBookSearch: _actions.clearBookSearch, handleNotification: _actions.handleNotification })(AddBook);