text: import * as ActionTypes from './AppConstants';
  import API from './Utilities/Api';
  import cogoToast from 'cogo-toast';
  
  const notificationMessages = {
      ru: {
          error: 'Ошибка! ',
          success: 'Успешно! ',
          processError: 'Повторите попытку позже.',
          deleteDiskError: 'Отсоедините клиентов перед удалением диска.'
      },
      en: {
          error: 'Error! ',
          success: 'Success! ',
          processError: 'Please try again later.',
          deleteDiskError: 'Unassign clients before deleting disk.'
      }
  };
  
  const waitingForBaseUrl = async () => {
      let url = insights.chrome.getBaseUrl();
  
      if (!url) {
          await new Promise(res => setTimeout(res, 500));
          return waitingForBaseUrl();
      } else {
          return url;
      }
  };
  
  const fetchData = async (url, headers, options) => {
      await insights.chrome.auth.getUser();
      const token = await insights.chrome.auth.getToken();
      const baseUrl = await waitingForBaseUrl();
      const response = await API.get(baseUrl + url, { ...headers, Authorization: `Bearer ${token}` }, { role: insights.chrome.getRole(), ...options });
      return response.data;
  };
  
  const createData = async (url, payload, headers) => {
      await insights.chrome.auth.getUser();
      const token = await insights.chrome.auth.getToken();
      const baseUrl = await waitingForBaseUrl();
      let newUrl = `${baseUrl}${url}?role=${insights.chrome.getRole()}`;
      const response = await API.put(newUrl, payload, { ...headers, Authorization: `Bearer ${token}` });
      return response.data;
  };
  
  const deleteData = async (url, payload, headers) => {
      await insights.chrome.auth.getUser();
      const token = await insights.chrome.auth.getToken();
      const baseUrl = await waitingForBaseUrl();
      let newUrl = `${baseUrl}${url}?role=${insights.chrome.getRole()}`;
      const response = await API.delete(newUrl, payload, { ...headers, Authorization: `Bearer ${token}` });
      return response.data;
  };
  
  const notificationOptions = { position: 'top-right', hideAfter: 7 };
  
  const errorNotification = (msg) => cogoToast.error(notificationMessages[localStorage.getItem('icdc-lang') || 'en'].error + msg, notificationOptions);
  const successNotification = (msg) => cogoToast.success(notificationMessages[localStorage.getItem('icdc-lang') || 'en'].success + msg, notificationOptions);
  
  const checkErrorCodes = (item) => {
      switch (item.status) {
      case 422:
          return errorNotification(notificationMessages[localStorage.getItem('icdc-lang') || 'en'].deleteDiskError);
      case 500:
          return errorNotification(notificationMessages[localStorage.getItem('icdc-lang') || 'en'].processError);
      default:
          return errorNotification(item.data.explanation);
      }
  };
  
  // s3 users actions
  
  export const fetchInfo = () => ({
      type: ActionTypes.INFO_FETCH,
      payload: fetchData(ActionTypes.infoUrl(), {}, {})
  });
  
  export const createS3user = (payload) => ({
      type: ActionTypes.CREATE_S3_USER,
      payload: createData(ActionTypes.s3UsersUrl(), payload, {})
  });
  
  export const fetchS3Users = (options) => ({
      type: ActionTypes.S3_USERS_FETCH,
      payload: fetchData(ActionTypes.s3UsersUrl(), {}, options)
  });
  
  export const deleteS3user = (name) => ({
      type: ActionTypes.DELETE_S3_USER,
      payload: deleteData(`${ActionTypes.s3UsersUrl()}/${name}`, {}, {})
  });
  
  export const editS3user = (name, payload) => ({
      type: ActionTypes.EDIT_S3_USER,
      payload: createData(`${ActionTypes.s3UsersUrl()}/${name}`, payload, {})
  });
  
  export const fetchS3User = (name) => ({
      type: ActionTypes.S3_USER_FETCH,
      payload: fetchData(`${ActionTypes.s3UsersUrl()}/${name}`, {}, {})
  });
  
  export const clearS3UserFetchStatus = () => ({
      type: ActionTypes.CLEAR_S3_USER_FETCH_STATUS
  });
  
  export const lockS3user = (name) => ({
      type: ActionTypes.S3_USER_LOCK,
      payload: createData(`${ActionTypes.s3UsersUrl()}/${name}/lock`, {}, {})
  });
  
  export const unlockS3user = (name) => ({
      type: ActionTypes.S3_USER_UNLOCK,
      payload: createData(`${ActionTypes.s3UsersUrl()}/${name}/unlock`, {}, {})
  });
  
  export const generateKeys = (name) => ({
      type: ActionTypes.S3_USER_GENERATE_KEYS,
      payload: createData(`${ActionTypes.s3UsersUrl()}/${name}/keys`, {}, {})
  });
  
  export const createS3userAndFetch = (payload) => {
      return (dispatch) => {
          const response = dispatch(createS3user(payload));
  
          response.then(() => {
              dispatch(fetchS3Users());
              dispatch(fetchInfo());
              successNotification('');
          }, error => errorNotification(error.response.data.explanation));
      };
  };
  
  export const editS3userAndFetch = (name, payload) => {
      return (dispatch) => {
          const response = dispatch(editS3user(name, payload));
  
          response.then(() => {
              dispatch(fetchS3Users());
              dispatch(fetchInfo());
              successNotification('');
          }, error => errorNotification(error.response.data.explanation));
      };
  };
  
  export const deleteS3userAndFetch = (name) => {
      return (dispatch) => {
          const response = dispatch(deleteS3user(name));
  
          response.then(() => {
              dispatch(fetchS3Users());
              dispatch(fetchInfo());
              successNotification('');
          }, error => errorNotification(error.response.data.explanation));
      };
  };
  
  export const actionAndFetch = (action, payload) => {
      return (dispatch) => {
          const response = dispatch(action(payload));
  
          response.then(() => {
              dispatch(fetchS3Users());
              dispatch(fetchInfo());
              successNotification('');
          }, error => errorNotification(error.response.data.explanation));
      };
  };
  
  // buckets actions
  
  export const fetchBuckets = (name) => ({
      type: ActionTypes.BUCKETS_FETCH,
      payload: fetchData(`${ActionTypes.s3UsersUrl()}/${name}/buckets`, {}, {})
  });
  
  export const createBucket = (name, payload) => ({
      type: ActionTypes.CREATE_BUCKET,
      payload: createData(`${ActionTypes.s3UsersUrl()}/${name}/buckets`, payload, {})
  });
  
  export const deleteBucket = (bucket) => ({
      type: ActionTypes.DELETE_BUCKET,
      payload: deleteData(`${ActionTypes.s3UsersUrl()}/${bucket.s3user_name}/buckets/${bucket.bucket_name}`, {}, {})
  });
  
  export const editBucket = (name, payload) => ({
      type: ActionTypes.DELETE_BUCKET,
      payload: createData(`${ActionTypes.s3UsersUrl()}/${name}/buckets/${payload.bucket_name}`, payload, {})
  });
  
  export const createBucketAndFetch = (name, payload) => {
      return (dispatch) => {
          const response = dispatch(createBucket(name, payload));
  
          response.then(() => {
              dispatch(fetchBuckets(name));
              successNotification('');
          }, error => errorNotification(error.response.data.explanation));
      };
  };
  
  export const editBucketAndFetch = (name, payload) => {
      return (dispatch) => {
          const response = dispatch(editBucket(name, payload));
  
          response.then(() => {
              dispatch(fetchBuckets(name));
              successNotification('');
          }, error => errorNotification(error.response.data.explanation));
      };
  };
  
  export const deleteBucketAndFetch = (bucket) => {
      return (dispatch) => {
          const response = dispatch(deleteBucket(bucket));
  
          response.then(() => {
              dispatch(fetchBuckets(bucket.s3user_name));
              successNotification('');
          }, error => errorNotification(error.response.data.explanation));
      };
  };
  
  // iscsi actions
  
  export const fetchIscsiInfo = () => ({
      type: ActionTypes.ISCSI_INFO_FETCH,
      payload: fetchData(ActionTypes.iscsiInfoUrl(), {}, {})
  });
  
  // disks actions
  
  export const fetchIscsiDisks = (options) => ({
      type: ActionTypes.ISCSI_DISKS_FETCH,
      payload: fetchData(ActionTypes.iscsiDisksUrl(), {}, options)
  });
  
  export const createIscsiDisk = (payload) => ({
      type: ActionTypes.CREATE_ISCSI_DISK,
      payload: createData(ActionTypes.iscsiDisksUrl(), payload, {})
  });
  
  export const deleteIscsiDisk = (name) => ({
      type: ActionTypes.DELETE_ISCSI_DISK,
      payload: deleteData(`${ActionTypes.iscsiDisksUrl()}/${name}`, {}, {})
  });
  
  export const editIscsiDisk = (name, payload) => ({
      type: ActionTypes.EDIT_ISCSI_DISK,
      payload: createData(`${ActionTypes.iscsiDisksUrl()}/${name}`, payload, {})
  });
  
  export const createIscsiDiskActionAndFetch = (payload) => {
      return (dispatch) => {
          const response = dispatch(createIscsiDisk(payload));
  
          response.then(() => {
              dispatch(fetchIscsiDisks());
              dispatch(fetchIscsiInfo());
              successNotification('');
          }, error => checkErrorCodes(error.response));
      };
  };
  
  export const editIscsiDiskActionAndFetch = (name, payload) => {
      return (dispatch) => {
          const response = dispatch(editIscsiDisk(name, payload));
  
          response.then(() => {
              dispatch(fetchIscsiDisks());
              dispatch(fetchIscsiInfo());
              successNotification('');
          }, error => checkErrorCodes(error.response));
      };
  };
  
  export const deleteIscsiDiskActionAndFetch = (name) => {
      return (dispatch) => {
          const response = dispatch(deleteIscsiDisk(name));
          response.then(() => {
              dispatch(fetchIscsiDisks());
              dispatch(fetchIscsiInfo());
              successNotification('');
          }, error => checkErrorCodes(error.response));
      };
  };
  
  // clients actions
  
  export const fetchIscsiClients = (options) => ({
      type: ActionTypes.ISCSI_CLIENTS_FETCH,
      payload: fetchData(ActionTypes.iscsiClientsUrl(), {}, options)
  });
  
  export const createIscsiClient = (payload) => ({
      type: ActionTypes.CREATE_ISCSI_CLIENT,
      payload: createData(ActionTypes.iscsiClientsUrl(), payload, {})
  });
  
  export const deleteIscsiClient = (name) => ({
      type: ActionTypes.DELETE_ISCSI_CLIENT,
      payload: deleteData(`${ActionTypes.iscsiClientsUrl()}/${name}`, {}, {})
  });
  
  export const editIscsiClient = (name, payload) => ({
      type: ActionTypes.EDIT_ISCSI_CLIENT,
      payload: createData(`${ActionTypes.iscsiClientsUrl()}/${name}`, payload, {})
  });
  
  export const createIscsiClientActionAndFetch = (payload) => {
      return (dispatch) => {
          const response = dispatch(createIscsiClient(payload));
  
          response.then(() => {
              dispatch(fetchIscsiClients());
              dispatch(fetchIscsiInfo());
              successNotification('');
          }, error => checkErrorCodes(error.response));
      };
  };
  
  export const editIscsiClientActionAndFetch = (name, payload) => {
      return (dispatch) => {
          const response = dispatch(editIscsiClient(name, payload));
  
          response.then(() => {
              dispatch(fetchIscsiClients());
              dispatch(fetchIscsiInfo());
              successNotification('');
          }, error => checkErrorCodes(error.response));
      };
  };
  
  export const deleteIscsiClientActionAndFetch = (name) => {
      return (dispatch) => {
          const response = dispatch(deleteIscsiClient(name));
  
          response.then(() => {
              dispatch(fetchIscsiClients());
              dispatch(fetchIscsiInfo());
              successNotification('');
          }, error => checkErrorCodes(error.response));
      };
  };
  
  export const updateUser = (newUser) => ({
      type: ActionTypes.USER_UPDATE,
      payload: newUser
  });
  