2020-07-23 13:05:13 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Constants from "~/common/constants";
|
|
|
|
import * as System from "~/components/system";
|
2020-08-02 09:00:04 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-07-23 13:05:13 +03:00
|
|
|
|
|
|
|
import { css } from "@emotion/react";
|
2020-08-15 05:08:17 +03:00
|
|
|
import { DataMeterBar } from "~/components/core/DataMeter";
|
2020-07-23 13:05:13 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
`;
|
|
|
|
|
2020-08-15 05:08:17 +03:00
|
|
|
const STYLES_STRONG = css`
|
|
|
|
display: block;
|
|
|
|
margin: 16px 0 4px 0;
|
|
|
|
font-weight: 400;
|
|
|
|
font-family: ${Constants.font.medium};
|
|
|
|
font-size: 0.8rem;
|
|
|
|
`;
|
|
|
|
|
2020-07-23 13:05:13 +03:00
|
|
|
export default class SidebarAddFileToBucket extends React.Component {
|
|
|
|
_handleUpload = async (e) => {
|
|
|
|
e.persist();
|
2020-08-16 01:38:09 +03:00
|
|
|
let files = [];
|
|
|
|
let fileLoading = {};
|
|
|
|
for (let i = 0; i < e.target.files.length; i++) {
|
|
|
|
let file = e.target.files[i];
|
|
|
|
|
|
|
|
if (!file) {
|
2020-09-03 00:04:47 +03:00
|
|
|
alert("We could not find any files to upload.");
|
2020-08-16 01:38:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isAllowed = Validations.isFileTypeAllowed(file.type);
|
|
|
|
if (!isAllowed) {
|
2020-09-03 00:04:47 +03:00
|
|
|
alert(
|
|
|
|
`We currently do not accept ${file.type} yet but may in the future.`
|
|
|
|
);
|
2020-08-16 01:38:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
files.push(file);
|
|
|
|
fileLoading[`${file.lastModified}-${file.name}`] = {
|
|
|
|
name: file.name,
|
|
|
|
loaded: 0,
|
|
|
|
total: file.size,
|
|
|
|
};
|
2020-08-02 09:00:04 +03:00
|
|
|
}
|
|
|
|
|
2020-08-16 01:38:09 +03:00
|
|
|
if (!files.length) {
|
2020-09-03 00:04:47 +03:00
|
|
|
alert("We could not find any files to upload.");
|
2020-08-22 10:09:23 +03:00
|
|
|
return this.props.onRegisterFileLoading({ fileLoading: null });
|
2020-07-23 13:05:13 +03:00
|
|
|
}
|
|
|
|
|
2020-08-20 08:20:18 +03:00
|
|
|
this.props.onRegisterFileLoading({ fileLoading });
|
2020-08-15 05:08:17 +03:00
|
|
|
|
2020-08-16 01:38:09 +03:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
const file = files[i];
|
2020-08-27 02:24:43 +03:00
|
|
|
const slate =
|
|
|
|
this.props.data && this.props.data.slateId
|
|
|
|
? { id: this.props.data.slateId }
|
|
|
|
: null;
|
2020-08-16 01:38:09 +03:00
|
|
|
|
|
|
|
const response = await this.props.onUploadFile({
|
|
|
|
file,
|
|
|
|
slate,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response) {
|
2020-09-03 00:04:47 +03:00
|
|
|
alert(
|
|
|
|
"Something went wrong with saving your new file. Please refresh your browser."
|
|
|
|
);
|
2020-08-16 01:38:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-15 05:18:55 +03:00
|
|
|
|
2020-08-16 01:38:09 +03:00
|
|
|
if (response.error) {
|
2020-09-03 00:04:47 +03:00
|
|
|
alert(
|
|
|
|
"Something went wrong with saving your new file. Please refresh your browser."
|
|
|
|
);
|
2020-08-16 01:38:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-15 05:18:55 +03:00
|
|
|
}
|
|
|
|
|
2020-08-15 05:08:17 +03:00
|
|
|
await this.props.onRehydrate({ resetFiles: true });
|
2020-09-03 09:00:02 +03:00
|
|
|
|
|
|
|
dispatchCustomEvent({
|
|
|
|
name: "remote-update-slate-screen",
|
|
|
|
detail: {},
|
|
|
|
});
|
2020-07-23 13:05:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-08-27 02:24:43 +03:00
|
|
|
<System.P style={{ fontFamily: Constants.font.semiBold }}>
|
|
|
|
Upload Data
|
|
|
|
</System.P>
|
|
|
|
<input
|
|
|
|
css={STYLES_FILE_HIDDEN}
|
|
|
|
multiple
|
|
|
|
type="file"
|
|
|
|
id="file"
|
|
|
|
onChange={this._handleUpload}
|
|
|
|
/>
|
2020-07-23 13:05:13 +03:00
|
|
|
|
2020-07-27 11:33:39 +03:00
|
|
|
{this.props.data && this.props.data.decorator === "SLATE" ? (
|
|
|
|
<System.P style={{ marginTop: 24 }}>
|
2020-08-27 02:24:43 +03:00
|
|
|
This will add data to your Slate named{" "}
|
|
|
|
<strong>{this.props.data.slatename}</strong>.
|
2020-07-27 11:33:39 +03:00
|
|
|
</System.P>
|
|
|
|
) : null}
|
|
|
|
|
2020-08-02 22:17:13 +03:00
|
|
|
<System.ButtonPrimary
|
|
|
|
full
|
2020-07-23 13:05:13 +03:00
|
|
|
type="label"
|
|
|
|
htmlFor="file"
|
|
|
|
style={{ marginTop: 24 }}
|
2020-08-27 02:24:43 +03:00
|
|
|
loading={!!this.props.fileLoading}
|
|
|
|
>
|
2020-07-23 13:05:13 +03:00
|
|
|
Add file
|
2020-08-02 22:17:13 +03:00
|
|
|
</System.ButtonPrimary>
|
2020-07-23 13:05:13 +03:00
|
|
|
|
2020-08-16 04:32:38 +03:00
|
|
|
<br />
|
|
|
|
|
2020-08-15 05:08:17 +03:00
|
|
|
{this.props.fileLoading
|
|
|
|
? Object.keys(this.props.fileLoading).map((timestamp) => {
|
|
|
|
const p = this.props.fileLoading[timestamp];
|
|
|
|
return (
|
|
|
|
<React.Fragment key={timestamp}>
|
|
|
|
<strong css={STYLES_STRONG}>{p.name}</strong>
|
2020-08-16 04:32:38 +03:00
|
|
|
<DataMeterBar
|
|
|
|
failed={p.failed}
|
2020-08-17 07:49:50 +03:00
|
|
|
leftLabel={p.failed ? "failed" : "uploaded"}
|
|
|
|
rightLabel={p.failed ? null : "total"}
|
2020-08-16 04:32:38 +03:00
|
|
|
bytes={p.loaded}
|
|
|
|
maximumBytes={p.total}
|
|
|
|
/>
|
2020-08-15 05:08:17 +03:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
: null}
|
2020-07-23 13:05:13 +03:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|