|
@@ -1,15 +1,16 @@
|
|
|
-import {useRef} from 'react';
|
|
|
|
|
|
|
+import { useRoute } from 'react-router5'
|
|
|
|
|
+import { useRef } from 'react';
|
|
|
import AsyncSelect from 'react-select/async';
|
|
import AsyncSelect from 'react-select/async';
|
|
|
import {getCategory, request} from './Api';
|
|
import {getCategory, request} from './Api';
|
|
|
import './Filters.css';
|
|
import './Filters.css';
|
|
|
|
|
|
|
|
-const CATEGORIES = [{cat: 'artist', title: 'Artists'},
|
|
|
|
|
- {cat: 'year', title: 'Years'},
|
|
|
|
|
- {cat: 'genre', title: 'Genres'},
|
|
|
|
|
- {cat: 'publisher', title: 'Labels'},
|
|
|
|
|
- {cat: 'country', title: 'Countries'},
|
|
|
|
|
- {cat: 'type', title: 'Album types'},
|
|
|
|
|
- {cat: 'status', title: 'Album statuses'}];
|
|
|
|
|
|
|
+export const CATEGORIES = [{cat: 'artist', title: 'Artists'},
|
|
|
|
|
+ {cat: 'year', title: 'Years'},
|
|
|
|
|
+ {cat: 'genre', title: 'Genres'},
|
|
|
|
|
+ {cat: 'publisher', title: 'Labels'},
|
|
|
|
|
+ {cat: 'country', title: 'Countries'},
|
|
|
|
|
+ {cat: 'type', title: 'Album types'},
|
|
|
|
|
+ {cat: 'status', title: 'Album statuses'}];
|
|
|
|
|
|
|
|
function handleLoadOptions(cat, q) {
|
|
function handleLoadOptions(cat, q) {
|
|
|
const pageSize = 10000
|
|
const pageSize = 10000
|
|
@@ -20,18 +21,20 @@ function handleLoadOptions(cat, q) {
|
|
|
return request(url, options)
|
|
return request(url, options)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function handleFilterChange(e, timeout, dispatch) {
|
|
|
|
|
- if (timeout.current) {
|
|
|
|
|
- clearInterval(timeout.current);
|
|
|
|
|
- }
|
|
|
|
|
- const filter = e.target.value;
|
|
|
|
|
- timeout.current = setTimeout(() => {
|
|
|
|
|
- dispatch({type: 'SET_FILTER', payload: filter});
|
|
|
|
|
- }, 500);
|
|
|
|
|
|
|
+function handleCategoryChange(router, cat, value) {
|
|
|
|
|
+ const params = router.getState().params;
|
|
|
|
|
+ router.navigate('browser', {...params, [cat]:(value || null)});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export default function Filters({categories, filter, latest, dispatch}) {
|
|
|
|
|
|
|
+function handleFilterChange(router, timeout, value) {
|
|
|
|
|
+ if (timeout.current) clearInterval(timeout.current);
|
|
|
|
|
+ timeout.current = setTimeout(() => handleCategoryChange(router, 'filter', value), 500);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export default function Filters() {
|
|
|
const timeout = useRef();
|
|
const timeout = useRef();
|
|
|
|
|
+ const { router } = useRoute();
|
|
|
|
|
+ const { params } = router.getState();
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<div className="Filters">
|
|
<div className="Filters">
|
|
@@ -41,11 +44,12 @@ export default function Filters({categories, filter, latest, dispatch}) {
|
|
|
<AsyncSelect
|
|
<AsyncSelect
|
|
|
isClearable
|
|
isClearable
|
|
|
cacheOptions
|
|
cacheOptions
|
|
|
|
|
+ defaultValue={params[cat] && {item: params[cat]}}
|
|
|
defaultOptions
|
|
defaultOptions
|
|
|
loadOptions={(q) => handleLoadOptions(cat, q)}
|
|
loadOptions={(q) => handleLoadOptions(cat, q)}
|
|
|
- getOptionLabel={(i)=>`${i.item} (${i.count})`}
|
|
|
|
|
|
|
+ getOptionLabel={(i)=>i.count ? `${i.item} (${i.count})` : i.item}
|
|
|
getOptionValue={(i)=>i.item}
|
|
getOptionValue={(i)=>i.item}
|
|
|
- onChange={(e)=>dispatch({type: 'SET_CATEGORY', payload: {cat: cat, value: (e && e.item)}})}
|
|
|
|
|
|
|
+ onChange={(e)=>handleCategoryChange(router, cat, (e && e.item))}
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
))}
|
|
))}
|
|
@@ -53,15 +57,16 @@ export default function Filters({categories, filter, latest, dispatch}) {
|
|
|
<h3>Latest</h3>
|
|
<h3>Latest</h3>
|
|
|
<input className="Latest"
|
|
<input className="Latest"
|
|
|
type="checkbox"
|
|
type="checkbox"
|
|
|
- checked={latest}
|
|
|
|
|
- onChange={(e) => dispatch({type: 'SET_LATEST', payload: e.target.checked})} />
|
|
|
|
|
|
|
+ checked={ !params.alpha }
|
|
|
|
|
+ onChange={(e) => handleCategoryChange(router, 'alpha', !e.target.checked)} />
|
|
|
</div>
|
|
</div>
|
|
|
<div>
|
|
<div>
|
|
|
<h3>Search</h3>
|
|
<h3>Search</h3>
|
|
|
<input className="Filter"
|
|
<input className="Filter"
|
|
|
type="text"
|
|
type="text"
|
|
|
placeholder="Search albums"
|
|
placeholder="Search albums"
|
|
|
- onChange={(e)=>handleFilterChange(e, timeout, dispatch)} />
|
|
|
|
|
|
|
+ defaultValue={params.filter}
|
|
|
|
|
+ onChange={(e)=>handleFilterChange(router, timeout, e.target.value)} />
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
);
|
|
);
|