mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 23:12:13 +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 useLibrary = (urlParams) => {
|
||||||
const { core } = useServices();
|
const { core } = useServices();
|
||||||
const [library, setLibrary] = React.useState([[], null, null]);
|
const [library, setLibrary] = React.useState([[], null, null]);
|
||||||
const [type, setType] = React.useState(null);
|
|
||||||
React.useEffect(() => {
|
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 = () => {
|
const onNewState = () => {
|
||||||
updateType();
|
|
||||||
const state = core.getState();
|
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 = {
|
const selectInput = {
|
||||||
selected: [state.library.selected],
|
selected: [state.library.selected],
|
||||||
options: state.library.types
|
options: state.library.types
|
||||||
|
|
@ -38,21 +34,28 @@ const useLibrary = (urlParams) => {
|
||||||
setLibrary([state.library.items, selectInput, error]);
|
setLibrary([state.library.items, selectInput, error]);
|
||||||
};
|
};
|
||||||
core.on('NewModel', onNewState);
|
core.on('NewModel', onNewState);
|
||||||
return () => {
|
const state = core.getState();
|
||||||
core.off('NewModel', onNewState);
|
if (typeof urlParams.type === 'string') {
|
||||||
};
|
|
||||||
}, [urlParams]);
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (typeof type === 'string') {
|
|
||||||
core.dispatch({
|
core.dispatch({
|
||||||
action: 'Load',
|
action: 'Load',
|
||||||
args: {
|
args: {
|
||||||
load: 'LibItemsByType',
|
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;
|
return library;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue