2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService } from "../../services";
|
2023-03-27 02:06:42 +00:00
|
|
|
import { randomStr, toast, uploadImage } 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;
|
2023-01-04 16:56:24 +00:00
|
|
|
imageSrc?: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
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 (
|
2022-09-22 15:03:35 +00:00
|
|
|
<form className="d-inline">
|
2020-09-06 16:15:25 +00:00
|
|
|
<label
|
|
|
|
htmlFor={this.id}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="pointer text-muted small font-weight-bold"
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.props.imageSrc ? (
|
|
|
|
<span className="d-inline-block position-relative">
|
|
|
|
<img
|
|
|
|
src={this.props.imageSrc}
|
|
|
|
height={this.props.rounded ? 60 : ""}
|
|
|
|
width={this.props.rounded ? 60 : ""}
|
|
|
|
className={`img-fluid ${
|
|
|
|
this.props.rounded ? "rounded-circle" : ""
|
|
|
|
}`}
|
|
|
|
/>
|
|
|
|
<a
|
|
|
|
onClick={linkEvent(this, this.handleRemoveImage)}
|
|
|
|
aria-label={i18n.t("remove")}
|
|
|
|
>
|
|
|
|
<Icon icon="x" classes="mini-overlay" />
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span className="btn btn-secondary">{this.props.uploadTitle}</span>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
id={this.id}
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name={this.id}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="d-none"
|
2023-01-04 16:56:24 +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();
|
2023-03-27 02:06:42 +00:00
|
|
|
const file = event.target.files[0];
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-03-27 02:06:42 +00:00
|
|
|
uploadImage(file)
|
2020-09-06 16:15:25 +00:00
|
|
|
.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);
|
2023-03-27 02:06:42 +00:00
|
|
|
if (res.msg === "ok") {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: false });
|
2023-03-27 02:06:42 +00:00
|
|
|
i.props.onUpload(res.url as string);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: false });
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(JSON.stringify(res), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: false });
|
2021-12-02 16:46:32 +00:00
|
|
|
console.error(error);
|
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();
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
i.props.onRemove();
|
|
|
|
}
|
|
|
|
}
|