<commit_before>import { Map, List, Set } from 'immutable';

const initialAnswers = Map({
  activities: Map(),
  moods: Set(),
  freeWord: Set(),
});

const initialState = Map({
  users: List(),
  currentUser: Map({
    id: null,
    token: '',
    name: '',
    image: null,
    answers: initialAnswers,
  }),
});

const CREATE_USER = 'UserState/CREATE_USER';
const EDIT_USER = 'UserState/EDIT_USER';
const REMOVE_USER = 'UserState/REMOVE_USER';
const RESET_CURRENT_USER = 'UserState/RESET_CURRENT_USER';
const SET_CURRENT_USER = 'UserState/SET_CURRENT_USER';
const ADD_ACTIVITY = 'UserState/ADD_ACTIVITY';
const DELETE_ACTIVITY = 'UserState/DELETE_ACTIVITY';
const ADD_MOOD = 'UserState/ADD_MOOD';
const DELETE_MOOD = 'UserState/DELETE_MOOD';
const ADD_FREEWORD = 'UserState/ADD_FREEWORD';

export function createUser(newUser) {
  return {
    type: CREATE_USER,
    payload: Map({
      id: newUser.get('id'),
      name: newUser.get('name'),
      token: newUser.get('token'),
      image: newUser.get('image'),
      answers: initialAnswers,
    }),
  };
}

export function editUser(user) {
  return {
    type: EDIT_USER,
    payload: {
      id: user.get('id'),
      values: Map({
        id: user.get('id'),
        name: user.get('name'),
        token: user.get('token'),
        image: user.get('image'),
        answers: initialAnswers,
      }),
    },
  };
}

export function removeUser(id) {
  return {
    type: REMOVE_USER,
    payload: id,
  };
}

export function resetCurrentUser() {
  return {
    type: RESET_CURRENT_USER,
  };
}

export function setCurrentUser(user) {
  console.log(user.get('image'));
  return {
    type: SET_CURRENT_USER,
    payload: user,
  };
}

export function addActivity(activity) {
  return {
    type: ADD_ACTIVITY,
    payload: activity,
  };
}

export function deleteActivity(activity) {
  return {
    type: DELETE_ACTIVITY,
    payload: activity,
  };
}

export function addMood(mood) {
  return {
    type: ADD_MOOD,
    payload: mood,
  };
}

export function deleteMood(mood) {
  return {
    type: DELETE_MOOD,
    payload: mood,
  };
}

export function addFreeWord(freeWord) {
  return {
    type: ADD_FREEWORD,
    payload: freeWord,
  };
}

function usersReducer(state = List(), action) {
  switch (action.type) {
    case CREATE_USER:
      return state.update(list => list.push(action.payload));

    case EDIT_USER:
      return state.set(action.payload.id, action.payload.values);

    case REMOVE_USER:
      return state.filter(user => user.get('id') !== action.payload);

    default:
      return state;
  }
}

function currentUserReducer(state = Map(), action, wholeState) {
  switch (action.type) {
    case RESET_CURRENT_USER:
      return initialState.get('currentUser');

    case SET_CURRENT_USER:
      return state
        .set('name', action.payload.get('name'))
        .set('image', action.payload.get('image'))
        .set('token', action.payload.get('token'))
        .set('id', action.payload.get('id'));

    case ADD_ACTIVITY:
      return state.setIn(
        ['answers', 'activities', action.payload.main, action.payload.sub],
        action.payload.thumb,
      );

    case DELETE_ACTIVITY:
      return state.deleteIn([
        'answers',
        'activities',
        action.payload.main,
        action.payload.sub,
      ]);
<commit_msg>Delete main activity when its last sub activity is deleted<commit_after>import { Map, List, Set } from 'immutable';

const initialAnswers = Map({
  activities: Map(),
  moods: Set(),
  freeWord: Set(),
});

const initialState = Map({
  users: List(),
  currentUser: Map({
    id: null,
    token: '',
    name: '',
    image: null,
    answers: initialAnswers,
  }),
});

const CREATE_USER = 'UserState/CREATE_USER';
const EDIT_USER = 'UserState/EDIT_USER';
const REMOVE_USER = 'UserState/REMOVE_USER';
const RESET_CURRENT_USER = 'UserState/RESET_CURRENT_USER';
const SET_CURRENT_USER = 'UserState/SET_CURRENT_USER';
const ADD_ACTIVITY = 'UserState/ADD_ACTIVITY';
const DELETE_ACTIVITY = 'UserState/DELETE_ACTIVITY';
const ADD_MOOD = 'UserState/ADD_MOOD';
const DELETE_MOOD = 'UserState/DELETE_MOOD';
const ADD_FREEWORD = 'UserState/ADD_FREEWORD';

export function createUser(newUser) {
  return {
    type: CREATE_USER,
    payload: Map({
      id: newUser.get('id'),
      name: newUser.get('name'),
      token: newUser.get('token'),
      image: newUser.get('image'),
      answers: initialAnswers,
    }),
  };
}

export function editUser(user) {
  return {
    type: EDIT_USER,
    payload: {
      id: user.get('id'),
      values: Map({
        id: user.get('id'),
        name: user.get('name'),
        token: user.get('token'),
        image: user.get('image'),
        answers: initialAnswers,
      }),
    },
  };
}

export function removeUser(id) {
  return {
    type: REMOVE_USER,
    payload: id,
  };
}

export function resetCurrentUser() {
  return {
    type: RESET_CURRENT_USER,
  };
}

export function setCurrentUser(user) {
  console.log(user.get('image'));
  return {
    type: SET_CURRENT_USER,
    payload: user,
  };
}

export function addActivity(activity) {
  return {
    type: ADD_ACTIVITY,
    payload: activity,
  };
}

export function deleteActivity(activity) {
  return {
    type: DELETE_ACTIVITY,
    payload: activity,
  };
}

export function addMood(mood) {
  return {
    type: ADD_MOOD,
    payload: mood,
  };
}

export function deleteMood(mood) {
  return {
    type: DELETE_MOOD,
    payload: mood,
  };
}

export function addFreeWord(freeWord) {
  return {
    type: ADD_FREEWORD,
    payload: freeWord,
  };
}

function usersReducer(state = List(), action) {
  switch (action.type) {
    case CREATE_USER:
      return state.update(list => list.push(action.payload));

    case EDIT_USER:
      return state.set(action.payload.id, action.payload.values);

    case REMOVE_USER:
      return state.filter(user => user.get('id') !== action.payload);

    default:
      return state;
  }
}

function currentUserReducer(state = Map(), action, wholeState) {
  switch (action.type) {
    case RESET_CURRENT_USER:
      return initialState.get('currentUser');

    case SET_CURRENT_USER:
      return state
        .set('name', action.payload.get('name'))
        .set('image', action.payload.get('image'))
        .set('token', action.payload.get('token'))
        .set('id', action.payload.get('id'));

    case ADD_ACTIVITY:
      return state.setIn(
        ['answers', 'activities', action.payload.main, action.payload.sub],
        action.payload.thumb,
      );

    case DELETE_ACTIVITY:
      const subs = state.getIn(['answers', 'activities', action.payload.main]);

      if (subs.size === 1) {
        return state.deleteIn(['answers', 'activities', action.payload.main]);
      }

      return state.deleteIn([
        'answers',
        'activities',
        action.payload.main,
        action.payload.sub,
      ]);

{'fraction_non_alphanumeric': 0.14037375054324208, 'fraction_numerical': 0.00014486455164421265, 'mean_word_length': 3.1217910447761192, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4589236', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2276, 'n_words': 475}