index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React, { useState, useEffect } from 'react'
  2. import { connect } from 'react-redux'
  3. import { actionUploadFiles } from '../../actions/query/uploadFilesQuery'
  4. import { actionClearOnePostType } from '../../actions/types/postTypes'
  5. import { Button, message } from 'antd'
  6. import { Dropzone } from '../../components/UploadFiles'
  7. import { SortableContainer, SortableItem } from '../../components/Sortable'
  8. import { arrayMoveImmutable } from 'array-move'
  9. import { Col } from 'antd'
  10. import history from '../../helpers/history'
  11. import { CustomInput } from '../../components/Input'
  12. import { actionCreateEditPostTypeSaga } from '../../actions/typeSaga/postTypesSaga'
  13. import { actionClearPromiseForName } from '../../actions/types/promiseTypes'
  14. const PostEditor = ({
  15. match: {
  16. params: { _id },
  17. },
  18. myId,
  19. onSave,
  20. onFileDrop,
  21. post,
  22. fileStatus,
  23. }) => {
  24. post = {
  25. _id: post?._id || '',
  26. title: post?.title || '',
  27. text: post?.text || '',
  28. images: post?.images || [],
  29. }
  30. const [state, setState] = useState(post)
  31. useEffect(() => {
  32. if (fileStatus?.status === 'FULFILLED' && fileStatus?.payload != [])
  33. setState({
  34. ...state,
  35. images: [...state?.images, ...fileStatus?.payload],
  36. })
  37. else if (fileStatus?.status === 'REJECTED') message.error('Error')
  38. }, [fileStatus])
  39. console.log('state images ', state?.images)
  40. const onSortEnd = ({ oldIndex, newIndex }) => {
  41. setState({
  42. ...state,
  43. images: arrayMoveImmutable(state?.images, oldIndex, newIndex),
  44. })
  45. }
  46. const onChangeTitle = (event) =>
  47. setState({
  48. ...state,
  49. title: event.target.value,
  50. })
  51. const onChangeText = (event) =>
  52. setState({
  53. ...state,
  54. text: event.target.value,
  55. })
  56. const onRemoveImage = (_id) =>
  57. setState({
  58. ...state,
  59. images: state.images.filter((item) => item._id !== _id),
  60. })
  61. const disabledBtn =
  62. state?.images && state?.title && state?.text ? false : true
  63. const savePost = () =>
  64. onSave(state) &&
  65. message.success(`Post published success!`) &&
  66. history.push(`/profile/${myId}`)
  67. const checkLength = () => {
  68. if (state?.images?.length > 8) {
  69. message.error('Error, please, upload maximum 8 elements')
  70. state['images'] = []
  71. return false
  72. } else {
  73. return <p className="NumberPosts"> {state?.images.length} / 8</p>
  74. }
  75. }
  76. return (
  77. <section className="Post">
  78. <Dropzone onLoad={onFileDrop} />
  79. <div style={{}}>
  80. <SortableContainer onSortEnd={onSortEnd}>
  81. {state?.images?.length <= 8 &&
  82. (state?.images || []).map(({_id, url, originalFileName }, index) => (
  83. <SortableItem
  84. key={`item-${_id}`}
  85. url={url}
  86. index={index}
  87. onRemoveImage={onRemoveImage}
  88. _id={_id}
  89. originalFileName={originalFileName}
  90. />
  91. ))}
  92. </SortableContainer>
  93. </div>
  94. {checkLength()}
  95. <h2 className="Title"> Title </h2>
  96. <CustomInput
  97. state={state?.title || ''}
  98. className="Input"
  99. onChangeText={onChangeTitle}
  100. />
  101. <h2 className="Title"> Text </h2>
  102. <CustomInput
  103. state={state?.text || ''}
  104. className="Input"
  105. onChangeText={onChangeText}
  106. />
  107. <br />
  108. <Col offset={5}>
  109. <Button
  110. className='Save'
  111. disabled={disabledBtn}
  112. onClick={savePost}
  113. size="large"
  114. type="primary"
  115. >
  116. Save
  117. </Button>
  118. </Col>
  119. </section>
  120. )
  121. }
  122. export const CPostCreator = connect(
  123. (state) => ({
  124. fileStatus: state.promise?.uploadFiles,
  125. myId: state?.myData?.aboutMe?._id,
  126. }),
  127. {
  128. onSave: actionCreateEditPostTypeSaga,
  129. onFileDrop: actionUploadFiles,
  130. clearPostOne: actionClearOnePostType,
  131. clearPromise: actionClearPromiseForName,
  132. },
  133. )(PostEditor)
  134. export const CPostEditor = connect(
  135. (state) => ({
  136. fileStatus: state.promise?.uploadFiles,
  137. post: state?.post?.onePost,
  138. myId: state?.myData?.aboutMe?._id,
  139. }),
  140. {
  141. onSave: actionCreateEditPostTypeSaga,
  142. onFileDrop: actionUploadFiles,
  143. clearPostOne: actionClearOnePostType,
  144. },
  145. )(PostEditor)