Skip to content

Instantly share code, notes, and snippets.

@leandrojo
Created November 14, 2018 19:08
Show Gist options
  • Save leandrojo/4921c4e288afc9b1010741c39745849a to your computer and use it in GitHub Desktop.
Save leandrojo/4921c4e288afc9b1010741c39745849a to your computer and use it in GitHub Desktop.
Store/Register
/* @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
});
};
}
/* @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);
/* @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