<commit_before>import { fetchWrapper, fetchOAuthURL, updatePopup } from '../actions';
import { credential_path, credential_oauth_path } from '../../localconf';
import moment from 'moment';

export const loginCloudMiddleware = () => {
  // Fetch projects, if unauthorized, login
  return (dispatch, getState) => {
    return dispatch(fetchStorageAccess()).then(()=>{
      let projects = getState().cloud_access.access_key_pair;
      if (projects){
        // user already logged in
        return Promise.reject("already logged in");
      }
      else {
        return Promise.resolve()
      }
    }).then(()=>dispatch(fetchOAuthURL(credential_oauth_path))).then(()=>{
      let url = getState().user.oauth_url;
      return dispatch(fetchWrapper({
        path:url,
        handler:receiveUserAPILogin
      }))}).then(()=>dispatch(fetchStorageAccess()))
      .catch((error) => console.log(error));
  }
};

export const receiveUserAPILogin = ({status, data}) => {
  switch (status) {
    case 200:
      return {
        type: 'RECEIVE_USERAPI_LOGIN',
        result: true
      };
    default: {
      return {
        type: "RECEIVE_USERAPI_LOGIN",
        result: false,
        error: data
      }
    }
  }
};

export const fetchStorageAccess = () => {
  return fetchWrapper({
    path: credential_path,
    handler: receiveCloudAccess
  })
};

const convertTime = (value) => {
  value['create_date'] = moment(value['create_date']).format('YYYY-MM-DD HH:mm:ss');
  return value
};

const convertTimes = (values) => {
  values.map( item => {
    convertTime(item)
  });
  return values;
};

export const receiveCloudAccess = ({status, data}) => {
  switch (status) {
    case 200:
      return {
        type: 'RECEIVE_CLOUD_ACCESS',
        access_keys: data,
      };
    default:
      return {
        type: 'CLOUD_ACCESS_ERROR',
        error: data['error']
      }
  }
};

export const requestDeleteKey = (access_key_id) => {
  return {
    type: 'REQUEST_DELETE_KEY',
    access_key_id: access_key_id
  }
};

export const deleteKey = (access_key_id) => {
  let receiveDeleteKey = ({status, data}) => {
    console.log('receive delete');
    return receiveDeleteKeyResponse({status, data, access_key_id})
  };
  return fetchWrapper({
    path: credential_path + 'access_key/' + access_key_id,
    method: 'DELETE',
    handler: receiveDeleteKey
  })
};

export const receiveDeleteKeyResponse = ({status, data, access_key_id}) => {
  return (dispatch) => {
    switch (status) {
      case 200:
        dispatch({
          type: 'DELETE_KEY_SUCCEED',
          access_keys: convertTimes(data["access_keys"])
        });
        dispatch(updatePopup({key_delete_popup: false}));
        return dispatch(clearDeleteSession());
      default:
        return dispatch({
          type: 'DELETE_KEY_FAIL',
          access_key_id: access_key_id,
          error: data
        });
    }
  }<commit_msg>fix(access): fix delete key pair<commit_after>import { fetchWrapper, fetchOAuthURL, updatePopup } from '../actions';
import { credential_path, credential_oauth_path } from '../../localconf';
import moment from 'moment';

export const loginCloudMiddleware = () => {
  // Fetch projects, if unauthorized, login
  return (dispatch, getState) => {
    return dispatch(fetchStorageAccess()).then(()=>{
      let projects = getState().cloud_access.access_key_pair;
      if (projects){
        // user already logged in
        return Promise.reject("already logged in");
      }
      else {
        return Promise.resolve()
      }
    }).then(()=>dispatch(fetchOAuthURL(credential_oauth_path))).then(()=>{
      let url = getState().user.oauth_url;
      return dispatch(fetchWrapper({
        path:url,
        handler:receiveUserAPILogin
      }))}).then(()=>dispatch(fetchStorageAccess()))
      .catch((error) => console.log(error));
  }
};

export const receiveUserAPILogin = ({status, data}) => {
  switch (status) {
    case 200:
      return {
        type: 'RECEIVE_USERAPI_LOGIN',
        result: true
      };
    default: {
      return {
        type: "RECEIVE_USERAPI_LOGIN",
        result: false,
        error: data
      }
    }
  }
};

export const fetchStorageAccess = () => {
  return fetchWrapper({
    path: credential_path,
    handler: receiveCloudAccess
  })
};

const convertTime = (value) => {
  value['create_date'] = moment(value['create_date']).format('YYYY-MM-DD HH:mm:ss');
  return value
};

const convertTimes = (values) => {
  values.map( item => {
    convertTime(item)
  });
  return values;
};

export const receiveCloudAccess = ({status, data}) => {
  switch (status) {
    case 200:
      return {
        type: 'RECEIVE_CLOUD_ACCESS',
        access_keys: data,
      };
    default:
      return {
        type: 'CLOUD_ACCESS_ERROR',
        error: data['error']
      }
  }
};

export const requestDeleteKey = (access_key_id) => {
  return {
    type: 'REQUEST_DELETE_KEY',
    access_key_id: access_key_id
  }
};

export const deleteKey = (access_key_id) => {
  let receiveDeleteKey = ({status, data}) => {
    console.log('receive delete');
    return receiveDeleteKeyResponse({status, data, access_key_id})
  };
  return fetchWrapper({
    path: credential_path + access_key_id,
    method: 'DELETE',
    handler: receiveDeleteKey
  })
};

export const receiveDeleteKeyResponse = ({status, data, access_key_id}) => {
  return (dispatch) => {
    switch (status) {
      case 200:
        dispatch({
          type: 'DELETE_KEY_SUCCEED',
          access_keys: convertTimes(data["access_keys"])
        });
        dispatch(updatePopup({key_delete_popup: false}));
        return dispatch(clearDeleteSession());
      default:
        return dispatch({
          type: 'DELETE_KEY_FAIL',
          access_key_id: access_key_id,
          error: data
        });
    }
  }
{'fraction_non_alphanumeric': 0.1375107296137339, 'fraction_numerical': 0.003090128755364807, 'mean_word_length': 2.8531746031746033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5537011', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1772, 'n_words': 373}