<commit_before>import config from '../../config';


/**
 * constants
 */
export const FETCH_WAY_TASK = 'FETCH_WAY_TASK';
export const FETCH_NEXT_WAY_TASK = 'FETCH_NEXT_WAY_TASK';
export const FETCH_WAY_TASK_SUCCESS = 'FETCH_WAY_TASK_SUCCESS';
export const FETCH_WAY_TASK_ERROR = 'FETCH_WAY_TASK_ERROR';
export const FETCH_WAY_TASK_COUNT = 'FETCH_WAY_TASK_COUNT';
export const FETCH_WAY_TASK_COUNT_SUCCESS = 'FETCH_WAY_TASK_COUNT_SUCCESS';
export const FETCH_WAY_TASK_COUNT_ERROR = 'FETCH_WAY_TASK_COUNT_ERROR';
export const MARK_WAY_TASK_PENDING = 'MARK_WAY_TASK_PENDING';
export const MARK_WAY_TASK_PENDING_SUCCESS = 'MARK_WAY_TASK_PENDING_SUCCESS';
export const MARK_WAY_TASK_PENDING_ERROR = 'MARK_WAY_TASK_PENDING_ERROR';
export const SKIP_TASK = 'SKIP_TASK';
export const SELECT_NEXT_WAY_TASK_PROVINCE = 'SELECT_NEXT_WAY_TASK_PROVINCE';

/**
 * action creators
 */
export const fetchWayTask = (id) => ({ type: FETCH_WAY_TASK, id });
export const fetchNextWayTask = () => ({ type: FETCH_NEXT_WAY_TASK });
export const fetchWayTaskSuccess = (id, geoJSON) => ({
  type: FETCH_WAY_TASK_SUCCESS,
  id,
  geoJSON
});
export const fetchWayTaskError = () => ({ type: FETCH_WAY_TASK_ERROR });
export const fetchWayTaskCount = () => ({ type: FETCH_WAY_TASK_COUNT });
export const fetchWayTaskCountSuccess = count => ({ type: FETCH_WAY_TASK_COUNT_SUCCESS, count });
export const fetchWayTaskCountError = count => ({ type: FETCH_WAY_TASK_COUNT_ERROR });
export const markWayTaskPending = () => ({ type: MARK_WAY_TASK_PENDING });
export const markWayTaskPendingSuccess = () => ({ type: MARK_WAY_TASK_PENDING_SUCCESS });
export const markWayTaskPendingError = () => ({ type: MARK_WAY_TASK_PENDING_ERROR });
export const skipTask = id => ({ type: SKIP_TASK, id });
export const selectWayTaskProvince = (provinceId) => ({ type: SELECT_NEXT_WAY_TASK_PROVINCE, selectedProvince: provinceId });


export const fetchNextWayTaskEpic = () => (dispatch, getState) => {
  dispatch(fetchNextWayTask());

  const skipped = getState().waytasks.skipped;
  const selectedProvince = getState().waytasks.selectedProvince;

  let params = {};
  if (skipped.length) params['skip'] = skipped;
  if (selectedProvince) params['province'] = selectedProvince;

  let url = `${config.api}/tasks/next`;
  if (Object.keys(params).length) {
    url = url + '?' + Object.keys(params).map(key => `${key}=${params[key]}`).join('&');
  }

  return fetch(url)
    .then(response => {
      if (response.status === 404) {
        throw new Error('No tasks remaining');
      } else if (response.status >= 400) {
        throw new Error(response.statusText);
      }
      return response.json();
    })
    .then(json => {
      json.data.features.forEach(feature => {
        feature.properties._id = feature.meta.id;
      });
      return dispatch(fetchWayTaskSuccess(json.id, json.data));
    }, e => {
      console.error('Error requesting task', e);
      return dispatch(fetchWayTaskError());
    });
};


export const fetchWayTaskEpic = taskId => (dispatch, getState) => {
  dispatch(fetchWayTask(taskId));

  return fetch(`${config.api}/tasks/${taskId}`)
    .then(response => {
      if (response.status >= 400) {
        throw new Error(response.statusText);
      }
      return response.json();
    })
    .then(({ id, data }) => {
      data.features.forEach(feature => {
        feature.properties._id = feature.meta.id;
      });
      return dispatch(fetchWayTaskSuccess(id, data));
    }, e => {
      console.error('Error reloading task', e);
      return dispatch(fetchWayTaskError());
    });
};


export const fetchWayTaskCountEpic = () => dispatch => {
  dispatch(fetchWayTaskCount());

  fetch(`${config.api}/tasks/count`)
    .then(response => {
      if (response.status >= 400) {
        throw new Error(response.statusText);
      }
      return response.json();
    })
    .then(({ count }) => {
      dispatch(fetchWayTaskCountSuccess(count));
    }, e => {
      console.error('Error requesting task count', e);
      dispatch(fetchWayTaskCountError());
    });
};


export const markWayTaskPendingEpic = way_ids => dispatch => {
  dispatch(markWayTaskPending());

  return fetch(`${config.api}/tasks/pending`, {
    method: 'PUT',
    body: new Blob([JSON.stringify({ way_ids: way_ids })], { type: 'application/json' })
  })
    .then(response => {
      if (response.status >= 400) {
        console.error('Error marking task as pending', response.statusText);
        dispatch(markWayTaskPendingError());
      }

      dispatch(markWayTaskPendingSuccess());
    });
};


/**
 * reducer
 */
export default (
  state = {
    status: 'complete',
    countStatus: 'complete',
    id: null,
    geoJSON: null,
    skipped: [],
    provinces: [],
    selectedProvince: null
  },
  action
) => {
  if (action.type === FETCH_WAY_TASK) {
    return Object.assign({}, state, {
      status: 'pending'
    });
  } else if (action.type === FETCH_NEXT_WAY_TASK) {
    return Object.assign({}, state, {
      status: 'pending'
    });
  } else if (action.type === FETCH_WAY_TASK_SUCCESS) {
    return Object.assign({}, state, {
      geoJSON: action.geoJSON,
      id: action.id,
      status: 'complete'
    });
  } else if (action.type === FETCH_WAY_TASK_ERROR) {
    return Object.assign({}, state, {
      status: 'error'
    });
  } else if (action.type === FETCH_WAY_TASK_COUNT) {
    return Object.assign({}, state, {
      taskCount: action.count,
      countStatus: 'pending'
    });
  } else if (action.type === FETCH_WAY_TASK_COUNT_SUCCESS) {
    return Object.assign({}, state, {
      taskCount: action.count,
      countStatus: 'complete'
    });
  } else if (action.type === FETCH_WAY_TASK_COUNT_ERROR) {
    return Object.assign({}, state, {
      countStatus: 'error'
    });
  } else if (action.type === SKIP_TASK) {
    return Object.assign({}, state, {
      skipped: state.skipped.concat(action.id)
    });
  } else if (action.type === SELECT_NEXT_WAY_TASK_PROVINCE) {
    return Object.assign({}, state, {
      selectedProvince: action.selectedProvince
    });
  }

  return state;
};
<commit_msg>also update the task count<commit_after>import config from '../../config';


/**
 * constants
 */
export const FETCH_WAY_TASK = 'FETCH_WAY_TASK';
export const FETCH_NEXT_WAY_TASK = 'FETCH_NEXT_WAY_TASK';
export const FETCH_WAY_TASK_SUCCESS = 'FETCH_WAY_TASK_SUCCESS';
export const FETCH_WAY_TASK_ERROR = 'FETCH_WAY_TASK_ERROR';
export const FETCH_WAY_TASK_COUNT = 'FETCH_WAY_TASK_COUNT';
export const FETCH_WAY_TASK_COUNT_SUCCESS = 'FETCH_WAY_TASK_COUNT_SUCCESS';
export const FETCH_WAY_TASK_COUNT_ERROR = 'FETCH_WAY_TASK_COUNT_ERROR';
export const MARK_WAY_TASK_PENDING = 'MARK_WAY_TASK_PENDING';
export const MARK_WAY_TASK_PENDING_SUCCESS = 'MARK_WAY_TASK_PENDING_SUCCESS';
export const MARK_WAY_TASK_PENDING_ERROR = 'MARK_WAY_TASK_PENDING_ERROR';
export const SKIP_TASK = 'SKIP_TASK';
export const SELECT_NEXT_WAY_TASK_PROVINCE = 'SELECT_NEXT_WAY_TASK_PROVINCE';

/**
 * action creators
 */
export const fetchWayTask = (id) => ({ type: FETCH_WAY_TASK, id });
export const fetchNextWayTask = () => ({ type: FETCH_NEXT_WAY_TASK });
export const fetchWayTaskSuccess = (id, geoJSON) => ({
  type: FETCH_WAY_TASK_SUCCESS,
  id,
  geoJSON
});
export const fetchWayTaskError = () => ({ type: FETCH_WAY_TASK_ERROR });
export const fetchWayTaskCount = () => ({ type: FETCH_WAY_TASK_COUNT });
export const fetchWayTaskCountSuccess = count => ({ type: FETCH_WAY_TASK_COUNT_SUCCESS, count });
export const fetchWayTaskCountError = count => ({ type: FETCH_WAY_TASK_COUNT_ERROR });
export const markWayTaskPending = () => ({ type: MARK_WAY_TASK_PENDING });
export const markWayTaskPendingSuccess = () => ({ type: MARK_WAY_TASK_PENDING_SUCCESS });
export const markWayTaskPendingError = () => ({ type: MARK_WAY_TASK_PENDING_ERROR });
export const skipTask = id => ({ type: SKIP_TASK, id });
export const selectWayTaskProvince = (provinceId) => ({ type: SELECT_NEXT_WAY_TASK_PROVINCE, selectedProvince: provinceId });


export const fetchNextWayTaskEpic = () => (dispatch, getState) => {
  dispatch(fetchNextWayTask());

  const skipped = getState().waytasks.skipped;
  const selectedProvince = getState().waytasks.selectedProvince;

  let params = {};
  if (skipped.length) params['skip'] = skipped;
  if (selectedProvince) params['province'] = selectedProvince;

  let url = `${config.api}/tasks/next`;
  if (Object.keys(params).length) {
    url = url + '?' + Object.keys(params).map(key => `${key}=${params[key]}`).join('&');
  }

  return fetch(url)
    .then(response => {
      if (response.status === 404) {
        throw new Error('No tasks remaining');
      } else if (response.status >= 400) {
        throw new Error(response.statusText);
      }
      return response.json();
    })
    .then(json => {
      json.data.features.forEach(feature => {
        feature.properties._id = feature.meta.id;
      });
      dispatch(fetchWayTaskCountEpic());
      return dispatch(fetchWayTaskSuccess(json.id, json.data));
    }, e => {
      console.error('Error requesting task', e);
      return dispatch(fetchWayTaskError());
    });
};


export const fetchWayTaskEpic = taskId => (dispatch, getState) => {
  dispatch(fetchWayTask(taskId));

  return fetch(`${config.api}/tasks/${taskId}`)
    .then(response => {
      if (response.status >= 400) {
        throw new Error(response.statusText);
      }
      return response.json();
    })
    .then(({ id, data }) => {
      data.features.forEach(feature => {
        feature.properties._id = feature.meta.id;
      });
      return dispatch(fetchWayTaskSuccess(id, data));
    }, e => {
      console.error('Error reloading task', e);
      return dispatch(fetchWayTaskError());
    });
};


export const fetchWayTaskCountEpic = () => (dispatch, getState) => {
  dispatch(fetchWayTaskCount());
  const selectedProvince = getState().waytasks.selectedProvince;
  const url = selectedProvince ? `${config.api}/tasks/count?province=${selectedProvince}` : `${config.api}/tasks/count`;

  fetch(url)
    .then(response => {
      if (response.status >= 400) {
        throw new Error(response.statusText);
      }
      return response.json();
    })
    .then(({ count }) => {
      dispatch(fetchWayTaskCountSuccess(count));
    }, e => {
      console.error('Error requesting task count', e);
      dispatch(fetchWayTaskCountError());
    });
};


export const markWayTaskPendingEpic = way_ids => dispatch => {
  dispatch(markWayTaskPending());

  return fetch(`${config.api}/tasks/pending`, {
    method: 'PUT',
    body: new Blob([JSON.stringify({ way_ids: way_ids })], { type: 'application/json' })
  })
    .then(response => {
      if (response.status >= 400) {
        console.error('Error marking task as pending', response.statusText);
        dispatch(markWayTaskPendingError());
      }

      dispatch(markWayTaskPendingSuccess());
    });
};


/**
 * reducer
 */
export default (
  state = {
    status: 'complete',
    countStatus: 'complete',
    id: null,
    geoJSON: null,
    skipped: [],
    provinces: [],
    selectedProvince: null
  },
  action
) => {
  if (action.type === FETCH_WAY_TASK) {
    return Object.assign({}, state, {
      status: 'pending'
    });
  } else if (action.type === FETCH_NEXT_WAY_TASK) {
    return Object.assign({}, state, {
      status: 'pending'
    });
  } else if (action.type === FETCH_WAY_TASK_SUCCESS) {
    return Object.assign({}, state, {
      geoJSON: action.geoJSON,
      id: action.id,
      status: 'complete'
    });
  } else if (action.type === FETCH_WAY_TASK_ERROR) {
    return Object.assign({}, state, {
      status: 'error'
    });
  } else if (action.type === FETCH_WAY_TASK_COUNT) {
    return Object.assign({}, state, {
      taskCount: action.count,
      countStatus: 'pending'
    });
  } else if (action.type === FETCH_WAY_TASK_COUNT_SUCCESS) {
    return Object.assign({}, state, {
      taskCount: action.count,
      countStatus: 'complete'
    });
  } else if (action.type === FETCH_WAY_TASK_COUNT_ERROR) {
    return Object.assign({}, state, {
      countStatus: 'error'
    });
  } else if (action.type === SKIP_TASK) {
    return Object.assign({}, state, {
      skipped: state.skipped.concat(action.id)
    });
  } else if (action.type === SELECT_NEXT_WAY_TASK_PROVINCE) {
    return Object.assign({}, state, {
      selectedProvince: action.selectedProvince
    });
  }

  return state;
};

{'fraction_non_alphanumeric': 0.1472387699243278, 'fraction_numerical': 0.002415070037031074, 'mean_word_length': 3.756125574272588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, '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': '4428213', 'n_tokens_mistral': 4188, 'n_tokens_neox': 4147, 'n_words': 792}