2023-06-21 22:28:24 +00:00
|
|
|
import { randomStr } from "@utils/helpers";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2023-06-22 00:54:35 +00:00
|
|
|
import { HttpService, I18NextService, UserService } from "../../services";
|
2023-06-21 22:28:24 +00:00
|
|
|
import { toast } from "../../toast";
|
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 (
|
2023-06-20 18:46:16 +00:00
|
|
|
<form className="image-upload-form d-inline">
|
2023-06-24 22:41:29 +00:00
|
|
|
<label htmlFor={this.id} className="pointer text-muted small fw-bold">
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.props.imageSrc ? (
|
|
|
|
<span className="d-inline-block position-relative">
|
2023-06-24 23:44:22 +00:00
|
|
|
{/* TODO: Create "Current Iamge" translation for alt text */}
|
2023-01-04 16:56:24 +00:00
|
|
|
<img
|
2023-06-24 23:44:22 +00:00
|
|
|
alt=""
|
2023-01-04 16:56:24 +00:00
|
|
|
src={this.props.imageSrc}
|
|
|
|
height={this.props.rounded ? 60 : ""}
|
|
|
|
width={this.props.rounded ? 60 : ""}
|
|
|
|
className={`img-fluid ${
|
|
|
|
this.props.rounded ? "rounded-circle" : ""
|
|
|
|
}`}
|
|
|
|
/>
|
2023-06-24 23:44:22 +00:00
|
|
|
<button
|
2023-06-24 23:58:06 +00:00
|
|
|
className="position-absolute d-block p-0 end-0 border-0 top-0 bg-transparent text-white"
|
2023-06-24 23:44:22 +00:00
|
|
|
type="button"
|
2023-01-04 16:56:24 +00:00
|
|
|
onClick={linkEvent(this, this.handleRemoveImage)}
|
2023-06-22 00:54:35 +00:00
|
|
|
aria-label={I18NextService.i18n.t("remove")}
|
2023-01-04 16:56:24 +00:00
|
|
|
>
|
|
|
|
<Icon icon="x" classes="mini-overlay" />
|
2023-06-24 23:44:22 +00:00
|
|
|
</button>
|
2023-01-04 16:56:24 +00:00
|
|
|
</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-06-14 12:20:40 +00:00
|
|
|
const image = event.target.files[0] as File;
|
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-06-14 12:20:40 +00:00
|
|
|
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);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(JSON.stringify(res), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
} else if (res.state === "failed") {
|
|
|
|
console.error(res.msg);
|
|
|
|
toast(res.msg, "danger");
|
|
|
|
}
|
|
|
|
|
|
|
|
i.setState({ loading: false });
|
|
|
|
});
|
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();
|
|
|
|
}
|
|
|
|
}
|