X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fcomponents%2Fepisodes%2FFilter.js;h=98d965dd3ff902751164140df20a79cc280b211d;hb=532dea7c62f3619a04cd8d7aa895f8db1f3f98d2;hp=b411f82193574c9036b3ff8d22bbe6d4b99a940e;hpb=c8bcea2a3093707b14fd655f3b284b2d245b483f;p=alttp.git diff --git a/resources/js/components/episodes/Filter.js b/resources/js/components/episodes/Filter.js index b411f82..98d965d 100644 --- a/resources/js/components/episodes/Filter.js +++ b/resources/js/components/episodes/Filter.js @@ -1,70 +1,34 @@ -import axios from 'axios'; -import moment from 'moment'; import PropTypes from 'prop-types'; import React from 'react'; import { Button } from 'react-bootstrap'; -const Filter = ({ filter, setFilter }) => { - const [events, setEvents] = React.useState([]); - - React.useEffect(() => { - const controller = new AbortController(); - axios.get(`/api/events`, { - signal: controller.signal, - params: { - after: moment().startOf('day').subtract(7, 'days').toISOString(), - before: moment().startOf('day').add(8, 'days').toISOString(), - }, - }).then(response => { - const newEvents = response.data || []; - setEvents(newEvents); - }).catch(e => { - if (!axios.isCancel(e)) { - console.error(e); - } - }); - return () => { - controller.abort(); - }; - }, []); +import { isEventSelected, toggleEventFilter } from '../../helpers/Episode'; +const Filter = ({ events, filter, setFilter }) => { const toggleEvent = React.useCallback(event => { - const eventFilter = filter.event || []; - if (eventFilter.includes(event.id)) { - setFilter({ - ...filter, - event: eventFilter.filter(id => id !== event.id && events.find(e => e.id === id)), - }); - } else { - setFilter({ - ...filter, - event: [...eventFilter, event.id], - }); - } + setFilter(toggleEventFilter(events, filter, event)); }, [events, filter, setFilter]); - const isEventSelected = React.useCallback(event => { - const eventFilter = filter.event || []; - return eventFilter.includes(event.id); - }, [filter]); - if (!events || !events.length) return null; - return