Change the way the request methods get the current state

Since the state cannot be retrieved while the store has changes to be dispatched
the actual state (which is used for building the auth headers for requests) has
to be retrieved beforehand and given to the action as parameter.
This commit is contained in:
JP1998 2018-12-11 13:34:18 +01:00
parent d46062f453
commit d81a8f8cc6
1 changed files with 20 additions and 16 deletions

View File

@ -59,11 +59,11 @@ export function deleteRequest(state, url) {
} }
function generateHeaders(state) { function generateHeaders(state) {
if(state.isSignedIn) { if(state.userinfo.isSignedIn) {
return { return {
'access-token' : state.accesstoken, 'access-token' : state.userinfo.accesstoken,
'client' : state.client, 'client' : state.userinfo.client,
'uid' : state.uid 'uid' : state.userinfo.uid
}; };
} else { } else {
return {}; return {};
@ -102,7 +102,7 @@ function checkForAuthenticationHeaders(response) {
const reducer_userinfo = (state = defaultstate_userinfo, action) => { const reducer_userinfo = (state = defaultstate_userinfo, action) => {
switch(action.type) { switch(action.type) {
case actiontypes_userinfo.REGISTER: case actiontypes_userinfo.REGISTER:
postRequest(state, '/users', { postRequest(action.state, '/users', {
'username' : action.parameters.username, 'username' : action.parameters.username,
'email' : action.parameters.email, 'email' : action.parameters.email,
'password' : action.parameters.password 'password' : action.parameters.password
@ -143,7 +143,7 @@ const reducer_userinfo = (state = defaultstate_userinfo, action) => {
errorMessages : action.parameters.errorMessages errorMessages : action.parameters.errorMessages
}); });
case actiontypes_userinfo.LOGIN: case actiontypes_userinfo.LOGIN:
postRequest(state, '/users/sign_in', { postRequest(action.state, '/users/sign_in', {
email : action.parameters.email, email : action.parameters.email,
password : action.parameters.password password : action.parameters.password
}).then((resp) => { }).then((resp) => {
@ -186,7 +186,7 @@ const reducer_userinfo = (state = defaultstate_userinfo, action) => {
errorMessages : action.parameters.errorMessages errorMessages : action.parameters.errorMessages
}); });
case actiontypes_userinfo.LOGOUT: case actiontypes_userinfo.LOGOUT:
deleteRequest(state, '/users/sign_out').then(() => { deleteRequest(action.state, '/users/sign_out').then(() => {
__store.dispatch({ type : actiontypes_userinfo.CLEAR }); __store.dispatch({ type : actiontypes_userinfo.CLEAR });
}).catch(() => { }).catch(() => {
__store.dispatch({ type : actiontypes_userinfo.CLEAR }); __store.dispatch({ type : actiontypes_userinfo.CLEAR });
@ -200,7 +200,7 @@ const reducer_userinfo = (state = defaultstate_userinfo, action) => {
uid : action.parameters.uid uid : action.parameters.uid
}); });
case actiontypes_userinfo.VERIFY_CREDENTIALS: case actiontypes_userinfo.VERIFY_CREDENTIALS:
getRequest(state, '/users/validate_token').then((resp) => { getRequest(action.state, '/users/validate_token').then((resp) => {
storeOptionalToken(resp); storeOptionalToken(resp);
}).catch(() => { }).catch(() => {
__store.dispatch({ type: actiontypes_userinfo.CLEAR }); __store.dispatch({ type: actiontypes_userinfo.CLEAR });
@ -250,7 +250,10 @@ export function verifyCredentials() {
rehydrateApplicationState(); rehydrateApplicationState();
if(__store.getState().userinfo.isSignedIn) { if(__store.getState().userinfo.isSignedIn) {
__store.dispatch({ type: actiontypes_userinfo.VERIFY_CREDENTIALS }); __store.dispatch({
type: actiontypes_userinfo.VERIFY_CREDENTIALS,
state: __store.getState()
});
} }
} }
@ -261,7 +264,8 @@ export function register(username, email, password) {
username: username, username: username,
email: email, email: email,
password: password password: password
} },
state: __store.getState()
}); });
} }
@ -271,16 +275,16 @@ export function login(email, password) {
parameters: { parameters: {
email: email, email: email,
password: password password: password
} },
state: __store.getState()
}); });
} }
export function logout() { export function logout() {
__store.dispatch({ type : actiontypes_userinfo.LOGOUT }); __store.dispatch({
} type : actiontypes_userinfo.LOGOUT,
state: __store.getState()
export function getState() { });
return __store.getState();
} }
function rehydrateApplicationState() { function rehydrateApplicationState() {