Skip to content

Instantly share code, notes, and snippets.

@pekkis
Created September 22, 2015 09:23
Show Gist options
  • Save pekkis/3f0d911dce690da446b3 to your computer and use it in GitHub Desktop.
Save pekkis/3f0d911dce690da446b3 to your computer and use it in GitHub Desktop.
import { createAction } from 'redux-actions';
import api from '../api';
import { Seq, Range, List, Set } from 'immutable';
import { fetchPersons } from './PersonActions';
export const RECEIVE_MOVIES = 'MOVIE_RECEIVE_MOVIES';
export const SET_CURRENT_MOVIE = 'MOVIE_SET_CURRENT_MOVIE';
export const receiveMovies = createAction(
RECEIVE_MOVIES
);
export function setCurrentMovie(movieId) {
movieId = 'Movies/' + movieId;
return function(dispatch, getState) {
dispatch(fetchMovies([movieId]));
dispatch(createAction(SET_CURRENT_MOVIE, movieId => movieId)(movieId));
};
};
export function fetchMovies(movies) {
return function(dispatch, getState) {
const loadedMovies = getState().movie.movies.map(m => m._id).toSet();
const moviesToLoad = Set(movies).subtract(loadedMovies);
if (moviesToLoad.count() === 0) {
return;
}
const numberOfSlices = Math.ceil(moviesToLoad.count() / 100);
Range(0, numberOfSlices).forEach(slice => {
const theSlice = moviesToLoad.slice(slice * 100, slice * 100 + 100);
api.fetchMovies(theSlice).then(movies => {
dispatch(receiveMovies(movies));
});
});
};
};
export function importMovies(data) {
return function(dispatch, getState) {
const token = getState().user.token;
api.importMovies(token, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment