Skip to content

Commit

Permalink
issue #21 - completes creating a new organization flow
Browse files Browse the repository at this point in the history
- [x] adds all necessary logic
- [x] adds preventDefault from pressing Enter on form
  • Loading branch information
ortonomy committed Feb 16, 2018
1 parent 131b2b6 commit 91c8c70
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/components/EnrollmentForms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ class EnrollmentForm extends Component {
}
}

keyPress({key}) {
keyPress(e) {
Debug.log('[keyPress] of <EnrollmentForm>: ', this.state);
const { key } = e;
if ( key === 'Enter' ) {
e.preventDefault();
this.submit();
}
}
Expand All @@ -97,7 +100,7 @@ class EnrollmentForm extends Component {
let f;
if ( !this.props.buttonOnly ) {
f = (
<form className={styles.Form}>
<form className={styles.Form} onKeyDown={this.keyPress}>
<FormInputAnimated passValue={this.handleOrgName} name="orgname" text="Organization name" type="text" />
<div className={styles.ErrorMessage}>
{
Expand All @@ -111,7 +114,7 @@ class EnrollmentForm extends Component {
}

return (
<form className={styles.Form}>
<form className={styles.Form} onKeyDown={this.keyPress}>
<div className={styles.ErrorMessage}>
{
this.props.Org.lastOrgSearchError && ( <ErrorMessage messages={[this.props.Org.lastOrgSearchError]} />)
Expand Down
31 changes: 31 additions & 0 deletions src/containers/Login/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const ACTIVATE_FAIL = 'ACTIVATE_FAIL';
export const THIS_USER_UPDATE = 'THIS_USER_UPDATE';
export const THIS_USER_UPDATE_SUCCESS = 'THIS_USER_UPDATE_SUCCESS';
export const THIS_USER_UPDATE_FAIL = 'THIS_USER_UPDATE_FAIL';
export const ADD_USER_ORG = 'ADD_USER_ORG';
export const ADD_USER_ORG_SUCCESS = 'ADD_USER_ORG_SUCCESS';
export const ADD_USER_ORG_FAIL = 'ADD_USER_ORG_FAIL';
export const SET_ERRORS = 'SET_ERRORS';
export const LOGOUT = 'LOGOUT';

Expand All @@ -28,6 +31,9 @@ export const actionTypes = {
THIS_USER_UPDATE,
THIS_USER_UPDATE_SUCCESS,
THIS_USER_UPDATE_FAIL,
ADD_USER_ORG,
ADD_USER_ORG_SUCCESS,
ADD_USER_ORG_FAIL,
SET_ERRORS,
LOGOUT
};
Expand Down Expand Up @@ -120,6 +126,28 @@ export const thisUserUpdateFail = err => (
}
);

export const addUserOrg = org => (
{
type: ADD_USER_ORG,
payload: org
}
);

export const addUserOrgSuccess = result => (
{
type: ADD_USER_ORG_SUCCESS,
payload: result
}
)

export const addUserOrgFail = err => (
{
type: ADD_USER_ORG_FAIL,
payload: err,
error: true
}
);

export const setErrors = error => (
{
type: SET_ERRORS,
Expand Down Expand Up @@ -148,6 +176,9 @@ export const actions = {
thisUserUpdate,
thisUserUpdateSuccess,
thisUserUpdateFail,
addUserOrg,
addUserOrgSuccess,
addUserOrgFail,
setErrors,
logOut
};
Expand Down
61 changes: 56 additions & 5 deletions src/containers/Login/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export const loginLogic = createLogic(
process: ({ action }, dispatch, done) => {
API.Axios('POST', '/graphql', API.generateLoginQuery(action.payload), null)
.then ( data => {
if ( data.authenticate && data.authenticate.hasOwnProperty('jwtToken') && data.authenticate.jwtToken !== null ) {
Debug.log('[action:process:jwt] LOGIN', data.authenticate.jwtToken);
dispatch(actions.loginSuccess(data.authenticate.jwtToken));
if ( data.authenticate ) {
Debug.log('[action:process:jwt] LOGIN', data.authenticate);
dispatch(actions.loginSuccess(data.authenticate));
} else {
throw(new Error('Incorrect email or password'));
}
Expand Down Expand Up @@ -135,7 +135,7 @@ export const thisUserUpdateLogic = createLogic(
})
.catch( error => {
Debug.log(error);
dispatch(actions.thisUserUpdateSuccess(error.message));
dispatch(actions.thisUserUpdateFail(error.message));
})
.then( () => {
done();
Expand Down Expand Up @@ -224,6 +224,56 @@ export const logOutLogic = createLogic(
);


export const addUserOrgLogic = createLogic(
{
type: actionTypes.ADD_USER_ORG,
validate: ({ action }, allow, reject) => {
if ( action.type === 'ADD_USER_ORG' && action.payload ) {
allow(action);
} else { /* empty request, silently reject */
reject();
}
},
process: ({ action, getState }, dispatch, done) => {
const { Login } = (getState());
Debug.log('[action:process:remoteAPI] ADD_USER_ORG', action);
API.Axios(
'POST', '/graphql',
API.generateUpdateUserByIdMutation(
{
id: Login.user.userAccId,
email: Login.user.userEmail,
firstName: Login.user.userFirstName,
lastName: Login.user.userLastName,
org: action.payload
}
),
Login.jwt
)
.then ( data => {
if ( data.usrUpdateUserById !== null ) {
dispatch(actions.addUserOrgSuccess(data.usrUpdateUserById.simpleUser.userOrg));
}
})
.catch ( err => ( dispatch(null)))
.then ( () => ( done() ));
}
}
)

export const addUserOrgLogicSuccess = createLogic(
{
type: actionTypes.ADD_USER_ORG_SUCCESS,
validate: ({ action }, allow, reject) => {
if ( action.type === 'ADD_USER_ORG_SUCCESS' && action.payload ) {
allow(action);
} else { /* empty request, silently reject */
reject();
}
}
}
)

export default [
loginLogic,
loginSuccessLogic,
Expand All @@ -233,5 +283,6 @@ export default [
activateSuccessLogic,
activateFailLogic,
thisUserUpdateLogic,
logOutLogic
logOutLogic,
addUserOrgLogic
];
9 changes: 9 additions & 0 deletions src/containers/Login/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ export default function loginReducer(state = initialState, action) {
loggedIn: false
}
}
case 'ADD_USER_ORG_SUCCESS': {
return {
...state,
user: {
...state.user,
userOrg: action.payload
}
}
}
default: {
return state;
}
Expand Down
5 changes: 4 additions & 1 deletion src/containers/OrgEnrolment/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createLogic } from 'redux-logic';

//custom actions
import { actionTypes, actions } from './actions';
import { actions as loginActions } from '../Login/actions';

// custom utils
import API from '../../utils/Api';
Expand Down Expand Up @@ -57,7 +58,7 @@ export const orgCreateLogic = createLogic(
Debug.log('[action:process:remoteAPI] ORG_CREATE', action);
API.Axios('POST', '/graphql', API.generateCreateOrgMutation({user: state.Login.user.userAccId, name: state.Org.enrolment.searchTerm}), state.Login.jwt)
.then ( data => (dispatch(actions.orgCreateSuccess(data.createOrganization.organization))))
.catch ( err => (dispatch(action.orgCreateFail(err.message))))
.catch ( err => (dispatch(actions.orgCreateFail(err.message))))
.then( () => ( done()));
}
}
Expand All @@ -74,6 +75,8 @@ export const orgCreateSuccessLogic = createLogic(
}
},
process: ({ getState, action }, dispatch, done ) => {
dispatch(loginActions.addUserOrg(action.payload.orgId));
done();
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion src/containers/OrgEnrolment/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function orgReducer(state = initialState, action) {
}
}
case 'ORG_SEARCH_FAIL': {
return {
return {
...state,
enrolment: {
...state.enrolment,
Expand Down
32 changes: 27 additions & 5 deletions src/utils/Api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ class API {

static generateLoginQuery({email, password}) {
return `
mutation {
authenticate (input: {
query {
authenticate (
email: "${email}",
password: "${password}"
} ) {
jwtToken
}
)
}
`;
}
Expand Down Expand Up @@ -144,6 +142,30 @@ class API {
}
`;
}

static generateUpdateUserByIdMutation({id, email, firstName, lastName, org}) {
return `
mutation {
usrUpdateUserById (input: {
userIdIn: "${id}",
userOrgIn: "${org}",
userFirstNameIn: "${firstName}",
userLastNameIn: "${lastName}",
userEmailIn: "${email}"
}) {
simpleUser {
userAccId
userEmail
userEmailConfirmed
userPasswordResetRequested
userFirstName
userLastName
userOrg
}
}
}
`;
}
}

export default API;

0 comments on commit 91c8c70

Please sign in to comment.