EditPhotos.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { DeleteOutlined, EyeOutlined, InboxOutlined } from "@ant-design/icons";
  2. import { Button, message, Image, Progress } from "antd";
  3. import Dragger from "antd/lib/upload/Dragger";
  4. import React, { useState } from "react";
  5. import {
  6. arrayMove,
  7. SortableContainer,
  8. SortableElement,
  9. SortableHandle
  10. } from "react-sortable-hoc";
  11. import { backURL, propsUploadFile, videoRegExp } from "../../helpers";
  12. const SortableItemMask = ({ removePhotosItem, chekMedia, setVisible, id }) =>
  13. <div className="SortableItemMask">
  14. {!chekMedia && <Button type="link" onClick={() => setVisible(true)}>
  15. <EyeOutlined />
  16. </Button>}
  17. <Button type="link" onClick={() => removePhotosItem(id)}>
  18. <DeleteOutlined />
  19. </Button>
  20. </div>
  21. const Handle = SortableHandle(({ tabIndex, value, removePhotosItem }) => {
  22. const [visible, setVisible] = useState(false);
  23. const chekMedia = videoRegExp.test(value.originalFileName)
  24. return (
  25. <div className="Handle" tabIndex={tabIndex} >
  26. <SortableItemMask id={value._id} setVisible={setVisible} chekMedia={chekMedia} removePhotosItem={removePhotosItem} />
  27. {chekMedia
  28. ? <video >
  29. <source src={backURL + '/' + value.url} />
  30. </video>
  31. : <img src={backURL + '/' + value.url} />
  32. }
  33. <Image className="hidden-item"
  34. width={200}
  35. style={{ display: 'none' }}
  36. preview={{
  37. visible,
  38. src: backURL + '/' + value.url,
  39. onVisibleChange: value => {
  40. setVisible(value);
  41. },
  42. }}
  43. />
  44. </ div>
  45. )
  46. })
  47. const SortableItem = SortableElement(props => {
  48. const { value, removePhotosItem } = props
  49. return (
  50. <div className="SortableItem">
  51. <Handle value={value} removePhotosItem={removePhotosItem} />
  52. </div >
  53. );
  54. });
  55. const SortableList = SortableContainer(({ items = [], ...restProps }) => {
  56. return (
  57. <div className='SortableList'>
  58. {items?.map((item, index) => (
  59. <SortableItem
  60. key={`item-${item._id}`}
  61. index={index}
  62. value={item}
  63. {...restProps}
  64. />
  65. ))}
  66. </div>
  67. );
  68. });
  69. export function EditPhotos({ photos = [], setPhotos }) {
  70. const [progress, setProgress] = useState(0);
  71. const [loading, setLoading] = useState(false);
  72. const handlerChange = async ({ file, fileList }) => {
  73. if (file.status === "uploading") {
  74. setLoading(true)
  75. setProgress(file.percent)
  76. } else if (file.status === 'done') {
  77. message.success(`${file.name} file uploaded successfully`);
  78. setPhotos([...photos || [], file.response])
  79. } else if (file.status === 'error') {
  80. message.error(`${file.name} file upload failed.`);
  81. }
  82. }
  83. const removePhotosItem = (id) => setPhotos(photos.filter(p => p._id !== id))
  84. const onSortEnd = ({ oldIndex, newIndex }) => {
  85. setPhotos(arrayMove(photos, oldIndex, newIndex));
  86. };
  87. return (
  88. <div className="EditPhotos" >
  89. {photos?.length >= 8 ? null
  90. : <Dragger {...propsUploadFile}
  91. className="EditPhotos__box"
  92. multiple={true}
  93. maxCount={8}
  94. listType="picture-card"
  95. showUploadList={false}
  96. onChange={handlerChange}>
  97. <p className="ant-upload-drag-icon">
  98. <InboxOutlined />
  99. </p>
  100. <p className="ant-upload-text">
  101. Click or drag file to this area to upload
  102. </p>
  103. </Dragger>
  104. }
  105. {loading &&
  106. <Progress
  107. showInfo={false}
  108. percent={progress}
  109. strokeColor={{
  110. '0%': '#10136c',
  111. '50%': '#755596',
  112. '100%': '#fdc229',
  113. }}
  114. />}
  115. <SortableList
  116. shouldUseDragHandle={true}
  117. useDragHandle
  118. axis="xy"
  119. removePhotosItem={removePhotosItem}
  120. items={photos}
  121. onSortEnd={onSortEnd}
  122. />
  123. </div>
  124. );
  125. }