import * as React from "react"; import * as Constants from "~/common/constants"; import * as System from "~/components/system"; import * as Validations from "~/common/validations"; import { css } from "@emotion/react"; import { DataMeterBar } from "~/components/core/DataMeter"; const STYLES_FILE_HIDDEN = css` height: 1px; width: 1px; opacity: 0; visibility: hidden; position: fixed; top: -1px; left: -1px; `; const STYLES_FOCUS = css` font-size: ${Constants.typescale.lvl1}; font-family: ${Constants.font.medium}; overflow-wrap: break-word; width: 100%; strong { font-family: ${Constants.font.semiBold}; font-weight: 400; } `; const STYLES_SUBTEXT = css` margin-top: 8px; font-size: 12px; `; const STYLES_ITEM = css` margin-top: 16px; `; const STYLES_IMAGE_PREVIEW = css` display: block; width: 100%; margin-top: 48px; `; const STYLES_STRONG = css` display: block; margin: 16px 0 4px 0; font-weight: 400; font-family: ${Constants.font.medium}; font-size: 0.8rem; `; export default class SidebarAddFileToBucket extends React.Component { _handleUpload = async (e) => { e.persist(); let files = []; let fileLoading = {}; for (let i = 0; i < e.target.files.length; i++) { let file = e.target.files[i]; if (!file) { alert("TODO: Something went wrong"); continue; } const isAllowed = Validations.isFileTypeAllowed(file.type); if (!isAllowed) { alert("TODO: File type is not allowed, yet."); continue; } files.push(file); fileLoading[`${file.lastModified}-${file.name}`] = { name: file.name, loaded: 0, total: file.size, }; } if (!files.length) { alert("TODO: Files not supported error"); return this.props.onRegisterFileLoading({ fileLoading: null }); } this.props.onRegisterFileLoading({ fileLoading }); for (let i = 0; i < files.length; i++) { const file = files[i]; const slate = this.props.data && this.props.data.slateId ? { id: this.props.data.slateId } : null; const response = await this.props.onUploadFile({ file, slate, }); if (!response) { alert("TODO: File upload error"); continue; } if (response.error) { alert("TODO: File upload error"); continue; } } await this.props.onRehydrate({ resetFiles: true }); }; render() { return ( Upload data {this.props.data && this.props.data.decorator === "SLATE" ? ( This will add data to your Slate named {this.props.data.slatename}. ) : null} Add file {!this.props.fileLoading ? ( Cancel ) : null}
{this.props.fileLoading ? Object.keys(this.props.fileLoading).map((timestamp) => { const p = this.props.fileLoading[timestamp]; return ( {p.name} ); }) : null}
); } }