// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { I18n, Logger } from "@aws-amplify/core"; import { useState, useEffect } from 'react'; import { useHistory } from 'react-router'; import { API } from '@aws-amplify/api'; import { API_NAME, validateField } from '../util/Utils'; import { IErrors, IPageProps, ISimulation, IDevice, simTypes } from '../components/Shared/Interfaces' import PageTitleBar from '../components/Shared/PageTitleBar'; import Footer from '../components/Shared/Footer'; import Form from 'react-bootstrap/Form'; import Card from 'react-bootstrap/Card'; import Row from 'react-bootstrap/Row'; import Button from 'react-bootstrap/Button'; import DeviceFields from '../components/SimulationCreate/DeviceFields'; /** * Renders The simulation Creation Form * @returns The simulation creation form */ export default function SimulationCreate(props: IPageProps): JSX.Element { const history = useHistory(); const logger = new Logger("Simulation Create"); const [simType, setSimType] = useState("Custom"); const [errs, setErrs] = useState>({}); const [deviceErrs, setDeviceErrs] = useState>>([]); const [simulation, setSimulation] = useState({ simId: "", name: "", stage: "", duration: 1, interval: 1, devices: [{ typeId: "", name: "", amount: 1 }] }); const [showValidation, setShowValidation] = useState([]); const [showDeviceValidation, setShowDeviceValidation] = useState([]); /** * React useEffect hook * run on simulation and deviceErrs changes * Checks for errors and updates accordingly */ useEffect(() => { let newErrs: IErrors = {}; Object.entries(simulation).forEach((entry) => { const key = entry[0]; const value = entry[1]; if (key === "name" || key === "interval" || key === "duration") { let error: IErrors = validateField(key, value); newErrs = { ...newErrs, ...error } } else if (key === "devices") { deviceErrs.forEach((device) => { if (Object.keys(device).length > 0) { let error = { devices: "error" } newErrs = { ...newErrs, ...error } } }) } }) if (!newErrs.interval && !newErrs.duration) { if (simulation.interval >= simulation.duration) newErrs = { ...newErrs, interval: `${I18n.get("interval.error")}` }; } setErrs({ ...newErrs }); }, [simulation, deviceErrs]) /** * updates dynamodb on form submission */ const handleSubmit = async (event: any) => { const form = event.currentTarget; event.preventDefault(); form.checkValidity(); if (Object.keys(errs).length > 0) { return; } try { await API.post(API_NAME, '/simulation', { body: simulation }); history.push('../'); } catch (err) { logger.error(I18n.get("simulation.create.error"), err); throw err; } } /** * Updates simulation parameters on form changes * @param event */ const handleFormChange = (event: any) => { let value = event.target.valueAsNumber || event.target.value; switch (event.target.id) { case "name": simulation.name = value; break; case "interval": simulation.interval = value; break; case "duration": simulation.duration = value; break; case "type": const type = event.target.value; if (type === simTypes.autoDemo) { simulation.simId = simTypes.autoDemo; } else { simulation.simId = ""; } let devices = [{ typeId: "", name: "", amount: 1 }]; simulation.devices = devices; setSimType(type); setShowDeviceValidation([]); break; default: return; } setSimulation({ ...simulation }); } /** * Shows form validation when a field is focused * @param event */ const handleFieldFocus = (event: any) => { if(!showValidation.includes(event.target.id)) { showValidation.push(event.target.id); setShowValidation([...showValidation]); } } return (
{I18n.get("create.simulation")} {I18n.get("create.simulation.description")}
handleSubmit(event)} > {I18n.get("simulation.name")} { handleFormChange(event) }} onFocus={(event: any) => handleFieldFocus(event)} isInvalid={!!errs.name && showValidation.includes('name')} isValid={!errs.name} value={simulation.name} maxLength={30} > {errs.name} {I18n.get("simulation.type")} {handleFormChange(event)}} onFocus={(event: any) => handleFieldFocus(event)} isInvalid={!simType && showValidation.includes('type')} isValid={!!simType && showValidation.includes('type')} > {I18n.get("simulation.type.description")} {I18n.get("interval")} handleFormChange(event)} onFocus={(event: any) => handleFieldFocus(event)} isInvalid={!!errs.interval && showValidation.includes('interval')} isValid={!errs.interval && showValidation.includes('interval')} value={simulation.interval} min={1} > {errs.interval} {I18n.get("interval.description")} {I18n.get("duration")} handleFormChange(event)} onFocus={(event: any) => handleFieldFocus(event)} isInvalid={!!errs.duration && showValidation.includes('duration')} isValid={!errs.duration && showValidation.includes('duration')} value={simulation.duration} min={1} max={604800} > {errs.duration} {I18n.get("duration.description")}
) }