42 lines
1007 B
TypeScript
42 lines
1007 B
TypeScript
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 (
|
|
<div class={'offers'}>
|
|
loading...
|
|
</div>
|
|
);
|
|
}
|
|
|
|
|
|
return (
|
|
<div class={'offers'}>
|
|
{offers.map(Offer)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default Home;
|