import R from 'ramda';

import { STATUS } from 'xod/client/utils/constants';
import { ApiActionTypes } from 'xod/client/utils/api';
import { UPDATE_COOKIES } from './constants';

const isSucceeded = (action) => (action.meta && action.meta.status === STATUS.SUCCEEDED);

export default (cookieState = {}, action) => {
  if (
    !action.type === UPDATE_COOKIES ||
    !isSucceeded(action)
  ) {
    return cookieState;
  }

  switch (action.type) {
    case UPDATE_COOKIES:
      return action.payload;
    case ApiActionTypes.profile.login:
      return R.pipe(
        R.assoc('access_token', action.payload.response.id),
        R.assoc('user_id', action.payload.response.userId)
      )(cookieState);
    case ApiActionTypes.profile.logout:
      return R.omit(['access_token', 'user_id']);
    default:
      return cookieState;
  }
};

@@ -266,161 +266,132 @@
 D);%0A
-%0Aexport default (cookieState = %7B%7D, action) =%3E %7B%0A  if (%0A    !action.type === UPDATE_COOKIES %7C%7C%0A    !isSucceeded(action)%0A  ) %7B%0A    return cookieState;%0A  %7D%0A
+const hasResponse = (action) =%3E (action.payload && action.payload.response);%0A%0Aexport default (cookieState = %7B%7D, action) =%3E %7B
 %0A  s
@@ -504,16 +504,113 @@
 e.login:
+ %7B%0A      if (!(isSucceeded(action) && hasResponse(action))) %7B%0A        return cookieState;%0A      %7D
 %0A      r
@@ -765,16 +765,22 @@
 State);%0A
+    %7D%0A
     case

allow to create process with progressed / succeed / failed status and roll back to always create new process on started status