/**
 * Actions Template By => create-module script
 * @version 1.0.0
 *
 */

// @flow
import logger from 'helpers/logger';
import { initialize } from 'redux-form';
import { showLoader, hideLoader } from 'ui/ModalLoader/actions';
import { LOADER_CARD_DETAIL_EDIT } from 'constants/actions/loaderNames';
import { CARD_DETAIL_FORM_KEY } from 'constants/forms/card';
import { CARD_TYPES } from 'modules/card/constants';
import { postPublicationDetail } from 'modules/publication/services';
import { postBookDetail } from 'modules/book/services';
import { PERMISSIONS } from 'modules/user/constants';
import { hasPermission } from 'modules/user/helpers';

import type { Dispatch } from 'redux';
import type { Immutable } from 'store/ImmutableTypes';
import type { ImmutableState } from 'store/StateTypes';

export const submitCardDetailForm = async (form: Immutable<Object>, dispatch: Dispatch<*>) => {
  try {
    logger.log('action: submitCardDetailFormStart');
    dispatch(showLoader(LOADER_CARD_DETAIL_EDIT));
    const formData = form.toJS();
    const { type, id } = formData;
    let service;

    switch (type) {
      case CARD_TYPES.PUBLICATION: service = postPublicationDetail;
        break;
      case CARD_TYPES.BOOK: service = postBookDetail;
        break;
      case CARD_TYPES.PUBLISHER: service = () => () => {};
        break;
      default: throw new Error('Card type not found');
    }

    await service(dispatch)(id, formData);

    logger.log('action: submitCardDetailFormEnd');
  } finally {
    dispatch(hideLoader(LOADER_CARD_DETAIL_EDIT));
  }
};

export const initializeForm = (values) => (dispatch) => {
  dispatch(initialize(CARD_DETAIL_FORM_KEY, values));
};

export const mapStateToProps = (state: ImmutableState) => {
  const { permissions } = state.toJS().user;

  return {
    hasEditPublicationPermission: hasPermission(PERMISSIONS.EDIT_PUBLICATION, permissions),
    hasEditBookPermission: hasPermission(PERMISSIONS.EDIT_BOOK, permissions),
    hasEditWriterPermission: hasPermission(PERMISSIONS.EDIT_WRITER, permissions),
    hasEditPublisherPermission: hasPermission(PERMISSIONS.EDIT_PUBLISHER, permissions),
  };
};

export const mapDispatchToProps = {
  initializeForm,
};

@@ -356,16 +356,17 @@
 /card';%0A
+%0A
 import %7B
@@ -414,207 +414,490 @@
 s';%0A
-import %7B postPublicationDetail %7D from 'modules/publication/services';%0Aimport %7B postBookDetail %7D from 'modules/book/services';%0Aimport %7B PERMISSIONS %7D from 'modules/user/constants';%0Aimport %7B hasPermiss
+%0Aimport %7B PERMISSIONS %7D from 'modules/user/constants';%0Aimport %7B hasPermission %7D from 'modules/user/helpers';%0A%0Aimport %7B postPublicationDetail %7D from 'modules/publication/services';%0Aimport %7B transformPublicationToCard %7D from 'modules/publication/transformers';%0Aimport %7B updatePublicationCardAction %7D from 'modules/publication/actions';%0A%0Aimport %7B postBookDetail %7D from 'modules/book/services';%0Aimport %7B transformBookToCard %7D from 'modules/book/transformers';%0Aimport %7B updateBookCardAct
 ion 
@@ -908,35 +908,35 @@
 om 'modules/
-user/helper
+book/action
 s';%0A%0Aimport 
@@ -1371,16 +1371,80 @@
  service
+ = () =%3E %7B%7D;%0A    let transform = () =%3E %7B%7D;%0A    let update = null
 ;%0A%0A    s
@@ -1492,16 +1492,24 @@
 ICATION:
+%0A       
  service
@@ -1526,32 +1526,126 @@
 licationDetail;%0A
+        transform = transformPublicationToCard;%0A        update = updatePublicationCardAction;%0A
         break;%0A 
@@ -1670,16 +1670,24 @@
 ES.BOOK:
+%0A       
  service
@@ -1697,32 +1697,112 @@
 postBookDetail;%0A
+        transform = transformBookToCard;%0A        update = updateBookCardAction;%0A
         break;%0A 
@@ -1940,16 +1940,42 @@
  %7D%0A%0A    
+dispatch(update(transform(
 await se
@@ -2003,16 +2003,19 @@
 ormData)
+)))
 ;%0A%0A    l

update state after update data on card.