2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { pictrsUri } from "../../env";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService } from "../../services";
|
|
|
|
import { randomStr, toast } from "../../utils";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Icon } from "./icon";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface ImageUploadFormProps {
|
|
|
|
uploadTitle: string;
|
|
|
|
imageSrc: string;
|
|
|
|
onUpload(url: string): any;
|
|
|
|
onRemove(): any;
|
|
|
|
rounded?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ImageUploadFormState {
|
|
|
|
loading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ImageUploadForm extends Component<
|
|
|
|
ImageUploadFormProps,
|
|
|
|
ImageUploadFormState
|
|
|
|
> {
|
|
|
|
private id = `image-upload-form-${randomStr()}`;
|
|
|
|
private emptyState: ImageUploadFormState = {
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<form class="d-inline">
|
|
|
|
<label
|
|
|
|
htmlFor={this.id}
|
|
|
|
class="pointer ml-4 text-muted small font-weight-bold"
|
|
|
|
>
|
|
|
|
{!this.props.imageSrc ? (
|
|
|
|
<span class="btn btn-secondary">{this.props.uploadTitle}</span>
|
|
|
|
) : (
|
|
|
|
<span class="d-inline-block position-relative">
|
|
|
|
<img
|
|
|
|
src={this.props.imageSrc}
|
2021-02-22 02:39:04 +00:00
|
|
|
height={this.props.rounded ? 60 : ""}
|
|
|
|
width={this.props.rounded ? 60 : ""}
|
2020-09-06 16:15:25 +00:00
|
|
|
className={`img-fluid ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.props.rounded ? "rounded-circle" : ""
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
/>
|
2021-02-06 20:20:41 +00:00
|
|
|
<a
|
|
|
|
onClick={linkEvent(this, this.handleRemoveImage)}
|
2021-02-22 02:39:04 +00:00
|
|
|
aria-label={i18n.t("remove")}
|
2021-02-06 20:20:41 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="x" classes="mini-overlay" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
id={this.id}
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name={this.id}
|
|
|
|
class="d-none"
|
2021-08-20 02:56:18 +00:00
|
|
|
disabled={!UserService.Instance.myUserInfo}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUpload(i: ImageUploadForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
let file = event.target.files[0];
|
|
|
|
const formData = new FormData();
|
2021-02-22 02:39:04 +00:00
|
|
|
formData.append("images[]", file);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
i.state.loading = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
|
2020-09-11 03:19:43 +00:00
|
|
|
fetch(pictrsUri, {
|
2021-02-22 02:39:04 +00:00
|
|
|
method: "POST",
|
2020-09-06 16:15:25 +00:00
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
2021-02-22 02:39:04 +00:00
|
|
|
console.log("pictrs upload:");
|
2020-09-06 16:15:25 +00:00
|
|
|
console.log(res);
|
2021-02-22 02:39:04 +00:00
|
|
|
if (res.msg == "ok") {
|
2020-09-06 16:15:25 +00:00
|
|
|
let hash = res.files[0].file;
|
2020-09-11 03:19:43 +00:00
|
|
|
let url = `${pictrsUri}/${hash}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
i.state.loading = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.props.onUpload(url);
|
|
|
|
} else {
|
|
|
|
i.state.loading = false;
|
|
|
|
i.setState(i.state);
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(JSON.stringify(res), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
i.state.loading = false;
|
|
|
|
i.setState(i.state);
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(error, "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRemoveImage(i: ImageUploadForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.loading = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.props.onRemove();
|
|
|
|
}
|
|
|
|
}
|