import React, { FunctionComponent, useState } from "react"; import { createRoot } from 'react-dom/client'; /** * Declaring globally what properties this page should inherited from the server under "HomePageProps". */ declare global { type HomePageProps = { maps: MapDoc[], userData?: { userId: number firstName: string, lastName: string, image: string } | undefined } } type Props = { ServerProps: ServerPropsType } /** * A React page that will render the homepage. This will link to all the map editors. * * @param maps The configuration of the maps to render all the options. */ const Home: FunctionComponent = (props) => { /** * Making sure we inherited the properties from the server. */ const pageProps = props.ServerProps.homePageProps; if(!pageProps) return <>; /** * State variable so that if a user deletes a map, the UI updates. */ const [maps, setMaps] = useState(pageProps.maps); /** * An event handler to delete the map at the current index. * @param index The index of the map to be deleted. */ async function deleteMap(index:number) { /** * Making a fetch call to the server to delete the map */ const response = await (await fetch(`/map/${maps[index].id}`, { method: 'DELETE', headers: { "Accept": "application/json", "Content-Type": "application/json" } })).json(); // Removing the map from the array on success. if(response.success) { const newMaps = [...maps]; newMaps.splice(index, 1); setMaps(newMaps); } } return <>

Materials Matter Prototype

{pageProps.userData ?
Logout {`${pageProps.userData.firstName}

{pageProps.userData.firstName} {pageProps.userData.lastName}

:
Login
}

Maps:

+ New Map
{maps.map((map, i) => { return
{map.name} {pageProps.userData ? deleteMap(i)}> : <>}
})}
} /** * Rendering our react element to the root element. */ const root = createRoot(document.getElementById('root') as HTMLDivElement); root.render( ); export default Home;