/* * 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 { useTopicAreas, useDashboard, useSettings, useChangeBackgroundColor, useFullPreview, useWindowSize, } from "../hooks"; import BackendService from "../services/BackendService"; import TextField from "../components/TextField"; import Dropdown from "../components/Dropdown"; import Button from "../components/Button"; import Breadcrumbs from "../components/Breadcrumbs"; import Spinner from "../components/Spinner"; import DashboardHeader from "../components/DashboardHeader"; import PrimaryActionBar from "../components/PrimaryActionBar"; import Link from "../components/Link"; import { useTranslation } from "react-i18next"; import Navigation from "../components/Navigation"; import AlertContainer from "./AlertContainer"; import { TopicAreaSortingCriteria } from "../models"; interface FormValues { name: string; topicAreaId: string; displayTableOfContents: boolean; description: string; } interface PathParams { dashboardId: string; } function EditDetails() { const history = useHistory(); const { settings } = useSettings(); const { topicareas } = useTopicAreas(); const { dashboardId } = useParams(); const { dashboard, loading } = useDashboard(dashboardId); const [activeWidgetId, setActiveWidgetId] = useState(""); const previewPanelId = "preview-details-panel"; const { fullPreview, fullPreviewButton } = useFullPreview(previewPanelId); const { register, errors, handleSubmit, watch, reset } = useForm(); const windowSize = useWindowSize(); const isMobile = windowSize.width <= 600; const sortedTopicAreas = [...topicareas].sort(TopicAreaSortingCriteria); const name = watch("name"); const description = watch("description"); const topicAreaId = watch("topicAreaId"); const displayTableOfContents = watch("displayTableOfContents"); useEffect(() => { if (dashboard) { const name = dashboard.name; const description = dashboard.description; const topicAreaId = dashboard.topicAreaId; const displayTableOfContents = dashboard.displayTableOfContents; reset({ name, description, topicAreaId, displayTableOfContents, }); } }, [dashboard, reset]); const { t } = useTranslation(); const onDisplayTableOfContentsChange = (value: boolean) => { if (!dashboard) { return; } }; const onSubmit = async (values: FormValues) => { await BackendService.editDashboard( dashboardId, values.name, values.topicAreaId, values.displayTableOfContents, values.description || "", dashboard ? dashboard.updatedAt : new Date(), {}, ); history.push(`/admin/dashboard/edit/${dashboardId}`, { alert: { type: "success", message: `"${values.name}" ${t("EditDetailsSuccess")}`, }, id: "top-alert", }); }; const getTopicAreaName = (topicAreaId: string) => { return topicareas.find((t) => t.id === topicAreaId)?.name || ""; }; const onCancel = () => { history.push(`/admin/dashboard/edit/${dashboardId}`); }; useChangeBackgroundColor(); if (loading || !dashboard || !topicareas || topicareas.length === 0) { return ; } return ( <>
{isMobile ?
: fullPreviewButton}

{ return { name: widget.name, id: widget.id, isInsideSection: !!widget.section, sectionWithTabs: "", }; })} activeWidgetId={activeWidgetId} isTop={false} displayTableOfContents={displayTableOfContents} onBottomOfThePage={() => {}} onClick={setActiveWidgetId} />
); } export default EditDetails;