From 1b14b697c9f30dff79ec5e740d6273067c00939e Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Wed, 18 Sep 2024 21:24:35 +0300 Subject: [PATCH] Add schedule loader --- .env | 1 + src/App.jsx | 7 ++----- src/ScheduleLoader.jsx | 21 +++++++++++++++++++++ src/hooks/useCfpRequest.js | 7 +++++++ src/hooks/useConferences.js | 17 +++++++++++++++++ src/main.jsx | 6 +++--- src/utils.js | 10 ++++++++++ 7 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 .env create mode 100644 src/ScheduleLoader.jsx create mode 100644 src/hooks/useCfpRequest.js create mode 100644 src/hooks/useConferences.js create mode 100644 src/utils.js diff --git a/.env b/.env new file mode 100644 index 0000000..6a20770 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +VITE_CFP_BASE_URL=http://cfp.localhost/api/conferences/ diff --git a/src/App.jsx b/src/App.jsx index 8c1cd86..90deecb 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,11 +1,8 @@ import './App.css'; +import ScheduleLoader from './ScheduleLoader.jsx'; function App() { - - return ( - <> - - ); + return (); } export default App; diff --git a/src/ScheduleLoader.jsx b/src/ScheduleLoader.jsx new file mode 100644 index 0000000..8737fea --- /dev/null +++ b/src/ScheduleLoader.jsx @@ -0,0 +1,21 @@ +import useConferences from './hooks/useConferences.js'; + +export default function ScheduleLoader() { + const { + conferences, + error, + isLoading, + } = useConferences(); + + return (<> + {isLoading &&

Please wait...

} + {error &&

Error: {error}

} + {conferences && <> + + + } + ); +} diff --git a/src/hooks/useCfpRequest.js b/src/hooks/useCfpRequest.js new file mode 100644 index 0000000..a2945a8 --- /dev/null +++ b/src/hooks/useCfpRequest.js @@ -0,0 +1,7 @@ +import useSWR from 'swr'; + +const fetcher = (...args) => fetch(...args).then(res => res.json()); + +export default function useCfpRequest(path = '') { + return useSWR(import.meta.env.VITE_CFP_BASE_URL.concat(path), fetcher); +} diff --git a/src/hooks/useConferences.js b/src/hooks/useConferences.js new file mode 100644 index 0000000..676a099 --- /dev/null +++ b/src/hooks/useConferences.js @@ -0,0 +1,17 @@ +import useCfpRequest from './useCfpRequest.js'; +import { useMemo } from 'react'; +import { dateSorter } from '../utils.js'; + +export default function useConferences() { + const { + data, + ...rest + } = useCfpRequest(); + + const conferences = useMemo(() => Array.isArray(data) ? data.sort(dateSorter('start_date')) : data, [data]); + + return { + conferences, + ...rest, + }; +} diff --git a/src/main.jsx b/src/main.jsx index 833c97f..c40899d 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -4,7 +4,7 @@ import App from './App.jsx'; import './index.css'; createRoot(document.getElementById('root')).render( - - - , + + + , ); diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..c384d26 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,10 @@ +function sorter(a, b, fieldFn) { + const fieldA = fieldFn(a); + const fieldB = fieldFn(b); + + return fieldA === fieldB ? 0 : ( + fieldA < fieldB ? -1 : 1 + ); +} + +export const dateSorter = key => (a, b) => sorter(a, b, item => Date.parse(item[key]));