import MovieList from "./components/movies/list" import ShowList from "./components/shows/list" import ShowDetails from "./components/shows/details" import UserLoginForm from "./components/users/login" import UserProfile from "./components/users/profile" import UserTokens from "./components/users/tokens" import UserActivation from "./components/users/activation" import UserSignUp from "./components/users/signup" import TorrentList from "./components/torrents/list" import TorrentSearch from "./components/torrents/search" import AdminPanel from "./components/admins/panel" import { fetchTorrents, searchTorrents } from "./actions/torrents" import { userLogout, getUserInfos, getUserTokens, getUserModules } from "./actions/users" import { fetchMovies, getMovieExploreOptions } from "./actions/movies" import { fetchShows, fetchShowDetails, getShowExploreOptions } from "./actions/shows" import { getUsers, getStats, getAdminModules } from "./actions/admins" import store from "./store" // Default route const defaultRoute = "/movies/explore/yts/seeds"; // This function returns true if the user is logged in, false otherwise function isLoggedIn() { const state = store.getState(); const isLogged = state.userStore.get("isLogged"); let token = localStorage.getItem("token"); // Let's check if the user has a token, if he does let's assume he's logged // in. If that's not the case he will be logged out on the fisrt query if (token && token !== "") { store.dispatch({ type: "USER_SET_TOKEN", payload: { token: token, }, }); } if (isLogged || (token && token !== "")) { return true } return false } function isActivated() { const state = store.getState(); return state.userStore.get("isActivated"); } var pollingTorrentsId; const loginCheck = function(nextState, replace, next, f = null) { const loggedIn = isLoggedIn(); if (!loggedIn) { replace("/users/login"); } else if (!isActivated()) { replace("/users/activation"); } else { if (f) { f(); } // Poll torrents once logged if (!pollingTorrentsId) { // Fetch the torrents every 10s pollingTorrentsId = setInterval(function() { store.dispatch(fetchTorrents()); }, 10000); } } next(); } const adminCheck = function(nextState, replace, next, f = null) { const state = store.getState(); const isAdmin = state.userStore.get("isAdmin"); loginCheck(nextState, replace, next, function() { if (!isAdmin) { replace(defaultRoute); } if (f) { f(); } next(); }) } export default function getRoutes(App) { return { path: "/", component: App, indexRoute: {onEnter: ({params}, replace) => replace(defaultRoute)}, childRoutes: [ { path: "/users/signup", component: UserSignUp, onEnter: function(nextState, replace, next) { if (isLoggedIn()) { // User is already logged in, redirect him to the default route replace(defaultRoute); } next(); }, }, { path: "/users/login", component: UserLoginForm, onEnter: function(nextState, replace, next) { if (isLoggedIn()) { // User is already logged in, redirect him to the default route replace(defaultRoute); } next(); }, }, { path: "/users/profile", component: UserProfile, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(getUserInfos()); store.dispatch(getUserModules()); }); }, }, { path: "/users/tokens", component: UserTokens, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(getUserTokens()); }); }, }, { path: "/users/activation", component: UserActivation, onEnter: function(nextState, replace, next) { if (!isLoggedIn()) { replace("/users/login"); } if (isActivated()) { // User is already activated, redirect him to the default route replace(defaultRoute); } next(); }, }, { path: "/users/logout", onEnter: function(nextState, replace, next) { // Stop polling if (pollingTorrentsId !== null) { clearInterval(pollingTorrentsId); pollingTorrentsId = null; } store.dispatch(userLogout()); replace("/users/login"); next(); }, }, { path: "/movies/search/:search", component: MovieList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchMovies(`/movies/search/${nextState.params.search}`)); }); }, }, { path: "/movies/polochon", component: MovieList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchMovies("/movies/polochon")); }); }, }, { path: "/movies/explore/:source/:category", component: MovieList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { var state = store.getState(); // Fetch the explore options if (state.movieStore.get("exploreOptions").size === 0) { store.dispatch(getMovieExploreOptions()); } store.dispatch(fetchMovies( `/movies/explore?source=${encodeURI(nextState.params.source)}&category=${encodeURI(nextState.params.category)}` )); }); }, }, { path: "/movies/wishlist", component: MovieList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchMovies("/wishlist/movies")); }); }, }, { path: "/shows/search/:search", component: ShowList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchShows(`/shows/search/${nextState.params.search}`)); }); }, }, { path: "/shows/polochon", component: ShowList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchShows("/shows/polochon")); }); }, }, { path: "/shows/wishlist", component: ShowList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchShows("/wishlist/shows")); }); }, }, { path: "/shows/details/:imdbId", component: ShowDetails, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchShowDetails(nextState.params.imdbId)); }); }, }, { path: "/shows/explore/:source/:category", component: ShowList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { var state = store.getState(); // Fetch the explore options if (state.showsStore.get("exploreOptions").size === 0) { store.dispatch(getShowExploreOptions()); } store.dispatch(fetchShows( `/shows/explore?source=${encodeURI(nextState.params.source)}&category=${encodeURI(nextState.params.category)}` )); }); }, }, { path: "/torrents/list", component: TorrentList, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(fetchTorrents()); }); }, }, { path: "/torrents/search", component: TorrentSearch, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next); }, }, { path: "/torrents/search/:type/:search", component: TorrentSearch, onEnter: function(nextState, replace, next) { loginCheck(nextState, replace, next, function() { store.dispatch(searchTorrents(`/torrents/search/${nextState.params.type}/${encodeURI(nextState.params.search)}`)); }); }, }, { path: "/admin", component: AdminPanel, onEnter: function(nextState, replace, next) { adminCheck(nextState, replace, next, function() { store.dispatch(getUsers()); store.dispatch(getStats()); store.dispatch(getAdminModules()); }); }, }, ], }; };