/* * 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"; interface FormValues { title: string; text: string; showTitle: boolean; } interface PathParams { dashboardId: string; widgetId: string; } function EditText() { 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-text-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, { text: values.text, }, widget.updatedAt, ); setEditingWidget(false); history.push(`/admin/dashboard/edit/${dashboardId}`, { alert: { type: "success", message: t("EditTextScreen.EditTextSuccess", { title: values.title, }), }, }); } catch (err) { console.log(t("AddContentFailure"), err); setEditingWidget(false); } }; const onCancel = () => { history.push(`/admin/dashboard/edit/${dashboardId}`); }; const onFormChange = () => { const { title, text, showTitle } = getValues(); setWidget({ ...widget, name: title, showTitle: showTitle, content: { ...widget?.content, text, }, }); }; const crumbs = [ { label: t("Dashboards"), url: "/admin/dashboards", }, { label: dashboard?.name, url: `/admin/dashboard/edit/${dashboardId}`, }, ]; useChangeBackgroundColor(); if (!loading && widget) { crumbs.push({ label: t("EditTextScreen.EditText"), url: "", }); } return ( <> {loading || !widget || editingWidget ? ( ) : ( <>
{isMobile ?
: fullPreviewButton} {widget.showTitle ? (

{widget.name}

) : ( "" )}
{widget.content.text ? (
) : ( "" )}
)} ); } export default EditText;