import { Component, linkEvent } from "inferno"; import { i18n } from "../../i18next"; import { HttpService, UserService } from "../../services"; import { randomStr, toast } from "../../utils"; import { Icon } from "./icon"; 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 (
); } handleImageUpload(i: ImageUploadForm, event: any) { event.preventDefault(); const image = event.target.files[0] as File; i.setState({ loading: true }); HttpService.client.uploadImage({ image }).then(res => { console.log("pictrs upload:"); console.log(res); if (res.state === "success") { if (res.data.msg === "ok") { i.props.onUpload(res.data.url as string); } else { toast(JSON.stringify(res), "danger"); } } else if (res.state === "failed") { console.error(res.msg); toast(res.msg, "danger"); } i.setState({ loading: false }); }); } handleRemoveImage(i: ImageUploadForm, event: any) { event.preventDefault(); i.setState({ loading: true }); i.props.onRemove(); } }