index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import * as React from 'react';
  2. import { useState, useEffect } from 'react';
  3. import { connect, useDispatch, useSelector } from 'react-redux';
  4. import { useHistory } from "react-router-dom";
  5. import { store } from '../../redux';
  6. import Box from '@mui/material/Box';
  7. import TextField from '@mui/material/TextField';
  8. import Stack from '@mui/material/Stack';
  9. import Button from '@mui/material/Button';
  10. import Container from '@mui/material/Container';
  11. import SendRoundedIcon from '@mui/icons-material/SendRounded';
  12. import { StyledDropzone } from './dropzone';
  13. // import { actionFilesUpload, actionCreatePost, actionFullCreatePost } from '../redux/action';
  14. import { actionFilesUpload, actionFullCreatePost } from '../../redux/thunks';
  15. import { actionCreatePost } from '../../redux/action';
  16. import HighlightOffRoundedIcon from '@mui/icons-material/HighlightOffRounded';
  17. import { Dnd } from './dnd';
  18. import { UploadedFiles } from './uploaded_files';
  19. export const CreatePost = () => {
  20. const [post, setPost] = useState({ title: '', text: '', images: [] })
  21. // console.log(1, "postMain: ", post)
  22. // создаем объект с данными из форм для отправки на бек
  23. // const newPostData = ({ ...post, images: ((post.images).map(item => `{_id:${item._id}}`)) })
  24. const newPostData = ({ ...post, images: ((post.images).map(item => ({ _id: item._id }))) })
  25. // console.log(33, newPostData)
  26. // const imgForCreatePost = (post.images).map(item => item._id)
  27. // console.log('images arr: ', imgForCreatePost)
  28. const dispatch = useDispatch()
  29. // вот эту часть нужно будет сделать через connect
  30. const images = useSelector(state => state.promise?.FilesUpload?.payload)
  31. useEffect((() => {
  32. if (images) {
  33. setPost({ ...post, images: [...post.images, ...images] })
  34. }
  35. }), [images])
  36. const deleteImage = image => setPost({ ...post, images: post.images.filter(i => i !== image) })
  37. const localPostImage = ({ image }) => <UploadedFiles image={image}
  38. onDelete={imgToDelete => deleteImage(imgToDelete)} />
  39. // создаем новый пост и переходим на его страницу
  40. const history = useHistory()
  41. const newPost = useSelector(state => state.promise?.CreatePost?.payload?._id)
  42. // console.log('newPost', newPost)
  43. // какого хуя не работает await
  44. const onSend = async (data) => {
  45. await store.dispatch(actionFullCreatePost(data));
  46. if (newPost) {
  47. // console.log('щас пойдем на переход', newPost)
  48. history.push(`/post/${newPost}`);
  49. }
  50. }
  51. return (
  52. <Container maxWidth="90%">
  53. <Container>
  54. <StyledDropzone onFiles={files => dispatch(actionFilesUpload(files))} onPost={post} />
  55. </Container>
  56. <Dnd items={post.images} render={localPostImage} itemProp="image" keyField="_id"
  57. onChange={images => setPost({ ...post, images })}
  58. horizontal
  59. />
  60. <Box
  61. component="form"
  62. sx={{
  63. '& > :not(style)': { m: 1, width: '100%', marginBottom: '20px' },
  64. }}
  65. >
  66. <TextField
  67. id="title"
  68. label="Название поста"
  69. variant="standard"
  70. onChange={e => setPost({ ...post, title: e.target.value })}
  71. value={post.title}
  72. />
  73. </Box>
  74. <Box
  75. component="form"
  76. sx={{
  77. '& .MuiTextField-root': { m: 1, width: '100%', marginBottom: '20px' },
  78. }}
  79. >
  80. <TextField
  81. id="text"
  82. label="Описание поста"
  83. multiline
  84. rows={2}
  85. variant="standard"
  86. onChange={e => setPost({ ...post, text: e.target.value })}
  87. value={post.text}
  88. />
  89. </Box>
  90. <Stack spacing={2} direction="row" >
  91. <Button
  92. variant="outlined"
  93. startIcon={
  94. <SendRoundedIcon
  95. style={{ transform: 'translate(3px, -4px) rotate(-30deg)' }}
  96. />}
  97. // onClick={() => { dispatch(actionCreatePost(newPostData)) }}
  98. onClick={() => onSend(newPostData)}
  99. >
  100. Опубликовать пост
  101. </Button>
  102. </Stack >
  103. </Container >
  104. )
  105. }