canape/src/public/js/routes.js

215 lines
6.4 KiB
JavaScript

import React from 'react'
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 UserEdit from './components/users/edit'
import UserSignUp from './components/users/signup'
import TorrentList from './components/torrents/list'
import { fetchTorrents } from './actions/torrents'
import { userLogout, getUserInfos } from './actions/users'
import { fetchMovies, getMovieExploreOptions } from './actions/movies'
import { fetchShows, fetchShowDetails, getShowExploreOptions } from './actions/shows'
import store from './store'
// This function returns true if the user is logged in, false otherwise
function isLoggedIn() {
const state = store.getState();
const isLogged = state.userStore.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
}
var pollingTorrentsId;
const loginCheck = function(nextState, replace, next, f = null) {
const loggedIn = isLoggedIn();
if (!loggedIn) {
replace('/users/login');
} else {
if (f) {
f();
}
// Poll torrents once logged
if (!pollingTorrentsId) {
// Fetch the torrents every 10s
pollingTorrentsId = setInterval(function() {
store.dispatch(fetchTorrents());
}, 10000);
}
}
next();
}
const defaultRoute = '/movies/explore/yts/seeds';
export default function getRoutes(App) {
return {
path: '/',
component: App,
indexRoute: {onEnter: ({params}, replace) => replace(defaultRoute)},
childRoutes: [
{
path: '/users/signup',
component: UserSignUp
},
{
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/edit',
component: UserEdit,
onEnter: function(nextState, replace, next) {
loginCheck(nextState, replace, next, function() {
store.dispatch(getUserInfos());
});
},
},
{
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',
component: TorrentList,
onEnter: function(nextState, replace, next) {
loginCheck(nextState, replace, next, function() {
store.dispatch(fetchTorrents());
});
},
},
],
};
};