/* * 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 { WidgetType } from "../models"; import BackendService from "../services/BackendService"; import { useDashboard, useFullPreview, useChangeBackgroundColor, useWindowSize } from "../hooks"; 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 ImageWidget from "../components/ImageWidget"; import Link from "../components/Link"; import Alert from "../components/Alert"; import PrimaryActionBar from "../components/PrimaryActionBar"; import RadioButtons from "../components/RadioButtons"; import { useTranslation } from "react-i18next"; interface FormValues { title: string; summary: string; altText: string; showTitle: boolean; summaryBelow: boolean; scalePct: string; imageFile: FileList; } interface PathParams { dashboardId: string; } function AddImage() { const history = useHistory(); const { t } = useTranslation(); const { dashboardId } = useParams(); const { dashboard, loading } = useDashboard(dashboardId); const { register, errors, handleSubmit, watch } = useForm(); const title = watch("title"); const summary = watch("summary"); const altText = watch("altText"); const showTitle = watch("showTitle"); const summaryBelow = watch("summaryBelow"); const scalePct = watch("scalePct"); const imageFile = watch("imageFile"); 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) => { try { if (!values.imageFile || !values.imageFile[0]) { throw new Error(t("AddImageScreen.ImageFileNotSpecified")); } setImageUploading(true); const s3Key = await StorageService.uploadImage(values.imageFile[0]); await BackendService.createWidget( dashboardId, values.title, WidgetType.Image, values.showTitle, { title: values.title, s3Key: { raw: s3Key, }, fileName: values.imageFile[0].name, imageAltText: values.altText, summary: values.summary, summaryBelow: values.summaryBelow, scalePct: values.scalePct, }, ); setImageUploading(false); history.push(`/admin/dashboard/edit/${dashboardId}`, { alert: { type: "success", message: t("AddImageScreen.ImageAddedSuccessfully", { title: values.title, }), }, }); } catch (err) { console.log(t("AddContentFailure"), err); setImageUploading(false); } }; const goBack = () => { history.push(`/admin/dashboard/${dashboardId}/add-content`); }; const onCancel = () => { history.push(`/admin/dashboard/edit/${dashboardId}`); }; const crumbs = [ { label: t("Dashboards"), url: "/admin/dashboards", }, { label: dashboard?.name, url: `/admin/dashboard/edit/${dashboardId}`, }, ]; useChangeBackgroundColor(); if (!loading) { crumbs.push({ label: t("AddImageScreen.AddImage"), url: "", }); } return ( <>
{isMobile ?
: fullPreviewButton}
); } export default AddImage;