import React, { useState } from "react"; import PropTypes from "prop-types"; import Attachment from "./Attachments/Attachment"; import Placeholder from "./Attachments/Placeholder"; import FileUploadButton from "./FileUploadButton"; import { post } from "../lib/request"; import { createDraggable, draggedOrder, useDragCollection, useDragUploader } from "./drag"; function filenameToName(str) { return str.replace(/\.[\w\d]+$/, "").replace(/_/g, " "); } export default function Attachments(props) { const collection = useDragCollection(props.records); const locales = props.locales && props.locales.length > 0 ? Object.keys(props.locales) : [props.locale]; const [deleted, setDeleted] = useState([]); const uploadAttachment = (file) => { let name = {}; locales.forEach((l) => name[l] = file.name); const draggable = createDraggable( { attachment: { filename: file.name, name: name }, uploading: true } ); let data = new FormData(); data.append("attachment[file]", file); locales.forEach((l) => { data.append(`attachment[name][${l}]`, filenameToName(file.name)); }); post("/admin/attachments.json", data) .then(json => { collection.dispatch({ type: "update", payload: { ...draggable, record: { attachment: json, uploading: false } } }); }); return draggable; }; const receiveFiles = (files) => { collection.dispatch({ type: "append", payload: files.map(f => uploadAttachment(f)) }); }; const dragEnd = (dragState, files) => { collection.dispatch({ type: "reorder", payload: draggedOrder(collection, dragState) }); collection.dispatch({ type: "insertFiles", payload: files.map(f => uploadAttachment(f)) }); }; const [dragState, dragStart, listeners] = useDragUploader([collection], dragEnd); const position = (record) => { return [...collection.draggables.map(d => d.record), ...deleted].indexOf(record) + 1; }; const attrName = (record) => { return `${props.attribute}[${position(record)}]`; }; const update = (draggable) => (attachment) => { const { record } = draggable; const updated = { ...draggable, record: { ...record, attachment: { ...record.attachment, ...attachment } } }; collection.dispatch({ type: "update", payload: updated }); }; const remove = (draggable) => () => { collection.dispatch({ type: "remove", payload: draggable }); if (draggable.record.id) { setDeleted([...deleted, draggable.record]); } }; const attachment = (draggable) => { const { dragging } = dragState; if (draggable === "Files") { return (); } return ( ); }; const dragOrder = draggedOrder(collection, dragState); const classes = ["attachments"]; if (dragState.dragging) { classes.push("dragover"); } return (
{dragOrder.map(d => attachment(d))}
{deleted.map(r => )}
); } Attachments.propTypes = { attribute: PropTypes.string, locale: PropTypes.string, locales: PropTypes.object, records: PropTypes.array, showEmbed: PropTypes.bool };