Created
November 14, 2018 19:08
-
-
Save leandrojo/4921c4e288afc9b1010741c39745849a to your computer and use it in GitHub Desktop.
Store/Register
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
/* @flow */ | |
export const REGISTER_USER_REQUEST = "REGISTER_USER_REQUEST"; | |
export const REGISTER_USER_SUCCESS = "REGISTER_USER_SUCCESS"; | |
export const REGISTER_USER_FAILURE = "REGISTER_USER_FAILURE"; | |
export function success(payload) { | |
return dispatch => { | |
dispatch({ | |
type: REGISTER_USER_SUCCESS, | |
payload | |
}); | |
}; | |
} | |
export function failure(payload) { | |
return dispatch => { | |
dispatch({ | |
type: REGISTER_USER_FAILURE, | |
payload | |
}); | |
}; | |
} | |
export default function register(payload) { | |
return (dispatch, getState) => { | |
dispatch({ | |
type: REGISTER_USER_REQUEST, | |
payload | |
}); | |
}; | |
} |
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
/* @flow */ | |
import { Observable } from "rxjs"; | |
import { combineEpics } from "redux-observable"; | |
import s from "underscore.string"; | |
import { mapObject } from "underscore"; | |
import { Auth } from "aws-amplify"; | |
import fetch from "../fetch"; | |
import { REGISTER_USER_REQUEST, failure, success } from "./action"; | |
const { underscored } = s; | |
const prepare = payload => { | |
const attributes = {}; | |
mapObject(prevAttributes, (value, key) => { | |
attributes[underscored(key)] = value; | |
}); | |
return { | |
username, | |
password, | |
attributes | |
}; | |
}; | |
const registerUser = action$ => | |
action$ | |
.ofType(REGISTER_USER_REQUEST) | |
.map(({ payload }) => prepare(payload)) | |
.mergeMap(({ username, password, attributes }) => | |
Observable.fromPromise(Auth.signUp(username, password, attributes)) | |
.map(response => response.json()) | |
.flatMap(response => Observable.of(success(response))) | |
.catch(err => Observable.of(failure(err))) | |
); | |
export default combineEpics(registerUser); |
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
/* @flow */ | |
import { | |
REGISTER_USER_REQUEST, | |
REGISTER_USER_FAILURE, | |
REGISTER_USER_SUCCESS | |
} from "./action"; | |
const initialState = { | |
type: REGISTER_USER_REQUEST, | |
payload: {} | |
}; | |
function reducer(state = initialState, action) { | |
const { payload, type } = action; | |
if (type === REGISTER_USER_REQUEST) { | |
return { | |
type, | |
payload | |
}; | |
} | |
if (type === REGISTER_USER_FAILURE) { | |
return { | |
type, | |
payload | |
}; | |
} | |
if (type === REGISTER_USER_SUCCESS) { | |
return { | |
type, | |
payload | |
}; | |
} | |
return state; | |
} | |
export default reducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment