additionalImages.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { useEffect, useState } from 'react';
  2. import { Upload, Button, Space } from 'antd';
  3. import ImgCrop from 'antd-img-crop';
  4. import { connect } from 'react-redux';
  5. import { backURL } from '../api';
  6. import { actionUploadImg, actionGoodById , actionChangeArrInGood, actionImgClear } from '../action';
  7. import { Link } from 'react-router-dom';
  8. const Additional = ({imgs, upload, _id, goodById, good, saveImg, imgClear}) => {
  9. const [fileList, setFileList] = useState([]);
  10. //const [selectGood, setSelectGood] = useState(good);
  11. useEffect(() => {
  12. goodById(_id);
  13. }, [_id])
  14. console.log('gooood', good);
  15. const onChange = ({ fileList: newFileList }) => {
  16. setFileList(newFileList); console.log('newFileList', newFileList)
  17. };
  18. const onPreview = async file => {
  19. let src = file.url; console.log('file', file)
  20. if (!src) {
  21. src = await new Promise(resolve => {
  22. const reader = new FileReader();
  23. reader.readAsDataURL(file.originFileObj);
  24. reader.onload = () => resolve(reader.result);
  25. });
  26. }
  27. const image = new Image();
  28. image.src = src;
  29. const imgWindow = window.open(src);
  30. imgWindow.document.write(image.outerHTML);
  31. };
  32. const onChangeArrImg = () => {
  33. const newArrImg = [... good.images].map((item) => ({"_id": item._id}));
  34. for (let i = 0; i < Object.keys(imgs).length; i++) {
  35. const newImgId = imgs[`${i}`].payload._id;
  36. newArrImg.push({"_id":newImgId});
  37. saveImg(good._id, newArrImg);
  38. }
  39. }
  40. const onSave = async () => {
  41. const newF = [...fileList].map((item) => [item.originFileObj]);
  42. const newFF = await newF.map((item, index) => [upload(item, index)]);
  43. // await onChangeArrImg();
  44. setFileList([]);
  45. // imgClear();
  46. }
  47. return (
  48. <div>
  49. <ImgCrop rotate>
  50. <Upload
  51. action=""
  52. listType="picture-card"
  53. fileList={fileList}
  54. onChange={onChange}
  55. onPreview={onPreview}
  56. >
  57. {fileList.length < 4 && '+ Добавить фото'}
  58. </Upload>
  59. </ImgCrop>
  60. <Space direction="vertical">
  61. <Button type="primary" onClick={onSave}>Закачать</Button>
  62. <Button type="primary" onClick={() => {onChangeArrImg(); imgClear()}}>Сохранить</Button>
  63. <Link to={`/goodAdmin/${_id}`}>
  64. <Button type="primary" >Просмотреть карточку товара</Button>
  65. </Link>
  66. </Space>
  67. </div>
  68. );
  69. };
  70. const mapStateToProps = (state) => ({
  71. imgs: state.img || [],
  72. good: state.promise.goodById?.payload || {},
  73. })
  74. const mapDispatchToProps = {
  75. upload: actionUploadImg,
  76. goodById: actionGoodById,
  77. saveImg: actionChangeArrInGood,
  78. imgClear: actionImgClear,
  79. }
  80. const AdditionalImages = connect(mapStateToProps, mapDispatchToProps)(Additional);
  81. export default AdditionalImages;