Last active
May 8, 2018 07:02
-
-
Save aprilrd/b926f9125b672f1dab0c516daf470c83 to your computer and use it in GitHub Desktop.
Typescript + Redux + Immutable.Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// scaffolding | |
interface IPostStateImmutable { | |
get(key: "post"): IPostImmutable | null; // IPostImmutable is also another hacky interface like IPostStateImmutable. | |
get(key: "isLoading"): boolean; | |
set(key: "post", value: IPostImmutable | null): IPostStateImmutable; | |
set(key: "isLoading", value: boolean): IPostStateImmutable; | |
withMutations( | |
mutator: (mutable: IPostStateImmutable) => IPostStateImmutable, | |
): IPostStateImmutable; | |
} | |
// reducers | |
const INITIAL_STATE: IPostStateImmutable = Map({ | |
post: null, | |
isLoading: false, | |
}); | |
function postReducer( | |
state: IPostStateImmutable = INITIAL_STATE, | |
action, | |
): IPostStateImmutable { | |
switch (action.type) { | |
case "FETCHED": { | |
return state.withMutations(currentState => | |
currentState.set("post", action.payload.post).set("isLoading", false), | |
); | |
} | |
case "UPDATED_TITLE": { | |
return state.setIn(["post", "title"], action.payload.title); | |
} | |
default: { | |
return state; | |
} | |
} | |
} | |
// actions stay the same |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment