Skip to content

Instantly share code, notes, and snippets.

@bikefighter
Created August 15, 2016 19:25
Show Gist options
  • Save bikefighter/97af6b77ea59d2a543cc2d6281d5d0b0 to your computer and use it in GitHub Desktop.
Save bikefighter/97af6b77ea59d2a543cc2d6281d5d0b0 to your computer and use it in GitHub Desktop.
sample React actions
import { path } from 'path';
import { glob } from 'glob';
import { push } from 'react-router-redux';
export const INCREMENT_CURSOR = 'INCREMENT_CURSOR';
export const DECREMENT_CURSOR = 'DECREMENT_CURSOR';
export const SET_PATH = 'SET_PATH';
export const ADD_ALBUM = 'ADD_ALBUM';
import fs from 'fs';
export function incrementCursor() {
return {
type: INCREMENT_CURSOR
}
}
export function decrementCursor() {
return {
type: DECREMENT_CURSOR
}
}
export function goHome() {
return dispatch => dispatch(push('/folder'))
}
export function goToPage(folderName, pageNum) {
return dispatch => {
const path = `/folder/${folderName}/${pageNum}`
dispatch(push(path))
}
}
export function addAlbums(dispatch, path, ignore = ['.DS_Store']) {
fs.readdir(path, function(err, files) {
const filteredFiles = files.filter(file => !ignore.includes(file))
filteredFiles.forEach(name => {
loadAlbum(dispatch, path, name)
})
})
}
export function setPath(path) {
return dispatch => {
dispatch(push('/folder'))
dispatch(addAlbums(dispatch, path))
return {
type: SET_PATH,
path
}
}
}
export function loadAlbum(dispatch, path, name) {
const albumPath = path + '/' + name;
// options is optional
const options = null;
glob(albumPath + "/*.@(jpg|png)", options, function (error, files) {
if (error) {
console.log(error);
} else {
dispatch(addAlbum(name, files));
}
})
}
export function addAlbum(name, images) {
return {
type: ADD_ALBUM,
album: {
name,
images
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment