Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.78 KB | None | 0 0
  1. // @route Put api/words/:id
  2. // @desc Update a word by id
  3. // @access Private
  4. router.put(
  5. '/:id',
  6. passport.authenticate('jwt', { session: false }),
  7. (req, res) => {
  8. const { errors, isValid } = validateWordInput(req.body);
  9.  
  10. // Check validation
  11. if (!isValid) {
  12. // Return any errors
  13. return res.status(400).json(errors);
  14. }
  15.  
  16. Profile.findOne({ user: req.user.id }).then(profile => {
  17. Word.findById(req.params.id).then(word => {
  18. // Check for word owner
  19. if (word.user.toString() !== req.user.id) {
  20. return res.status(401).json({ notauthorized: 'User not authorized' });
  21. }
  22.  
  23. const wordID = req.params.id;
  24. const wordInput = req.body;
  25.  
  26. // Update
  27. Word.findByIdAndUpdate(
  28. { _id: wordID },
  29. { $set: wordInput },
  30. { returnOriginal: false },
  31. (err, word) => {
  32. if (err) {
  33. console.log(err);
  34. }
  35. }
  36. )
  37. .then(word => {
  38. res.json(word);
  39. })
  40. .catch(err =>
  41. res
  42. .status(404)
  43. .json({ wordcannotbeupdated: 'word cannot be updated' })
  44. );
  45. });
  46. });
  47. }
  48. );
  49.  
  50. export const updateWord = (id, updatedWord, history) => dispatch => {
  51. axios
  52. .put(`/api/words/${id}`, updatedWord)
  53. .then(res => {
  54. dispatch({
  55. type: UPDATE_WORD,
  56. payload: res.data
  57. });
  58. history.push('/my-words');
  59. })
  60. .catch(err =>
  61. dispatch({
  62. type: UPDATE_WORD,
  63. payload: ''
  64. })
  65. );
  66. };
  67.  
  68. import {
  69. GET_WORDS_BY_USER,
  70. ADD_WORD,
  71. UPDATE_WORD,
  72. GET_WORD_BY_ID,
  73. DELETE_WORD,
  74. SEARCH_WORD
  75. } from '../actions/types';
  76.  
  77. const initialState = {
  78. words: [],
  79. word: {},
  80. loading: false
  81. };
  82.  
  83. export default function(state = initialState, action) {
  84. switch (action.type) {
  85. case ADD_WORD:
  86. return {
  87. ...state,
  88. words: [action.payload, ...state.words]
  89. };
  90. case GET_WORDS_BY_USER:
  91. return {
  92. ...state,
  93. words: action.payload
  94. };
  95. case GET_WORD_BY_ID:
  96. return {
  97. ...state,
  98. word: action.payload
  99. };
  100. case UPDATE_WORD:
  101. return {
  102. ...state,
  103. words: action.payload
  104. };
  105. case SEARCH_WORD:
  106. return {
  107. ...state,
  108. words: action.payload
  109. };
  110. case DELETE_WORD:
  111. return {
  112. ...state,
  113. words: state.words.filter(word => word._id !== action.payload)
  114. };
  115. default:
  116. return state;
  117. }
  118. }
  119.  
  120. import React, { Component } from 'react';
  121. import { Link, withRouter } from 'react-router-dom';
  122. import TextFieldGroup from '../common/TextFieldGroup';
  123. import SelectListGroup from '../common/SelectListGroup';
  124. import { connect } from 'react-redux';
  125. import PropTypes from 'prop-types';
  126. import { getWordByID, updateWord } from '../../actions/wordActions';
  127. import isEmpty from '../../validation/is-empty';
  128. import {
  129. lexisOptions,
  130. grammarOptions,
  131. partOfSpeechOptions,
  132. styleOptions,
  133. originOptions,
  134. sphereOptions
  135. } from './options';
  136.  
  137. class EditWord extends Component {
  138. constructor(props) {
  139. super(props);
  140. this.state = {
  141. ugrWordCyr: '',
  142. rusTranslation: '',
  143. example: '',
  144. exampleTranslation: '',
  145. origin: '',
  146. sphere: '',
  147. see: '',
  148. lexis: '',
  149. grammar: '',
  150. partOfSpeech: '',
  151. style: '',
  152. errors: {}
  153. };
  154. this.onChange = this.onChange.bind(this);
  155. this.onSubmit = this.onSubmit.bind(this);
  156. this.onCheck = this.onCheck.bind(this);
  157. }
  158.  
  159. componentWillReceiveProps(nextProps) {
  160. if (nextProps.errors) {
  161. this.setState({ errors: nextProps.errors });
  162. }
  163.  
  164. if (nextProps.words.word[0]) {
  165. nextProps.words.word.map(word => {
  166. word.ugrWordCyr = !isEmpty(word.ugrWordCyr) ? word.ugrWordCyr : '';
  167. word.rusTranslation = !isEmpty(word.rusTranslation)
  168. ? word.rusTranslation
  169. : '';
  170. word.rusTranslation = !isEmpty(word.rusTranslation)
  171. ? word.rusTranslation
  172. : '';
  173. word.example = !isEmpty(word.example) ? word.example : '';
  174. word.exampleTranslation = !isEmpty(word.exampleTranslation)
  175. ? word.exampleTranslation
  176. : '';
  177. word.origin = !isEmpty(word.origin) ? word.origin : '';
  178. word.sphere = !isEmpty(word.sphere) ? word.sphere : '';
  179. word.see = !isEmpty(word.see) ? word.see : {};
  180. word.lexis = !isEmpty(word.lexis) ? word.lexis : {};
  181. word.grammar = !isEmpty(word.grammar) ? word.grammar : {};
  182. word.see = !isEmpty(word.see) ? word.see : {};
  183. word.style = !isEmpty(word.style) ? word.style : {};
  184.  
  185. this.setState({
  186. ugrWordCyr: word.ugrWordCyr,
  187. rusTranslation: word.rusTranslation,
  188. example: word.example,
  189. exampleTranslation: word.exampleTranslation,
  190. origin: word.origin,
  191. sphere: word.sphere,
  192. style: word.style,
  193. lexis: word.lexis,
  194. grammar: word.grammar,
  195. see: word.see,
  196. partOfSpeech: word.partOfSpeech
  197. });
  198. });
  199. }
  200. }
  201.  
  202. componentDidMount() {
  203. this.props.getWordByID(this.props.match.params.id);
  204. }
  205.  
  206. onSubmit(e) {
  207. e.preventDefault();
  208. const wordData = {
  209. ugrWordCyr: this.state.ugrWordCyr,
  210. rusTranslation: this.state.rusTranslation,
  211. example: this.state.example,
  212. exampleTranslation: this.state.exampleTranslation,
  213. origin: this.state.origin,
  214. sphere: this.state.sphere,
  215. see: this.state.see,
  216. lexis: this.state.lexis,
  217. grammar: this.state.grammar,
  218. partOfSpeech: this.state.partOfSpeech,
  219. style: this.state.style
  220. };
  221. let id = this.props.match.params.id;
  222. // Change a function to update
  223. this.props.updateWord(id, wordData, this.props.history);
  224. }
  225.  
  226. onChange(e) {
  227. this.setState({ [e.target.name]: e.target.value });
  228. }
  229.  
  230. onCheck(e) {
  231. this.setState({
  232. current: !this.state.current
  233. });
  234. }
  235. render() {
  236. const { errors } = this.state;
  237.  
  238. return (
  239. <div className="add-word">
  240. <div className="container">
  241. <div className="row">
  242. <div className="col-md-8 m-auto">
  243. <Link to="/my-words" className="btn btn-light">
  244. Go Back
  245. </Link>
  246. <h1 className="display-4 text-center">Edit Word</h1>
  247. <form onSubmit={this.onSubmit}>
  248. <TextFieldGroup
  249. placeholder="Бала"
  250. info="Введите слово на уйгурском"
  251. name="ugrWordCyr"
  252. value={this.state.ugrWordCyr}
  253. onChange={this.onChange}
  254. error={errors.ugrWordCyr}
  255. />
  256. <TextFieldGroup
  257. placeholder="Ребенок"
  258. info="Введите слово на русском"
  259. name="rusTranslation"
  260. value={this.state.rusTranslation}
  261. onChange={this.onChange}
  262. error={errors.rusTranslation}
  263. />
  264. <div className="form-check mb-form">
  265. <input
  266. type="checkbox"
  267. className="form-check-input"
  268. name="see"
  269. value={this.state.see}
  270. onChange={this.onCheck}
  271. id="see"
  272. />
  273. <label htmlFor="see">Смотри</label>
  274. </div>
  275. <TextFieldGroup
  276. placeholder=""
  277. info="Введите пример предложения на уйгурском"
  278. name="example"
  279. value={this.state.example}
  280. onChange={this.onChange}
  281. error={errors.example}
  282. />
  283. <TextFieldGroup
  284. placeholder=""
  285. info="Введите перевод примерного предложения на русском"
  286. name="exampleTranslation"
  287. value={this.state.exampleTranslation}
  288. onChange={this.onChange}
  289. error={errors.exampleTranslation}
  290. />
  291. <h6>Происхождение слова</h6>
  292. <SelectListGroup
  293. placeholder="Арабское"
  294. name="origin"
  295. value={this.state.origin}
  296. onChange={this.onChange}
  297. error={errors.origin}
  298. options={originOptions}
  299. />
  300. <h6>Сфера употребления слова</h6>
  301. <SelectListGroup
  302. placeholder="Физика"
  303. name="sphere"
  304. value={this.state.sphere}
  305. onChange={this.onChange}
  306. error={errors.sphere}
  307. options={sphereOptions}
  308. />
  309. <h6>Лексика слова</h6>
  310. <SelectListGroup
  311. placeholder="лексика"
  312. name="lexis"
  313. value={this.state.lexis}
  314. onChange={this.onChange}
  315. error={errors.lexis}
  316. options={lexisOptions}
  317. />
  318. <h6>Стиль слова</h6>
  319. <SelectListGroup
  320. placeholder="стиль"
  321. name="style"
  322. value={this.state.style}
  323. onChange={this.onChange}
  324. error={errors.style}
  325. options={styleOptions}
  326. />
  327. <h6>Часть речи</h6>
  328. <SelectListGroup
  329. placeholder=""
  330. name="partOfSpeech"
  331. value={this.state.partOfSpeech}
  332. onChange={this.onChange}
  333. error={errors.partOfSpeech}
  334. options={partOfSpeechOptions}
  335. />
  336. <h6>Грамматика слова</h6>
  337. <SelectListGroup
  338. placeholder="грамматика"
  339. name="grammar"
  340. value={this.state.grammar}
  341. onChange={this.onChange}
  342. error={errors.grammar}
  343. options={grammarOptions}
  344. />
  345. <input
  346. type="submit"
  347. value="submit"
  348. className="btn btn-info btn-block mt-4"
  349. />
  350. </form>
  351. </div>
  352. </div>
  353. </div>
  354. </div>
  355. );
  356. }
  357. }
  358.  
  359. EditWord.propTypes = {
  360. getWordByID: PropTypes.func.isRequired,
  361. errors: PropTypes.object.isRequired,
  362. updateWord: PropTypes.func.isRequired
  363. };
  364.  
  365. const mapStateToProps = state => ({
  366. errors: state.errors,
  367. words: state.words
  368. });
  369.  
  370. export default connect(
  371. mapStateToProps,
  372. { getWordByID, updateWord }
  373. )(withRouter(EditWord));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement