1 import { randomStr } from "@utils/helpers";
2 import { Component, linkEvent } from "inferno";
3 import { HttpService, I18NextService, UserService } from "../../services";
4 import { toast } from "../../toast";
5 import { Icon } from "./icon";
7 interface ImageUploadFormProps {
10 onUpload(url: string): any;
15 interface ImageUploadFormState {
19 export class ImageUploadForm extends Component<
23 private id = `image-upload-form-${randomStr()}`;
24 private emptyState: ImageUploadFormState = {
28 constructor(props: any, context: any) {
29 super(props, context);
30 this.state = this.emptyState;
35 <form className="image-upload-form d-inline">
36 <label htmlFor={this.id} className="pointer text-muted small fw-bold">
37 {this.props.imageSrc ? (
38 <span className="d-inline-block position-relative">
39 {/* TODO: Create "Current Iamge" translation for alt text */}
42 src={this.props.imageSrc}
43 height={this.props.rounded ? 60 : ""}
44 width={this.props.rounded ? 60 : ""}
45 className={`img-fluid ${
46 this.props.rounded ? "rounded-circle" : ""
50 className="position-absolute d-block p-0 end-0 border-0 top-0 bg-transparent text-white"
52 onClick={linkEvent(this, this.handleRemoveImage)}
53 aria-label={I18NextService.i18n.t("remove")}
55 <Icon icon="x" classes="mini-overlay" />
59 <span className="btn btn-secondary">{this.props.uploadTitle}</span>
65 accept="image/*,video/*"
68 disabled={!UserService.Instance.myUserInfo}
69 onChange={linkEvent(this, this.handleImageUpload)}
75 handleImageUpload(i: ImageUploadForm, event: any) {
76 event.preventDefault();
77 const image = event.target.files[0] as File;
79 i.setState({ loading: true });
81 HttpService.client.uploadImage({ image }).then(res => {
82 console.log("pictrs upload:");
84 if (res.state === "success") {
85 if (res.data.msg === "ok") {
86 i.props.onUpload(res.data.url as string);
87 } else if (res.data.msg === "too_large") {
88 toast(I18NextService.i18n.t("upload_too_large"), "danger");
90 toast(JSON.stringify(res), "danger");
92 } else if (res.state === "failed") {
93 console.error(res.msg);
94 toast(res.msg, "danger");
97 i.setState({ loading: false });
101 handleRemoveImage(i: ImageUploadForm, event: any) {
102 event.preventDefault();
103 i.setState({ loading: true });