Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducer with types #20

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ docs/
node_modules/
npm-debug.log
.DS_Store

# IDE
.idea/
193 changes: 193 additions & 0 deletions flow-typed/npm/flow-typed_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// flow-typed signature: 8898603ed592b58f9cb89fd30a371a09
// flow-typed version: <<STUB>>/flow-typed_v^2.1.2/flow_v0.46.0

/**
* This is an autogenerated libdef stub for:
*
* 'flow-typed'
*
* Fill this stub out by replacing all the `any` types.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/

declare module 'flow-typed' {
declare module.exports: any;
}

/**
* We include stubs for each file inside this npm package in case you need to
* require those files directly. Feel free to delete any files that aren't
* needed.
*/
declare module 'flow-typed/dist/cli' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/create-stub' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/install' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/runTests' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/search' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/update-cache' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/update' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/validateDefs' {
declare module.exports: any;
}

declare module 'flow-typed/dist/commands/version' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/cacheRepoUtils' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/codeSign' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/fileUtils' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/flowProjectUtils' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/flowVersion' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/git' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/github' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/isInFlowTypedRepo' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/libDefs' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/node' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/npm/npmLibDefs' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/npm/npmProjectUtils' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/semver' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/stubUtils' {
declare module.exports: any;
}

declare module 'flow-typed/dist/lib/validationErrors' {
declare module.exports: any;
}

// Filename aliases
declare module 'flow-typed/dist/cli.js' {
declare module.exports: $Exports<'flow-typed/dist/cli'>;
}
declare module 'flow-typed/dist/commands/create-stub.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/create-stub'>;
}
declare module 'flow-typed/dist/commands/install.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/install'>;
}
declare module 'flow-typed/dist/commands/runTests.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/runTests'>;
}
declare module 'flow-typed/dist/commands/search.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/search'>;
}
declare module 'flow-typed/dist/commands/update-cache.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/update-cache'>;
}
declare module 'flow-typed/dist/commands/update.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/update'>;
}
declare module 'flow-typed/dist/commands/validateDefs.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/validateDefs'>;
}
declare module 'flow-typed/dist/commands/version.js' {
declare module.exports: $Exports<'flow-typed/dist/commands/version'>;
}
declare module 'flow-typed/dist/lib/cacheRepoUtils.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/cacheRepoUtils'>;
}
declare module 'flow-typed/dist/lib/codeSign.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/codeSign'>;
}
declare module 'flow-typed/dist/lib/fileUtils.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/fileUtils'>;
}
declare module 'flow-typed/dist/lib/flowProjectUtils.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/flowProjectUtils'>;
}
declare module 'flow-typed/dist/lib/flowVersion.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/flowVersion'>;
}
declare module 'flow-typed/dist/lib/git.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/git'>;
}
declare module 'flow-typed/dist/lib/github.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/github'>;
}
declare module 'flow-typed/dist/lib/isInFlowTypedRepo.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/isInFlowTypedRepo'>;
}
declare module 'flow-typed/dist/lib/libDefs.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/libDefs'>;
}
declare module 'flow-typed/dist/lib/node.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/node'>;
}
declare module 'flow-typed/dist/lib/npm/npmLibDefs.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/npm/npmLibDefs'>;
}
declare module 'flow-typed/dist/lib/npm/npmProjectUtils.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/npm/npmProjectUtils'>;
}
declare module 'flow-typed/dist/lib/semver.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/semver'>;
}
declare module 'flow-typed/dist/lib/stubUtils.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/stubUtils'>;
}
declare module 'flow-typed/dist/lib/validationErrors.js' {
declare module.exports: $Exports<'flow-typed/dist/lib/validationErrors'>;
}
86 changes: 86 additions & 0 deletions flow-typed/npm/seamless-immutable_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// flow-typed signature: 70f8099c896120a6e0625971ac1532e7
// flow-typed version: <<STUB>>/seamless-immutable_v^7.1.2/flow_v0.46.0

/**
* This is an autogenerated libdef stub for:
*
* 'seamless-immutable'
*
* Fill this stub out by replacing all the `any` types.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/

declare module 'seamless-immutable' {
declare module.exports: any;
}

// sources: https://github.com/rtfeldman/seamless-immutable/issues/121
// ->
// https://github.com/ajhyndman/fantasia/blob/master/flow/libs/seamless-immutable.d.js
//
//
// declare module 'seamless-immutable' {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve this declaration

// declare type Immutable<T: Object | Array<*>> = T & {
// // Array methods
// flatMap(fn: Function): Array<any>;
// asObject(fn: Function): Object;
// asMutable(): Array<any>;
// // Object methods
// merge(collection: Array<any> | Object, deep?: Object): Object;
// set(key: string, value: any): Object;
// setIn(keyPath: Array<string>, value: any): Object;
// update(key: string, fn: Function): Object;
// updateIn(keyPath: Array<string>, fn: Function): Object;
// without(fn: Function): Object;
// without(keys: Array<string>): Object;
// without(...keys: Array<string>): Object;
// asMutable(): Array<any> | Object;
// };
//
// declare function from<T: Object | Array<*>> (spec: T): Immutable<T>;
//
// declare type Default = {
// from: typeof from;
// isImmutable: (x: *) => boolean
// };
//
// declare module.exports: Default;
// }

/**
* We include stubs for each file inside this npm package in case you need to
* require those files directly. Feel free to delete any files that aren't
* needed.
*/
declare module 'seamless-immutable/seamless-immutable.development' {
declare module.exports: any;
}

declare module 'seamless-immutable/seamless-immutable.development.min' {
declare module.exports: any;
}

declare module 'seamless-immutable/seamless-immutable.production.min' {
declare module.exports: any;
}

declare module 'seamless-immutable/src/seamless-immutable' {
declare module.exports: any;
}

// Filename aliases
declare module 'seamless-immutable/seamless-immutable.development.js' {
declare module.exports: $Exports<'seamless-immutable/seamless-immutable.development'>;
}
declare module 'seamless-immutable/seamless-immutable.development.min.js' {
declare module.exports: $Exports<'seamless-immutable/seamless-immutable.development.min'>;
}
declare module 'seamless-immutable/seamless-immutable.production.min.js' {
declare module.exports: $Exports<'seamless-immutable/seamless-immutable.production.min'>;
}
declare module 'seamless-immutable/src/seamless-immutable.js' {
declare module.exports: $Exports<'seamless-immutable/src/seamless-immutable'>;
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"test": "./scripts/mocha.js",
"test:watch": "./scripts/mocha-watch.js",
"coverage": "./scripts/coverage.js",
"flow": "flow"
"flow": "flow",
"flow:typed": "flow-typed install"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -84,11 +85,10 @@
"tmp": "0.0.28"
},
"dependencies": {
"flow-typed": "^2.1.2",
"seamless-immutable": "^7.1.2"
},
"peerDependencies": {
"react": ">=0.14.2 || >=15.0.0",
"react-redux": ">=3",
"redux": ">=3"

}
}
4 changes: 2 additions & 2 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow */
import type { ActionType, PropertyType, ValueType } from './types';
import type { ActionType, PropertyType, ValueType, ValuesType } from './types';

export const SET_ONION_FORM_FIELD_PROPERTY = 'SET_ONION_FORM_FIELD_PROPERTY';
export const SET_ONION_FORM_MULTIPLE_FIELDS = 'SET_ONION_FORM_MULTIPLE_FIELDS';
Expand All @@ -15,7 +15,7 @@ export function registerField(form: string, field: string): ActionType {
};
}

export function setMultipleFields(form: string, property: PropertyType, values: Array<ValueType>): ActionType {
export function setMultipleFields(form: string, property: PropertyType, values: ValuesType): ActionType {
return {
type: 'SET_ONION_FORM_MULTIPLE_FIELDS',
form,
Expand Down
25 changes: 14 additions & 11 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @flow
import * as actions from './actions';
import Immutable from 'seamless-immutable';

import { reduceObject } from './helpers';

export const initialState = Immutable({
fields: Immutable({}),
forms: Immutable({})
import type { StateType, FieldsType, ActionType } from './types'

export const initialState: StateType = Immutable({
fields: Immutable({})
});

export const deafultFieldProperties = Immutable({
Expand All @@ -15,17 +17,17 @@ export const deafultFieldProperties = Immutable({
apiError: null
});

function reviveFields(fields) {
function reviveFields(fields: FieldsType): Immutable {
return Object.keys(fields).reduce(
(acc, field) => acc.set(field, deafultFieldProperties.merge(fields[field])),
(acc: Immutable, field: string): Immutable => acc.set(field, deafultFieldProperties.merge(fields[field])),
Immutable({})
);
}

function revive(state) {
function revive(state: StateType) {
const fields = (state.fields || Immutable({}));
return Object.keys(fields).reduce(
(acc, form) => acc.setIn(['fields', form], reviveFields(fields[form]))
(acc: Immutable, form: string) => acc.setIn(['fields', form], reviveFields(fields[form]))
, initialState
);
}
Expand All @@ -36,8 +38,8 @@ function revive(state) {
* @param {Object} action Flux action
* @return {Object} Updated app state
*/
export default function translationReducer(inputState = initialState, action = {}) {
const state = !(Immutable.isImmutable(inputState)) ? revive(inputState) : inputState;
export default function translationReducer(inputState: StateType = initialState, action: ActionType): StateType {
const state: Immutable = !(Immutable.isImmutable(inputState)) ? revive(inputState) : inputState;

switch (action.type) {
case actions.REGISTER_ONION_FORM_FIELD: {
Expand All @@ -55,8 +57,9 @@ export default function translationReducer(inputState = initialState, action = {

case actions.SET_ONION_FORM_MULTIPLE_FIELDS: {
const { form, property, values } = action;
return Object.keys(values).reduce(
(acc, field) => acc.setIn(['fields', form, field, property], values[field]),
const valuesKeys = Object.keys(values);
return valuesKeys.reduce(
(acc: Immutable<StateType>, field: string): StateType => acc.setIn(['fields', form, field, property], values[field]),
state
);
}
Expand Down
Loading