Skip to content

Instantly share code, notes, and snippets.

@joeyfigaro
Last active March 27, 2017 14:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joeyfigaro/7ea4da184277585194a71e762c93adcf to your computer and use it in GitHub Desktop.
Early DSL abstraction proposal for redux apps

Overview

Idea suggests that we contain language within a module, auto-generate plurals and any other pieces off of a singular base, and consume the pieces throughout our code.

constants/language.js

export const USER = {
	SINGULAR: 'account_holder',
	PLURAL: () => `${USER.SINGULAR}s`
};

export const BILLING = {
	SINGULAR: 'bill',
	PLURAL: () => `${BILLING.SINGULAR}s`
};

reducers/user.js

import { Map } from 'immutable';
import { USER } from '../constants/language';
import {
	SESSION_AUTHENTICATE_REQUEST,
	SESSION_AUTHENTICATE_FAILURE,
	SESSION_AUTHENTICATE_SUCCESS
} from '../actions';

const USER_SINGULAR_KEY = USER.SINGULAR;

import { Map } from 'immutable';
import { USER } from '../constants/language';
import {
	SESSION_AUTHENTICATE_REQUEST,
	SESSION_AUTHENTICATE_FAILURE,
	SESSION_AUTHENTICATE_SUCCESS
} from '../actions';

const USER_SINGULAR_KEY = USER.SINGULAR;

const initial_state = Map({
	is_logged_in: false,
	is_logging_in: false,
	[USER_SINGULAR_KEY]: Map()
});

function session_reducer(state = initialState, action) {
	switch (action.type) {
		case SESSION_AUTHENTICATE_REQUEST:
			return state.merge({
				is_logging_in: true
			});	
		case SESSION_AUTHENTICATE_FAILURE:
			return state.merge({
				is_logging_in: false
			});	
		case SESSION_AUTHENTICATE_SUCCESS:
			return state.merge({
				is_logged_in: true,
				is_logging_in: false,
				[USER_SINGULAR_KEY]: Map(action.payload)
			});
		default:
			return state;	
	}
}

translated reducers/user.js

...
function sessionReducer() {
  switch (action.type) {
		case <SESSION_AUTHENTICATION_CONSTANT>:
			return state.merge({
			        account_holder: action.payload
			});	
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment