mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
type checks for dispatch fixed
This commit is contained in:
parent
fb0ec7583a
commit
f70ca3d293
1 changed files with 25 additions and 22 deletions
|
|
@ -5,23 +5,19 @@ const { useServices } = require('stremio/services');
|
|||
const useLibrary = (urlParams) => {
|
||||
const { core } = useServices();
|
||||
const [library, setLibrary] = React.useState([[], null, null]);
|
||||
const [type, setType] = React.useState(null);
|
||||
React.useEffect(() => {
|
||||
const updateType = () => {
|
||||
const state = core.getState();
|
||||
setType(typeof urlParams.type === 'string' ?
|
||||
urlParams.type
|
||||
:
|
||||
state.library.types.length > 0 ?
|
||||
state.library.types[0]
|
||||
:
|
||||
''
|
||||
);
|
||||
};
|
||||
updateType();
|
||||
const onNewState = () => {
|
||||
updateType();
|
||||
const state = core.getState();
|
||||
if (state.library.selected === null && state.library.types.length > 0) {
|
||||
core.dispatch({
|
||||
action: 'Load',
|
||||
args: {
|
||||
load: 'LibItemsByType',
|
||||
args: state.library.types[0]
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
const selectInput = {
|
||||
selected: [state.library.selected],
|
||||
options: state.library.types
|
||||
|
|
@ -38,21 +34,28 @@ const useLibrary = (urlParams) => {
|
|||
setLibrary([state.library.items, selectInput, error]);
|
||||
};
|
||||
core.on('NewModel', onNewState);
|
||||
return () => {
|
||||
core.off('NewModel', onNewState);
|
||||
};
|
||||
}, [urlParams]);
|
||||
React.useEffect(() => {
|
||||
if (typeof type === 'string') {
|
||||
const state = core.getState();
|
||||
if (typeof urlParams.type === 'string') {
|
||||
core.dispatch({
|
||||
action: 'Load',
|
||||
args: {
|
||||
load: 'LibItemsByType',
|
||||
args: type
|
||||
args: urlParams.type
|
||||
}
|
||||
});
|
||||
} else if (state.library.types.length > 0) {
|
||||
core.dispatch({
|
||||
action: 'Load',
|
||||
args: {
|
||||
load: 'LibItemsByType',
|
||||
args: state.library.types[0]
|
||||
}
|
||||
});
|
||||
}
|
||||
}, [type]);
|
||||
return () => {
|
||||
core.off('NewModel', onNewState);
|
||||
};
|
||||
}, [urlParams]);
|
||||
return library;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue