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]));