/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useEffect, useState } from "react"; import { useHistory, useParams } from "react-router-dom"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useDashboard, useChangeBackgroundColor } from "../hooks"; import Breadcrumbs from "../components/Breadcrumbs"; import Button from "../components/Button"; import PrimaryActionBar from "../components/PrimaryActionBar"; import RadioButtonsTile from "../components/RadioButtonsTile"; import Alert from "../components/Alert"; interface FormValues { widgetType: string; } interface PathParams { dashboardId: string; } function AddContent() { const { t } = useTranslation(); const history = useHistory(); const { dashboardId } = useParams(); const { dashboard, loading } = useDashboard(dashboardId); const { register, handleSubmit, watch } = useForm(); const [error, setError] = useState(""); const widgetType = watch("widgetType"); useEffect(() => { if (error && widgetType) { setError(""); } }, [widgetType, error]); const onSubmit = async (values: FormValues) => { if (values.widgetType === "chart") { history.push(`/admin/dashboard/${dashboardId}/add-chart`); } else if (values.widgetType === "table") { history.push(`/admin/dashboard/${dashboardId}/add-table`); } else if (values.widgetType === "text") { history.push(`/admin/dashboard/${dashboardId}/add-text`); } else if (values.widgetType === "image") { history.push(`/admin/dashboard/${dashboardId}/add-image`); } else if (values.widgetType === "metrics") { history.push(`/admin/dashboard/${dashboardId}/add-metrics`); } else if (values.widgetType === "section") { history.push(`/admin/dashboard/${dashboardId}/add-section`); } else { setError(t("AddContentScreen.InvalidWidgetType")); } }; 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("AddContentScreen.Title"), url: "", }); } return ( <>

{t("AddContentScreen.Title")}

{t("AddContentScreen.Instructions")} {error && }


{widgetType === "text" && ( {t("AddContentScreen.TextImageAlt")} )} {widgetType === "metrics" && ( {t("AddContentScreen.MetricsImageAlt")} )} {widgetType === "chart" && ( {t("AddContentScreen.ChartImageAlt")} )} {widgetType === "table" && ( {t("AddContentScreen.TableImageAlt")} )} {widgetType === "image" && ( {t("AddContentScreen.ImageAlt")} )} {widgetType === "section" && ( {t("AddContentScreen.SectionAlt")} )}
); } export default AddContent;