2020-07-09 06:35:54 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Strings from "~/common/strings";
|
|
|
|
import * as Constants from "~/common/constants";
|
|
|
|
import * as SVG from "~/components/system/svg";
|
|
|
|
import * as System from "~/components/system";
|
2020-04-09 00:29:13 +03:00
|
|
|
|
2020-07-09 06:35:54 +03:00
|
|
|
import { css } from "@emotion/react";
|
2020-04-09 00:29:13 +03:00
|
|
|
|
2020-06-09 21:00:36 +03:00
|
|
|
const STYLES_FILE_HIDDEN = css`
|
|
|
|
height: 1px;
|
|
|
|
width: 1px;
|
|
|
|
opacity: 0;
|
|
|
|
visibility: hidden;
|
|
|
|
position: fixed;
|
|
|
|
top: -1px;
|
|
|
|
left: -1px;
|
|
|
|
`;
|
|
|
|
|
2020-04-09 00:29:13 +03:00
|
|
|
const STYLES_FOCUS = css`
|
|
|
|
font-size: ${Constants.typescale.lvl1};
|
2020-07-02 11:24:14 +03:00
|
|
|
font-family: ${Constants.font.medium};
|
2020-04-09 00:29:13 +03:00
|
|
|
overflow-wrap: break-word;
|
|
|
|
width: 100%;
|
|
|
|
|
|
|
|
strong {
|
2020-07-02 11:24:14 +03:00
|
|
|
font-family: ${Constants.font.semiBold};
|
2020-04-09 00:29:13 +03:00
|
|
|
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 SELECT_MENU_OPTIONS = [
|
2020-07-09 06:35:54 +03:00
|
|
|
{ value: "1", name: "Anywhere" },
|
|
|
|
{ value: "2", name: "China" },
|
|
|
|
{ value: "3", name: "Russia" },
|
|
|
|
{ value: "4", name: "USA" },
|
2020-04-09 00:29:13 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
const SELECT_MENU_MAP = {
|
2020-07-09 06:35:54 +03:00
|
|
|
"1": "Anywhere",
|
|
|
|
"2": "China",
|
|
|
|
"3": "Russia",
|
|
|
|
"4": "USA",
|
2020-04-09 00:29:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class SidebarFileStorageDeal extends React.Component {
|
|
|
|
state = {
|
2020-07-02 11:24:14 +03:00
|
|
|
settings_cold_default_duration: this.props.viewer
|
|
|
|
.settings_cold_default_duration,
|
|
|
|
settings_cold_default_replication_factor: this.props.viewer
|
|
|
|
.settings_cold_default_replication_factor,
|
2020-06-09 21:00:36 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_handleUpload = async (e) => {
|
|
|
|
e.persist();
|
|
|
|
let file = e.target.files[0];
|
|
|
|
|
|
|
|
if (!file) {
|
2020-07-09 06:35:54 +03:00
|
|
|
alert("Something went wrong");
|
2020-06-09 21:00:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:31:06 +03:00
|
|
|
await this.props.onSetFile({ file });
|
2020-04-09 00:29:13 +03:00
|
|
|
};
|
|
|
|
|
2020-06-09 21:00:36 +03:00
|
|
|
_handleMakeDeal = async (src) => {
|
|
|
|
const options = {
|
2020-07-09 06:35:54 +03:00
|
|
|
method: "POST",
|
|
|
|
credentials: "include",
|
2020-06-09 21:00:36 +03:00
|
|
|
headers: {
|
2020-07-09 06:35:54 +03:00
|
|
|
Accept: "application/json",
|
|
|
|
"Content-Type": "application/json",
|
2020-06-09 21:00:36 +03:00
|
|
|
},
|
|
|
|
body: JSON.stringify({ src }),
|
|
|
|
};
|
|
|
|
|
2020-07-09 06:35:54 +03:00
|
|
|
const response = await fetch("/_/deals/storage", options);
|
2020-06-09 21:00:36 +03:00
|
|
|
const json = await response.json();
|
|
|
|
return json;
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleSubmit = async (e) => {
|
|
|
|
e.persist();
|
|
|
|
|
2020-07-01 14:31:06 +03:00
|
|
|
const path = `/public/static/files/${this.props.file.name}`;
|
2020-06-09 21:00:36 +03:00
|
|
|
await this._handleMakeDeal(path);
|
|
|
|
|
|
|
|
await this.props.onSubmit({});
|
2020-04-09 00:29:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_handleCancel = () => {
|
|
|
|
this.props.onCancel();
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleChange = (e) => {
|
|
|
|
this.setState({ [e.target.name]: e.target.value });
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let addresses = {};
|
2020-06-08 09:45:53 +03:00
|
|
|
let lastAddress;
|
2020-04-09 00:29:13 +03:00
|
|
|
|
|
|
|
this.props.viewer.addresses.forEach((a) => {
|
2020-06-08 09:45:53 +03:00
|
|
|
addresses[a.address] = a;
|
|
|
|
lastAddress = a.address;
|
2020-04-09 00:29:13 +03:00
|
|
|
});
|
|
|
|
|
2020-06-08 09:45:53 +03:00
|
|
|
const currentAddress = this.props.selected.address
|
|
|
|
? addresses[this.props.selected.address]
|
|
|
|
: addresses[lastAddress];
|
2020-04-09 00:29:13 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-07-09 06:19:08 +03:00
|
|
|
<System.P style={{ fontFamily: Constants.font.semiBold }}>
|
2020-07-02 11:24:14 +03:00
|
|
|
Upload a file to the network
|
|
|
|
</System.P>
|
|
|
|
<input
|
|
|
|
css={STYLES_FILE_HIDDEN}
|
|
|
|
type="file"
|
|
|
|
id="file"
|
|
|
|
onChange={this._handleUpload}
|
|
|
|
/>
|
2020-07-01 14:31:06 +03:00
|
|
|
|
|
|
|
{this.props.file ? (
|
2020-06-09 21:00:36 +03:00
|
|
|
<div>
|
2020-07-02 11:24:14 +03:00
|
|
|
<img
|
2020-07-09 05:56:22 +03:00
|
|
|
src={`/public/static/files/${this.props.file.name}`}
|
2020-07-02 11:24:14 +03:00
|
|
|
css={STYLES_IMAGE_PREVIEW}
|
|
|
|
/>
|
2020-06-09 21:00:36 +03:00
|
|
|
|
|
|
|
<div css={STYLES_ITEM}>
|
2020-07-01 14:31:06 +03:00
|
|
|
<div css={STYLES_FOCUS}>{this.props.file.name}</div>
|
2020-06-09 21:00:36 +03:00
|
|
|
<div css={STYLES_SUBTEXT}>Name</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={STYLES_ITEM}>
|
2020-07-01 14:31:06 +03:00
|
|
|
<div css={STYLES_FOCUS}>{this.props.file.size}</div>
|
2020-06-09 21:00:36 +03:00
|
|
|
<div css={STYLES_SUBTEXT}>File size</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
2020-07-02 11:24:14 +03:00
|
|
|
<System.ButtonSecondaryFull
|
|
|
|
type="label"
|
|
|
|
htmlFor="file"
|
|
|
|
style={{ marginTop: 24 }}
|
|
|
|
>
|
2020-06-09 21:00:36 +03:00
|
|
|
Add file
|
|
|
|
</System.ButtonSecondaryFull>
|
|
|
|
|
2020-07-01 14:31:06 +03:00
|
|
|
{this.props.file ? (
|
2020-06-09 21:00:36 +03:00
|
|
|
<System.Input
|
|
|
|
containerStyle={{ marginTop: 48 }}
|
|
|
|
label="Deal duration"
|
|
|
|
name="settings_cold_default_duration"
|
2020-06-17 21:05:13 +03:00
|
|
|
placeholder="Type in epochs (~25 seconds)"
|
2020-06-09 21:00:36 +03:00
|
|
|
type="number"
|
|
|
|
value={this.state.settings_cold_default_duration}
|
|
|
|
onChange={this._handleChange}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
|
2020-07-01 14:31:06 +03:00
|
|
|
{this.props.file ? (
|
2020-06-09 21:00:36 +03:00
|
|
|
<System.Input
|
|
|
|
containerStyle={{ marginTop: 24 }}
|
|
|
|
label="Replication factor"
|
|
|
|
name="settings_cold_default_replication_factor"
|
|
|
|
value={this.state.settings_cold_default_replication_factor}
|
|
|
|
onChange={this._handleChange}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
|
2020-07-01 14:31:06 +03:00
|
|
|
{this.props.file ? (
|
2020-07-05 09:13:13 +03:00
|
|
|
<System.SelectMenu
|
|
|
|
full
|
2020-06-09 21:00:36 +03:00
|
|
|
containerStyle={{ marginTop: 24 }}
|
|
|
|
name="address"
|
|
|
|
label="Payment address"
|
|
|
|
value={this.props.selected.address}
|
|
|
|
category="address"
|
|
|
|
onChange={this.props.onSelectedChange}
|
2020-07-02 11:24:14 +03:00
|
|
|
options={this.props.viewer.addresses}
|
|
|
|
>
|
2020-06-09 21:00:36 +03:00
|
|
|
{currentAddress.name}
|
2020-07-05 09:13:13 +03:00
|
|
|
</System.SelectMenu>
|
2020-06-09 21:00:36 +03:00
|
|
|
) : null}
|
|
|
|
|
2020-07-01 14:31:06 +03:00
|
|
|
{this.props.file ? (
|
2020-07-02 11:24:14 +03:00
|
|
|
<System.ButtonPrimaryFull
|
|
|
|
style={{ marginTop: 48 }}
|
|
|
|
onClick={this._handleSubmit}
|
|
|
|
>
|
2020-06-09 21:00:36 +03:00
|
|
|
Make storage deal
|
|
|
|
</System.ButtonPrimaryFull>
|
|
|
|
) : null}
|
2020-04-09 00:29:13 +03:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|