doc: move Alan from root directory into components directory
This commit is contained in:
1 parent
4f30c1d434
commit
2ca1a190e7
1 file changed
@@ -0,0 +1,48 @@
|
||||
import { useEffect, useContext } from 'react';
|
||||
import alanBtn from '@alan-ai/alan-sdk-web';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
import { ColorModeContext } from '../utils/ToggleColorMode';
|
||||
import { fetchToken } from '../utils/index';
|
||||
import { selectGenreOrCategory, searchMovie } from '../features/currentGenreOrCategory';
|
||||
|
||||
function useAlan() {
|
||||
const { setMode } = useContext(ColorModeContext);
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
alanBtn({
|
||||
key: process.env.REACT_APP_ALAN_SDK_KEY,
|
||||
onCommand: ({ command, mode, genres, genreOrCategory, query }) => {
|
||||
if (command === 'chooseGenre') {
|
||||
const foundGenre = genres.find((g) => g.name.toLowerCase() === genreOrCategory.toLowerCase());
|
||||
if (foundGenre) {
|
||||
navigate('/');
|
||||
dispatch(selectGenreOrCategory(foundGenre.id));
|
||||
} else {
|
||||
const category = genreOrCategory.startsWith('top') ? 'top_rated' : genreOrCategory;
|
||||
navigate('/');
|
||||
dispatch(selectGenreOrCategory(category));
|
||||
}
|
||||
} else if (command === 'changeMode') {
|
||||
if (mode === 'light') {
|
||||
setMode('light');
|
||||
} else {
|
||||
setMode('dark');
|
||||
}
|
||||
} else if (command === 'login') {
|
||||
fetchToken();
|
||||
} else if (command === 'logout') {
|
||||
localStorage.clear();
|
||||
navigate('/');
|
||||
} else if (command === 'search') {
|
||||
dispatch(searchMovie(query));
|
||||
}
|
||||
},
|
||||
});
|
||||
}, []);
|
||||
}
|
||||
|
||||
export default useAlan;
|
||||
Reference in new issue
Block a user