From 6b8ebe3f35fbe22aea34124abfabcc60ce83a521 Mon Sep 17 00:00:00 2001 From: ntr Date: Fri, 16 Sep 2022 21:48:26 +1000 Subject: [PATCH] rn home offers --- client/src/routes/offers.tsx | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 client/src/routes/offers.tsx diff --git a/client/src/routes/offers.tsx b/client/src/routes/offers.tsx new file mode 100644 index 0000000..3c556ea --- /dev/null +++ b/client/src/routes/offers.tsx @@ -0,0 +1,41 @@ +import { h } from 'preact'; +import { useState, useEffect } from 'preact/hooks'; +import { route } from 'preact-router'; +import { useSelector, useDispatch } from 'react-redux' + +import http from './../http'; +import { State, offersState } from './../store'; + +import Offer from './../components/offer'; + +function Home() { + const dispatch = useDispatch() + const offers = useSelector((state: State) => state.offers); + + useEffect(() => { + async function getOffers() { + const [error, offers] = await http.getOffers(); + if (error) return console.error(error); + dispatch(offersState.actions.set(offers)); + if (offers.length === 0) route('/search'); + } + getOffers(); + }, []); + + if (!offers.length) { + return ( +
+ loading... +
+ ); + } + + + return ( +
+ {offers.map(Offer)} +
+ ); +} + +export default Home;