index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. const onSortEnd = ({ oldIndex, newIndex }) => {
  40. setState({
  41. ...state,
  42. images: arrayMoveImmutable(state?.images, oldIndex, newIndex),
  43. })
  44. }
  45. const onChangeTitle = (event) =>
  46. setState({
  47. ...state,
  48. title: event.target.value,
  49. })
  50. const onChangeText = (event) =>
  51. setState({
  52. ...state,
  53. text: event.target.value,
  54. })
  55. const onRemoveImage = (_id) =>
  56. setState({
  57. ...state,
  58. images: state.images.filter((item) => item._id !== _id),
  59. })
  60. const disabledBtn =
  61. state?.images && state?.title && state?.text ? false : true
  62. const savePost = () =>
  63. onSave(state) &&
  64. message.success(`Post published success!`) &&
  65. history.push(`/profile/${myId}`)
  66. const checkLength = () => {
  67. if (state?.images?.length > 8) {
  68. message.error('Error, please, upload maximum 8 elements')
  69. state['images'] = []
  70. return false
  71. } else {
  72. return <h2 className="NumberPosts"> {state?.images.length} / 8</h2>
  73. }
  74. }
  75. return (
  76. <section className="Post">
  77. <Dropzone onLoad={onFileDrop} />
  78. <div style={{}}>
  79. <SortableContainer onSortEnd={onSortEnd}>
  80. {state?.images?.length <= 8 &&
  81. (state?.images || []).map(({ _id, url }, index) => (
  82. <SortableItem
  83. key={`item-${_id}`}
  84. url={url}
  85. index={index}
  86. onRemoveImage={onRemoveImage}
  87. _id={_id}
  88. />
  89. ))}
  90. </SortableContainer>
  91. </div>
  92. {checkLength()}
  93. <h2 className="Title"> Title </h2>
  94. <CustomInput
  95. state={state?.title || ''}
  96. className="Input"
  97. onChangeText={onChangeTitle}
  98. />
  99. <h2 className="Title"> Text </h2>
  100. <CustomInput
  101. state={state?.text || ''}
  102. className="Input"
  103. onChangeText={onChangeText}
  104. />
  105. <br />
  106. <Col offset={5}>
  107. <Button
  108. className='Save'
  109. disabled={disabledBtn}
  110. onClick={savePost}
  111. size="large"
  112. type="primary"
  113. >
  114. Save
  115. </Button>
  116. </Col>
  117. </section>
  118. )
  119. }
  120. export const CPostCreator = connect(
  121. (state) => ({
  122. fileStatus: state.promise?.uploadFiles,
  123. myId: state?.myData?.aboutMe?._id,
  124. }),
  125. {
  126. onSave: actionCreateEditPostTypeSaga,
  127. onFileDrop: actionUploadFiles,
  128. clearPostOne: actionClearOnePostType,
  129. clearPromise: actionClearPromiseForName,
  130. },
  131. )(PostEditor)
  132. export const CPostEditor = connect(
  133. (state) => ({
  134. fileStatus: state.promise?.uploadFiles,
  135. post: state?.post?.onePost,
  136. myId: state?.myData?.aboutMe?._id,
  137. }),
  138. {
  139. onSave: actionCreateEditPostTypeSaga,
  140. onFileDrop: actionUploadFiles,
  141. clearPostOne: actionClearOnePostType,
  142. },
  143. )(PostEditor)