import { actionTypes } from 'constants/action-types';
import { request } from 'utils/fetch';
import { Dispatch } from 'redux';
import { api } from 'constants/config';
import moment from 'moment';

export function getTransferinfo() {
  return async (dispatch: Dispatch) => {
    const result = await request(api.getTransferInfo);
    if (result) {
      dispatch({
        type: actionTypes.GET_TRANSFER_INFO,
        payload: {
          deadline: result.deadline
            ? moment(result.deadline).format('YYYY-MM-DD HH:mm:ss')
            : result.deadline,
        },
      });
    }
  };
}

const initialState = {
  deadline: '',
};

export default function reducer(state = initialState, { type, payload }) {
  switch (type) {
    case actionTypes.GET_TRANSFER_INFO: {
      return { ...state, deadline: payload.deadline };
    }
    default:
      return state;
  }
}
