<commit_before>import { select, call, put, takeLatest, takeEvery } from 'redux-saga/effects';
import { createReducer, union } from '@/utils/reducers.utils';
import Api from '@/services/api';
import { getArtistsSection } from './selectors';

// Actions
const RESET_STATE = 'artists/RESET_STATE';
const SELECT_GENRE = 'artists/SELECT_GENRE';
const SET_ARTISTS = 'artists/SET_ARTISTS';
const ADD_ARTISTS = 'artists/ADD_ARTISTS';
const INCREMENT_OFFSET = 'artists/INCREMENT_OFFSET';
const LOADING = 'artists/LOADING';

// @effect Actions
const FETCH_ARTISTS_REQUEST = '@effect/artists/FETCH_ARTISTS_REQUEST';
const FETCH_MORE_ARTIST = '@effect/artists/FETCH_MORE_ARTIST';
const FETCH_ARTISTS_FAILURE = '@effect/artists/FETCH_ARTISTS_FAILURE';

// Initial State
const INIT_STATE = {
  offset: 0,
  count: 0,
  genre: '',
  limit: 20,
  list: [],
  loading: false,
};

// Reducers
export default createReducer(INIT_STATE, {

  [RESET_STATE](state) {
    return {
      ...state,
      ...INIT_STATE,
    };
  },

  [SET_ARTISTS](state, payload) {
    return {
      ...state,
      list: payload,
      count: payload.length,
    };
  },

  [ADD_ARTISTS](state, payload) {
    const list = union(state.list, payload);
    return {
      ...state,
      list,
      count: list.length,
    };
  },

  [LOADING](state, payload) {
    return {
      ...state,
      loading: payload,
    };
  },

  [INCREMENT_OFFSET](state) {
    return {
      ...state,
      offset: state.offset + state.limit,
    };
  },

  [SELECT_GENRE](state, payload) {
    return {
      ...state,
      genre: payload,
    };
  },

});

// Action Creators
export const resetState = () => ({
  type: RESET_STATE,
});

export const addArtists = ({ entities, result }) => ({
  type: ADD_ARTISTS,
  entities,
  payload: result,
});

// TODO: reuse action creators
export const setArtists = ({ entities, result }) => ({
  type: SET_ARTISTS,
  entities,
  payload: result,
});

export const incrementOffset = () => ({
  type: INCREMENT_OFFSET,
});

export const setLoading = loading => ({
  type: LOADING,
  payload: loading,
});

export const selectGenre = genre => ({
  type: SELECT_GENRE,
  payload: genre,
});

export const fetchAllArtists = genre => ({
  type: FETCH_ARTISTS_REQUEST,
  payload: genre,
});

export const fetchMoreArtists = () => ({
  type: FETCH_MORE_ARTIST,
});

export const throwError = error => ({
  type: FETCH_ARTISTS_FAILURE,
  payload: error,
});

// Sagas
function* fetchArtists({ genre, offset }, artistAction) {
  const artists = yield call(Api.getAllArtists, genre, offset);
  yield put(selectGenre(genre));
  yield put(artistAction(artists));
  yield put(incrementOffset());
}

function* loadAllArtists({ payload: genre }) {
  const { count } = yield select(getArtistsSection);
  if (!count) {
    yield put(resetState());
    yield put(setLoading(true));
    try {
      const { offset } = yield select(getArtistsSection);
      yield call(fetchArtists, { genre, offset }, addArtists);
    } catch ({ status }) {
      yield put(throwError(status));
    }
    yield put(setLoading(false));
  }
}

function* loadMoreArtists() {
  yield put(setLoading(true));
  try {
    const { genre, offset } = yield select(getArtistsSection);
    yield call(fetchArtists, { genre, offset }, addArtists);
  } catch ({ status }) {
    yield put(throwError(status));
  }
  yield put(setLoading(false));
}

export function* watchLoadArtists() {
  yield takeLatest(FETCH_ARTISTS_REQUEST, loadAllArtists);
  yield takeEvery(FETCH_MORE_ARTIST, loadMoreArtists);
}<commit_msg>fix(#14) local cache disabled<commit_after>import { select, call, put, takeLatest, takeEvery } from 'redux-saga/effects';
import { createReducer, union } from '@/utils/reducers.utils';
import Api from '@/services/api';
import { getArtistsSection } from './selectors';

// Actions
const RESET_STATE = 'artists/RESET_STATE';
const SELECT_GENRE = 'artists/SELECT_GENRE';
const SET_ARTISTS = 'artists/SET_ARTISTS';
const ADD_ARTISTS = 'artists/ADD_ARTISTS';
const INCREMENT_OFFSET = 'artists/INCREMENT_OFFSET';
const LOADING = 'artists/LOADING';

// @effect Actions
const FETCH_ARTISTS_REQUEST = '@effect/artists/FETCH_ARTISTS_REQUEST';
const FETCH_MORE_ARTIST = '@effect/artists/FETCH_MORE_ARTIST';
const FETCH_ARTISTS_FAILURE = '@effect/artists/FETCH_ARTISTS_FAILURE';

// Initial State
const INIT_STATE = {
  offset: 0,
  count: 0,
  genre: '',
  limit: 20,
  list: [],
  loading: false,
};

// Reducers
export default createReducer(INIT_STATE, {

  [RESET_STATE](state) {
    return {
      ...state,
      ...INIT_STATE,
    };
  },

  [SET_ARTISTS](state, payload) {
    return {
      ...state,
      list: payload,
      count: payload.length,
    };
  },

  [ADD_ARTISTS](state, payload) {
    const list = union(state.list, payload);
    return {
      ...state,
      list,
      count: list.length,
    };
  },

  [LOADING](state, payload) {
    return {
      ...state,
      loading: payload,
    };
  },

  [INCREMENT_OFFSET](state) {
    return {
      ...state,
      offset: state.offset + state.limit,
    };
  },

  [SELECT_GENRE](state, payload) {
    return {
      ...state,
      genre: payload,
    };
  },

});

// Action Creators
export const resetState = () => ({
  type: RESET_STATE,
});

export const addArtists = ({ entities, result }) => ({
  type: ADD_ARTISTS,
  entities,
  payload: result,
});

// TODO: reuse action creators
export const setArtists = ({ entities, result }) => ({
  type: SET_ARTISTS,
  entities,
  payload: result,
});

export const incrementOffset = () => ({
  type: INCREMENT_OFFSET,
});

export const setLoading = loading => ({
  type: LOADING,
  payload: loading,
});

export const selectGenre = genre => ({
  type: SELECT_GENRE,
  payload: genre,
});

export const fetchAllArtists = genre => ({
  type: FETCH_ARTISTS_REQUEST,
  payload: genre,
});

export const fetchMoreArtists = () => ({
  type: FETCH_MORE_ARTIST,
});

export const throwError = error => ({
  type: FETCH_ARTISTS_FAILURE,
  payload: error,
});

// Sagas
function* fetchArtists({ genre, offset }, artistAction) {
  const artists = yield call(Api.getAllArtists, genre, offset);
  yield put(selectGenre(genre));
  yield put(artistAction(artists));
  yield put(incrementOffset());
}

function* loadAllArtists({ payload: genre }) {
  yield put(resetState());
  yield put(setLoading(true));
  try {
    const { offset } = yield select(getArtistsSection);
    yield call(fetchArtists, { genre, offset }, addArtists);
  } catch ({ status }) {
    yield put(throwError(status));
  }
  yield put(setLoading(false));
}

function* loadMoreArtists() {
  yield put(setLoading(true));
  try {
    const { genre, offset } = yield select(getArtistsSection);
    yield call(fetchArtists, { genre, offset }, addArtists);
  } catch ({ status }) {
    yield put(throwError(status));
  }
  yield put(setLoading(false));
}

export function* watchLoadArtists() {
  yield takeLatest(FETCH_ARTISTS_REQUEST, loadAllArtists);
  yield takeEvery(FETCH_MORE_ARTIST, loadMoreArtists);
}
{'fraction_non_alphanumeric': 0.14511310285958173, 'fraction_numerical': 0.0014226774790155072, 'mean_word_length': 3.6587143803843607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3208512', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2342, 'n_words': 519}