refactor: Creating Medias reducer
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import Immutable from 'immutable';
|
||||
import { AUTH_REQUEST, AUTH_SUCCESS, AUTH_FAILURE } from '../actions/auth';
|
||||
|
||||
export function auth(state = null, action) {
|
||||
const auth = (state = null, action) => {
|
||||
switch (action.type) {
|
||||
case AUTH_REQUEST:
|
||||
return Immutable.Map({isFetching: true});
|
||||
@ -13,4 +13,6 @@ export function auth(state = null, action) {
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default auth;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { OrderedMap, fromJS } from 'immutable';
|
||||
import { CONFIG_SUCCESS } from '../actions/config';
|
||||
|
||||
export function collections(state = null, action) {
|
||||
const collections = (state = null, action) => {
|
||||
switch (action.type) {
|
||||
case CONFIG_SUCCESS:
|
||||
const collections = action.payload && action.payload.collections;
|
||||
@ -14,3 +14,5 @@ export function collections(state = null, action) {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
export default collections;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import Immutable from 'immutable';
|
||||
import { CONFIG_REQUEST, CONFIG_SUCCESS, CONFIG_FAILURE } from '../actions/config';
|
||||
|
||||
export function config(state = null, action) {
|
||||
const config = (state = null, action) => {
|
||||
switch (action.type) {
|
||||
case CONFIG_REQUEST:
|
||||
return Immutable.Map({isFetching: true});
|
||||
@ -12,4 +12,6 @@ export function config(state = null, action) {
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
@ -3,7 +3,7 @@ import {
|
||||
ENTRY_REQUEST, ENTRY_SUCCESS, ENTRIES_REQUEST, ENTRIES_SUCCESS
|
||||
} from '../actions/entries';
|
||||
|
||||
export function entries(state = Map({entities: Map(), pages: Map()}), action) {
|
||||
const entries = (state = Map({entities: Map(), pages: Map()}), action) => {
|
||||
switch (action.type) {
|
||||
case ENTRY_REQUEST:
|
||||
return state.setIn(['entities', `${action.payload.collection}.${action.payload.slug}`, 'isFetching'], true);
|
||||
@ -28,13 +28,15 @@ export function entries(state = Map({entities: Map(), pages: Map()}), action) {
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export function selectEntry(state, collection, slug) {
|
||||
return state.entries.getIn(['entities', `${collection}.${slug}`]);
|
||||
}
|
||||
export const selectEntry = (state, collection, slug) => (
|
||||
state.getIn(['entities', `${collection}.${slug}`])
|
||||
);
|
||||
|
||||
export function selectEntries(state, collection) {
|
||||
const slugs = state.entries.getIn(['pages', collection, 'ids']);
|
||||
export const selectEntries = (state, collection) => {
|
||||
const slugs = state.getIn(['pages', collection, 'ids']);
|
||||
return slugs && slugs.map((slug) => selectEntry(state, collection, slug));
|
||||
}
|
||||
};
|
||||
|
||||
export default entries;
|
||||
|
@ -1,9 +1,10 @@
|
||||
import { Map, List } from 'immutable';
|
||||
import { DRAFT_CREATE, DRAFT_DISCARD, DRAFT_CHANGE, DRAFT_ADD_MEDIA, DRAFT_REMOVE_MEDIA } from '../actions/entries';
|
||||
import { DRAFT_CREATE, DRAFT_DISCARD, DRAFT_CHANGE } from '../actions/entries';
|
||||
import { ADD_MEDIA } from '../actions/media';
|
||||
|
||||
const initialState = Map({entry: Map(), mediaFiles: List()});
|
||||
|
||||
export function entryDraft(state = Map(), action) {
|
||||
const entryDraft = (state = Map(), action) => {
|
||||
switch (action.type) {
|
||||
case DRAFT_CREATE:
|
||||
if (!action.payload) {
|
||||
@ -20,15 +21,12 @@ export function entryDraft(state = Map(), action) {
|
||||
case DRAFT_CHANGE:
|
||||
return state.set('entry', action.payload);
|
||||
|
||||
case DRAFT_ADD_MEDIA:
|
||||
return state.update('mediaFiles', (list) => list.push(action.payload));
|
||||
|
||||
case DRAFT_REMOVE_MEDIA:
|
||||
const mediaIndex = state.get('mediaFiles').indexOf(action.payload);
|
||||
if (mediaIndex === -1) return state;
|
||||
return state.update('mediaFiles', (list) => list.splice(mediaIndex, 1));
|
||||
case ADD_MEDIA:
|
||||
return state.update('mediaFiles', (list) => list.push(action.payload.name));
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default entryDraft;
|
||||
|
27
src/reducers/index.js
Normal file
27
src/reducers/index.js
Normal file
@ -0,0 +1,27 @@
|
||||
import auth from './auth';
|
||||
import config from './config';
|
||||
import entries, * as fromEntries from './entries';
|
||||
import entryDraft from './entryDraft';
|
||||
import collections from './collections';
|
||||
import medias, * as fromMedias from './medias';
|
||||
|
||||
const reducers = {
|
||||
auth,
|
||||
config,
|
||||
collections,
|
||||
entries,
|
||||
entryDraft,
|
||||
medias
|
||||
};
|
||||
|
||||
export default reducers;
|
||||
|
||||
export const selectEntry = (state, collection, slug) =>
|
||||
fromEntries.selectEntry(state.entries, collection, slug);
|
||||
|
||||
|
||||
export const selectEntries = (state, collection) =>
|
||||
fromEntries.selectEntries(state.entries, collection);
|
||||
|
||||
export const getMedia = (state, fileName) =>
|
||||
fromMedias.getMedia(state.medias, fileName);
|
23
src/reducers/medias.js
Normal file
23
src/reducers/medias.js
Normal file
@ -0,0 +1,23 @@
|
||||
import { Map } from 'immutable';
|
||||
import { ADD_MEDIA } from '../actions/media';
|
||||
import MediaProxy from '../valueObjects/MediaProxy';
|
||||
|
||||
const medias = (state = Map(), action) => {
|
||||
switch (action.type) {
|
||||
case ADD_MEDIA:
|
||||
return state.set(action.payload.name, action.payload);
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default medias;
|
||||
|
||||
export const getMedia = (state, filePath) => {
|
||||
const fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
|
||||
if (state.has(fileName)) {
|
||||
return new MediaProxy(fileName, window.URL.createObjectURL(state.get(fileName), {oneTimeOnly: true}));
|
||||
} else {
|
||||
return new MediaProxy(filePath, null, filePath, true);
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user