import { CODE_OK } from '../../common';
import { prihlaseniValid } from './prihlaseniReducer';

export const hideError = () => ({ type: 'PRIHLASENI_HIDE_ERROR' });

export const inputChanged = (name, event) => ({
  type: 'PRIHLASENI_INPUT_CHANGED',
  name,
  value: event.target.value
});

export const reset = () => ({
  type: 'PRIHLASENI_RESET'
});

const validateEmpty = () => ({ type: 'PRIHLASENI_VALIDATE_EMPTY' });

const validationError = () => ({
  type: 'PRIHLASENI_FORM_INVALID',
  code: 'nejde uložit',
  status: 'Přihláška nejde uložit. Povinná pole nejsou vyplněna.'
});

export const saveUcastRequest = () => ({
  type: 'PRIHLASENI_SAVE_REQUEST'
});

export const saveUcastSuccess = json => ({
  type: 'PRIHLASENI_SAVE_SUCCESS',
  receivedAt: Date.now()
});

export const saveUcastError = ({ code, status, err, ...rest }) => ({
  type: 'PRIHLASENI_SAVE_ERROR',
  code,
  status,
  err,
  ...rest,
  receivedAt: Date.now()
});

export const saveUcast = () => async (dispatch, getState, wsClient) => {
  await dispatch(validateEmpty());

  const { registrator: { prihlaseni } } = getState();
  if (!prihlaseniValid(prihlaseni)) {
    dispatch(validationError());
    return;
  }

  dispatch(saveUcastRequest());

  try {
    const response = await wsClient.sendRequest(
      saveUcast(prihlaseni.ucastnikId, prihlaseni.udaje, prihlaseni.prihlaska)
    );
    const { code } = response;
    if (code === CODE_OK) {
      dispatch(saveUcastSuccess(response));
    } else {
      dispatch(saveUcastError(response));
    }
  } catch (err) {
    dispatch(saveUcastError({ code: 'internal error', err }));
  }
};

@@ -13,29 +13,107 @@
 E_OK
- %7D from '../../common
+, saveUcast as saveUcastAction %7D from '../../common';%0Aimport %7B AKTUALNI_ROK %7D from '../../constants
 ';%0Ai
@@ -1121,16 +1121,44 @@
 ty());%0A%0A
+  const state = getState();%0A
   const 
@@ -1191,26 +1191,21 @@
 i %7D %7D = 
-getS
+s
 tate
-()
 ;%0A  if (
@@ -1395,70 +1395,211 @@
 cast
-(prihlaseni.ucastnikId, prihlaseni.udaje, prihlaseni.prihlaska
+Action(%0A        %7B%0A          id: prihlaseni.ucastnikId,%0A          rok: AKTUALNI_ROK,%0A          udaje: prihlaseni.udaje,%0A          prihlaska: prihlaseni.prihlaska%0A        %7D,%0A        state.auth.token%0A      
 )%0A  

Fix call site of saveUcast() API endpoint.