/* * 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 Breadcrumbs from "../components/Breadcrumbs"; import TextField from "../components/TextField"; import Button from "../components/Button"; import MarkdownRender from "../components/MarkdownRender"; import { useWidget, useDashboard, useFullPreview, useChangeBackgroundColor, useWindowSize, } from "../hooks"; import Spinner from "../components/Spinner"; 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; showTitle: boolean; summary: string; showWithTabs: boolean; horizontally: string; } interface PathParams { dashboardId: string; widgetId: string; } function EditSection() { const history = useHistory(); const { dashboardId, widgetId } = useParams(); const { dashboard, loading } = useDashboard(dashboardId); const { register, errors, handleSubmit, getValues } = useForm(); const { t } = useTranslation(); const [editingWidget, setEditingWidget] = useState(false); const { widget, setWidget } = useWidget(dashboardId, widgetId); const previewPanelId = "preview-section-panel"; const { fullPreview, fullPreviewButton } = useFullPreview(previewPanelId); const windowSize = useWindowSize(); const isMobile = windowSize.width <= 600; const onSubmit = async (values: FormValues) => { if (!widget) { return; } try { setEditingWidget(true); await BackendService.editWidget( dashboardId, widgetId, values.title, values.showTitle, { title: values.title, summary: values.summary, widgetIds: widget.content.widgetIds, showWithTabs: values.showWithTabs, horizontally: values.horizontally === "horizontally", }, widget.updatedAt, ); setEditingWidget(false); history.push(`/admin/dashboard/edit/${dashboardId}`, { alert: { type: "success", message: t("EditSectionScreen.EditSectionSuccess", { title: values.title, }), }, }); } catch (err) { console.log(t("AddContentFailure"), err); setEditingWidget(false); } }; const onCancel = () => { history.push(`/admin/dashboard/edit/${dashboardId}`); }; const onFormChange = () => { const { title, showTitle, summary, showWithTabs, horizontally } = getValues(); setWidget({ ...widget, name: title, showTitle: showTitle, content: { ...widget?.content, title, summary, showWithTabs, horizontally: horizontally === "horizontally", }, }); }; const crumbs = [ { label: t("Dashboards"), url: "/admin/dashboards", }, { label: dashboard?.name, url: `/admin/dashboard/edit/${dashboardId}`, }, ]; useChangeBackgroundColor(); if (!loading && widget) { crumbs.push({ label: t("EditSectionScreen.EditSection"), url: "", }); } return ( <> {loading || !widget || editingWidget ? ( ) : ( <>
{isMobile ?
: fullPreviewButton}
{widget.showTitle ? (

{widget.content.title}

) : ( "" )} {widget.content.summary ? (
) : ( "" )}
)} ); } export default EditSection;