index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import React, { useState, useEffect } from 'react'
  2. import { Link } from 'react-router-dom'
  3. import { connect } from 'react-redux'
  4. import {
  5. actionUploadFiles,
  6. actionPostUpsert,
  7. actionClearPromise,
  8. } from '../../actions'
  9. import { actionClearOnePostType } from '../../actions/types/postActionTypes'
  10. import { Button, message } from 'antd'
  11. import {
  12. Dropzone
  13. } from '../../components/UploadFiles'
  14. import {
  15. SortableContainer,
  16. SortableItem
  17. } from '../../components/Sortable'
  18. import { arrayMoveImmutable } from 'array-move'
  19. import { Row, Col } from 'antd'
  20. import { history } from '../../helpers'
  21. import { CustomInput } from '../../components/Input'
  22. import {actionCreateEditPost} from '../../redux/saga'
  23. const checkRoute = ({ match }) => {
  24. console.log('match route', match)
  25. }
  26. const PostEditor = ({
  27. match: {
  28. params: { _id },
  29. },
  30. myId,
  31. onSave,
  32. onFileDrop,
  33. post,
  34. fileStatus,
  35. clearPostOne,
  36. clearPromise,
  37. newPost
  38. }) => {
  39. post = {
  40. _id: post?._id || '',
  41. title: post?.title || '',
  42. text: post?.text || '',
  43. images: post?.images || [],
  44. }
  45. console.log('post after', post)
  46. console.log('post _id', _id)
  47. const [state, setState] = useState(post)
  48. console.log('state after change', state)
  49. console.log('post', post)
  50. useEffect(() => {
  51. if (fileStatus?.status === 'FULFILLED' && fileStatus?.payload != [])
  52. setState({
  53. ...state,
  54. images: [...state?.images, ...fileStatus?.payload],
  55. })
  56. else if (fileStatus?.status === 'REJECTED') message.error('Error')
  57. }, [fileStatus])
  58. console.log('images ', state?.images)
  59. const onSortEnd = ({ oldIndex, newIndex }) => {
  60. setState({
  61. ...state,
  62. images: arrayMoveImmutable(state?.images, oldIndex, newIndex),
  63. })
  64. }
  65. const onChangeTitle = (event) =>
  66. setState({
  67. ...state,
  68. title: event.target.value,
  69. })
  70. const onChangeText = (event) =>
  71. setState({
  72. ...state,
  73. text: event.target.value,
  74. })
  75. const onRemoveImage = (_id) =>
  76. setState({
  77. ...state,
  78. images: state.images.filter((item) => item._id !== _id),
  79. })
  80. const disabledBtn =
  81. state?.images && state?.title && state?.text ? false : true
  82. const savePost = () =>
  83. onSave(state) &&
  84. message.success(`Post published success!`) &&
  85. history.push(`/profile/${myId}`)
  86. const checkLength = () => {
  87. if (state?.images?.length > 8) {
  88. message.error('Error, please, upload maximum 8 elements')
  89. state['images'] = []
  90. return false
  91. } else {
  92. return <h2 className='NumberPosts'> {state?.images.length} / 8</h2>
  93. }
  94. }
  95. return (
  96. <section className="Post">
  97. {/* <Row> */}
  98. {/* <Col span={12} offset={6}> */}
  99. <Dropzone onLoad={onFileDrop} />
  100. {/* <Col offset={1}> */}
  101. <div style={{}}>
  102. <SortableContainer
  103. onSortEnd={onSortEnd}
  104. >
  105. {state?.images?.length <= 8 &&
  106. (state?.images || []).map(({ _id, url }, index) => (
  107. <SortableItem
  108. key={`item-${_id}`}
  109. url={url}
  110. index={index}
  111. onRemoveImage={onRemoveImage}
  112. _id={_id}
  113. />
  114. ))}
  115. </SortableContainer>
  116. </div>
  117. {checkLength()}
  118. {/* </Col> */}
  119. {/* </Col> */}
  120. {/* </Row> */}
  121. <h2 className="Title"> Title </h2>
  122. <CustomInput
  123. state={state?.title || ''}
  124. className="Input"
  125. onChangeText={onChangeTitle}
  126. />
  127. <h2 className="Title"> Text </h2>
  128. <CustomInput
  129. state={state?.text || ''}
  130. className="Input"
  131. onChangeText={onChangeText}
  132. />
  133. <br />
  134. <Col offset={5}>
  135. <Button
  136. style={{
  137. display: 'flex',
  138. marginTop: '30px',
  139. alignItems: 'center',
  140. alignContent: 'center',
  141. justifyContent: 'center',
  142. width: '200px',
  143. }}
  144. disabled={disabledBtn}
  145. onClick={savePost}
  146. size="large"
  147. type="primary"
  148. >
  149. Save
  150. </Button>
  151. </Col>
  152. </section>
  153. )
  154. }
  155. // export const CPostEditor = connect(
  156. // (state) => ({
  157. // fileStatus: state.promise?.uploadFiles,
  158. // post: state?.post?.onePost,
  159. // myId: state?.myData?.aboutMe?._id,
  160. // }),
  161. // {
  162. // onSave: actionCreateEditPost,
  163. // onFileDrop: actionUploadFiles,
  164. // clearPostOne: actionClearOnePostType,
  165. // // clearPromise: actionClearPromise,
  166. // },
  167. // )(PostEditor)
  168. export const CPostCreator = connect(
  169. (state) => ({
  170. fileStatus: state.promise?.uploadFiles,
  171. // post: state?.post?.onePost,
  172. myId: state?.myData?.aboutMe?._id,
  173. newPost:true
  174. }),
  175. {
  176. onSave: actionCreateEditPost,
  177. onFileDrop: actionUploadFiles,
  178. clearPostOne: actionClearOnePostType,
  179. clearPromise: actionClearPromise,
  180. },
  181. )(PostEditor)
  182. export const CPostEditor = connect(
  183. (state) => ({
  184. fileStatus: state.promise?.uploadFiles,
  185. post: state?.post?.onePost,
  186. myId: state?.myData?.aboutMe?._id,
  187. }),
  188. {
  189. onSave: actionCreateEditPost,
  190. onFileDrop: actionUploadFiles,
  191. clearPostOne: actionClearOnePostType,
  192. // clearPromise: actionClearPromise,
  193. },
  194. )(PostEditor)