<commit_before>import fetch from 'isomorphic-fetch';
import config from '../configuration';
import * as actionTypes from '../constants/actionTypes'

export function loadSinger(singer, orgId) {
  return { type: actionTypes.SINGER_ADDED, payload: { singer, orgId }};
}

export function loadSingers(singers, orgId) {
  return { type: actionTypes.SINGERS_LOADED, payload: { singers, orgId }};
}

export function failAddSinger(error) {
  return { type: actionTypes.ADD_SINGER_FAILED, payload: { error }};
}

export function clearAddSingerFailed() {
  return { type: actionTypes.ADD_SINGER_FAILED_CLEARED };
}

export function singerDeletedFromOrganization({ singerId, orgId }) {
  return { type: actionTypes.SINGER_DELETED_FROM_ORGANIZATION, payload: { singerId, orgId } };
}

export function failDeleteSingerFromOrganization(error) {
  return { type: actionTypes.DELETE_SINGER_FROM_ORGANIZATION_FAILED };
}

export function addSinger(token, singer, orgId) {
  return async dispatch => {
    let response = await fetch(config.baseApiUrl + '/organizations/' + orgId + '/singers', {
      method: 'POST',
      headers: {
        'Authorization': 'jwt ' + token,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        'name': singer.name,
        'height': singer.height,
        'voice': singer.voice,
        'gender': singer.gender
      })
    });
    let json = await response.json();
    if(response.status === 201) {
      dispatch(loadSinger(json));
    } else {
      dispatch(failAddSinger(json.error));
    }
  }
}

export function deleteSingerFromOrganization(token, orgId, singerId) {
  return async dispatch => {
    let response = await fetch(config.baseApiUrl + '/organizations/' + orgId + '/singers/' + singerId, {
      method: 'DELETE',
      headers: {
        'Authorization': 'jwt ' + token,
        'Content-Type': 'application/json'
      }
    });
    let json = await response.json();
    if(response.status === 204) {
      dispatch(singerDeletedFromOrganization({ singerId, orgId }));
    } else {
      dispatch(failDeleteSingerFromOrganization(json.error));
    }
  }
}

function requestSingers() {
  return { type: actionTypes.SINGERS_REQUESTED };
}

function receiveSingers(orgId, choirId, singers) {
  return { type: actionTypes.SINGERS_RECEIVED, payload: { orgId, choirId, singers } };
}

function fetchSingers(token, orgId, choirId) {
  return async dispatch => {
    dispatch(requestSingers());
    let response = await fetch(config.baseApiUrl + '/organizations/' + orgId + '/choirs/' + choirId + '/singers', {<commit_msg>Fix delete singer from organization<commit_after>import fetch from 'isomorphic-fetch';
import config from '../configuration';
import * as actionTypes from '../constants/actionTypes'

export function loadSinger(singer, orgId) {
  return { type: actionTypes.SINGER_ADDED, payload: { singer, orgId }};
}

export function loadSingers(singers, orgId) {
  return { type: actionTypes.SINGERS_LOADED, payload: { singers, orgId }};
}

export function failAddSinger(error) {
  return { type: actionTypes.ADD_SINGER_FAILED, payload: { error }};
}

export function clearAddSingerFailed() {
  return { type: actionTypes.ADD_SINGER_FAILED_CLEARED };
}

export function singerDeletedFromOrganization({ singerId, orgId }) {
  return { type: actionTypes.SINGER_DELETED_FROM_ORGANIZATION, payload: { singerId, orgId } };
}

export function failDeleteSingerFromOrganization(error) {
  return { type: actionTypes.DELETE_SINGER_FROM_ORGANIZATION_FAILED };
}

export function addSinger(token, singer, orgId) {
  return async dispatch => {
    let response = await fetch(config.baseApiUrl + '/organizations/' + orgId + '/singers', {
      method: 'POST',
      headers: {
        'Authorization': 'jwt ' + token,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        'name': singer.name,
        'height': singer.height,
        'voice': singer.voice,
        'gender': singer.gender
      })
    });
    let json = await response.json();
    if(response.status === 201) {
      dispatch(loadSinger(json));
    } else {
      dispatch(failAddSinger(json.error));
    }
  }
}

export function deleteSingerFromOrganization(token, orgId, singerId) {
  return async dispatch => {
    let response = await fetch(config.baseApiUrl + '/organizations/' + orgId + '/singers/' + singerId, {
      method: 'DELETE',
      headers: {
        'Authorization': 'jwt ' + token,
        'Content-Type': 'application/json'
      }
    });
    if(response.status === 204) {
      dispatch(singerDeletedFromOrganization({ singerId, orgId }));
    } else {
      dispatch(failDeleteSingerFromOrganization());
    }
  }
}

function requestSingers() {
  return { type: actionTypes.SINGERS_REQUESTED };
}

function receiveSingers(orgId, choirId, singers) {
  return { type: actionTypes.SINGERS_RECEIVED, payload: { orgId, choirId, singers } };
}

function fetchSingers(token, orgId, choirId) {
  return async dispatch => {
    dispatch(requestSingers());
    let response = await fetch(config.baseApiUrl + '/organizations/' + orgId + '/choirs/' + choirId + '/singers', {
{'fraction_non_alphanumeric': 0.13063763608087092, 'fraction_numerical': 0.0023328149300155523, 'mean_word_length': 3.755083179297597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2322100', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1568, 'n_words': 348}