get selected type from state

This commit is contained in:
svetlagasheva 2019-11-15 15:38:16 +02:00
parent 6be7f61866
commit f0078427fa

View file

@ -1,6 +1,6 @@
const React = require('react');
const { useServices } = require('stremio/services');
const DEFAULT_TYPE = 'movie';
const DEFAULT_SORT = 'recent';
const SORTS = [DEFAULT_SORT, 'year', 'a-z'];
const SORT_PROPS = new Map([
@ -10,9 +10,9 @@ const SORT_PROPS = new Map([
]);
const useSort = (urlParams, queryParams) => {
const { core } = useServices();
const [sort, setSort] = React.useState([null, () => { }]);
React.useEffect(() => {
const type = typeof type === 'string' && type.length > 0 ? urlParams.type : DEFAULT_TYPE;
const sort = queryParams.has('sort') && SORTS.includes(queryParams.get('sort')) ? queryParams.get('sort') : DEFAULT_SORT;
const sortProp = SORT_PROPS.get(sort);
const sortItems = (a, b) => {
@ -25,7 +25,8 @@ const useSort = (urlParams, queryParams) => {
options: [{ label: 'Recent', value: 'recent' }, { label: 'A-Z', value: 'a-z' }, { label: 'Year', value: 'year' }],
onSelect: (event) => {
const nextQuery = new URLSearchParams({ sort: event.value });
window.location.replace(`#/library/${type}?${nextQuery}`);
const state = core.getState();
window.location.replace(`#/library/${state.library.selected !== null ? state.library.selected : ''}?${nextQuery}`);
}
};
setSort([selectInput, sortItems]);