Add schedule loader
This commit is contained in:
parent
e87e4b9502
commit
1b14b697c9
|
@ -1,11 +1,8 @@
|
|||
import './App.css';
|
||||
import ScheduleLoader from './ScheduleLoader.jsx';
|
||||
|
||||
function App() {
|
||||
|
||||
return (
|
||||
<>
|
||||
</>
|
||||
);
|
||||
return (<ScheduleLoader/>);
|
||||
}
|
||||
|
||||
export default App;
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import useConferences from './hooks/useConferences.js';
|
||||
|
||||
export default function ScheduleLoader() {
|
||||
const {
|
||||
conferences,
|
||||
error,
|
||||
isLoading,
|
||||
} = useConferences();
|
||||
|
||||
return (<>
|
||||
{isLoading && <p>Please wait...</p>}
|
||||
{error && <p>Error: {error}</p>}
|
||||
{conferences && <>
|
||||
<label>Select a conference</label>
|
||||
<select>
|
||||
{conferences.map(conference => <option key={conference.id}
|
||||
value={conference.id}>{conference.title}</option>)}
|
||||
</select>
|
||||
</>}
|
||||
</>);
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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,
|
||||
};
|
||||
}
|
|
@ -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]));
|
Loading…
Reference in New Issue