/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useState } from "react"; import { useForm } from "react-hook-form"; import { useHistory, useParams } from "react-router-dom"; import BackendService from "../services/BackendService"; import StorageService from "../services/StorageService"; import Breadcrumbs from "../components/Breadcrumbs"; import TextField from "../components/TextField"; import FileInput from "../components/FileInput"; import Button from "../components/Button"; import { useDashboard, useWidget, useImage, useFullPreview, useChangeBackgroundColor, useWindowSize, } from "../hooks"; import Spinner from "../components/Spinner"; import ImageWidget from "../components/ImageWidget"; import Link from "../components/Link"; import PrimaryActionBar from "../components/PrimaryActionBar"; import { useTranslation } from "react-i18next"; import Alert from "../components/Alert"; import RadioButtons from "../components/RadioButtons"; interface FormValues { title: string; summary: string; showTitle: boolean; summaryBelow: boolean; altText: string; scalePct: string; } interface PathParams { dashboardId: string; widgetId: string; } function EditImage() { const history = useHistory(); const { t } = useTranslation(); const { dashboardId, widgetId } = useParams(); const { dashboard, loading } = useDashboard(dashboardId); const { setWidget, widget } = useWidget(dashboardId, widgetId); const { register, errors, handleSubmit } = useForm(); const { file, loadingFile } = useImage(widget?.content.s3Key.raw); const [newImageFile, setNewImageFile] = useState(undefined); const [imageUploading, setImageUploading] = useState(false); const supportedImageFileTypes = Object.values(StorageService.imageFileTypes); const previewPanelId = "preview-image-panel"; const { fullPreview, fullPreviewButton } = useFullPreview(previewPanelId); const windowSize = useWindowSize(); const isMobile = windowSize.width <= 600; const onSubmit = async (values: FormValues) => { if (!widget) { return; } try { setImageUploading(true); const s3Key = newImageFile ? await StorageService.uploadImage(newImageFile) : widget.content.s3Key.raw; const fileName = newImageFile ? newImageFile.name : widget.content.fileName; await BackendService.editWidget( dashboardId, widgetId, values.title, values.showTitle, { title: values.title, s3Key: { raw: s3Key, }, fileName: fileName, imageAltText: values.altText, summary: values.summary, summaryBelow: values.summaryBelow, scalePct: values.scalePct, }, widget.updatedAt, ); setImageUploading(false); history.push(`/admin/dashboard/edit/${dashboardId}`, { alert: { type: "success", message: t("EditImageScreen.ImageEditedSuccessfully", { title: values.title, }), }, }); } catch (err) { console.log(t("AddContentFailure"), err); setImageUploading(false); } }; const onCancel = () => { history.push(`/admin/dashboard/edit/${dashboardId}`); }; const handleChangeTitle = (event: React.FormEvent) => { if (widget) { setWidget({ ...widget, content: { ...widget.content, title: (event.target as HTMLInputElement).value, }, }); } }; const handleSummaryChange = (event: React.FormEvent) => { if (widget) { setWidget({ ...widget, content: { ...widget.content, summary: (event.target as HTMLTextAreaElement).value, }, }); } }; const handleSummaryBelowChange = (event: React.FormEvent) => { if (widget) { setWidget({ ...widget, content: { ...widget.content, summaryBelow: (event.target as HTMLInputElement).checked, }, }); } }; const handleShowTitleChange = (event: React.FormEvent) => { if (widget) { setWidget({ ...widget, showTitle: (event.target as HTMLInputElement).checked, content: { ...widget.content, }, }); } }; const handleImageScaleChange = (event: React.FormEvent) => { if (widget) { setWidget({ ...widget, content: { ...widget.content, scalePct: (event.target as HTMLInputElement).value, }, }); } }; const onFileProcessed = (data: File) => { if (data) { setNewImageFile(data); } }; const crumbs = [ { label: t("Dashboards"), url: "/admin/dashboards", }, { label: dashboard?.name, url: `/admin/dashboard/edit/${dashboardId}`, }, ]; useChangeBackgroundColor(); if (!loading) { crumbs.push({ label: t("EditImageScreen.EditImage"), url: "", }); } return ( <> {!widget || loading || loadingFile ? ( ) : ( <>
)} ); } export default EditImage;