From 33d1d72d5e7f2e8e7f846bbf8651d7f128765c64 Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Fri, 27 Oct 2023 16:09:31 +0200 Subject: New frontend project --- frontend/src/components/ElementList.tsx | 46 +++++++++++++++++++++++++++++++++ frontend/src/components/ElementView.tsx | 19 ++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 frontend/src/components/ElementList.tsx create mode 100644 frontend/src/components/ElementView.tsx (limited to 'frontend/src/components') diff --git a/frontend/src/components/ElementList.tsx b/frontend/src/components/ElementList.tsx new file mode 100644 index 0000000..20b27dd --- /dev/null +++ b/frontend/src/components/ElementList.tsx @@ -0,0 +1,46 @@ +import { FC, useEffect, useState } from 'react'; +import {Element} from '../types'; +import ElementView from './ElementView'; + +const ElementList: FC = () => { + const [elements, setElements] = useState([]); + + useEffect(() => { + const initialElements: number[] = [1, 2, 3, 4]; + + const elementStateString = localStorage.getItem('elementState'); + if (elementStateString != null) { + setElements(JSON.parse(elementStateString)); + return; + } + + const fetchElements = async () => { + const elems: Element[] = []; + + for (const elemId of initialElements) { + const response = await fetch(`/api/element/${elemId}`); + const elem: Element = await response.json(); + elems.push(elem); + } + + localStorage.setItem('elementState', JSON.stringify(elems)); + setElements(elems); + }; + + fetchElements().catch(console.error); + }, []); + + useEffect(() => { + if (elements.length != 0) { + localStorage.setItem('elementState', JSON.stringify(elements)); + } + }, [elements]) + + return ( + <> + {elements.map(elem => )} + + ); +}; + +export default ElementList; diff --git a/frontend/src/components/ElementView.tsx b/frontend/src/components/ElementView.tsx new file mode 100644 index 0000000..4f9d489 --- /dev/null +++ b/frontend/src/components/ElementView.tsx @@ -0,0 +1,19 @@ +import { FC } from 'react'; +import { Element } from '../types'; + +interface ElementViewProps { + element: Element; +} + +const ElementView: FC = ({ element }) => { + return ( +
+
+ +

{element.name}

+
+
+ ); +}; + +export default ElementView; -- cgit v1.2.3