EditPhotos.js 4.4 KB

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