<commit_before>import _ from 'lodash';
import merge from 'lodash/fp/merge';
import set from 'lodash/fp/set';

import { folderUrl } from '../utils/helpers';

import {
  CREATE_FOLDER_SUCCESS,
  DELETE_COMPOSE_MESSAGE,
  DELETE_FOLDER_SUCCESS,
  DELETE_MESSAGE_SUCCESS,
  FETCH_FOLDER_FAILURE,
  FETCH_FOLDER_SUCCESS,
  FETCH_FOLDERS_SUCCESS,
  LOADING_FOLDER,
  MOVE_MESSAGE_SUCCESS,
  RESET_REDIRECT,
  SAVE_DRAFT_SUCCESS,
  SEND_MESSAGE_SUCCESS,
  SET_CURRENT_FOLDER,
  TOGGLE_FOLDER_MOVE_TO,
  TOGGLE_FOLDER_NAV,
  TOGGLE_MANAGED_FOLDERS
} from '../utils/constants';

const initialState = {
  data: {
    currentItem: {
      attributes: {},
      filter: {},
      messages: [],
      pagination: {
        currentPage: 0,
        perPage: 0,
        totalEntries: 0,
        totalPages: 0
      },
      persistFolder: 0,
      sort: {
        value: 'sentDate',
        order: 'DESC'
      }
    },
    items: new Map()
  },
  ui: {
    loading: {
      inProgress: false,
      request: null
    },
    moveToId: null,
    nav: {
      foldersExpanded: false,
      visible: false
    },
    redirect: null
  }
};

const folderKey = (folderName) => _.kebabCase(folderName);

export default function folders(state = initialState, action) {
  switch (action.type) {
    // TODO: Handle the response in an appropriate way
    case CREATE_FOLDER_SUCCESS: {
      const folder = action.folder;
      const newFolders = new Map(state.data.items);
      newFolders.set(folderKey(folder.name), folder);
      return set('data.items', newFolders, state);
    }

    case DELETE_FOLDER_SUCCESS: {
      const folder = action.folder;
      const newFolders = new Map(state.data.items);
      newFolders.delete(folderKey(folder.name));
      return set('data.items', newFolders, state);
    }

    case FETCH_FOLDER_FAILURE:
      return set('ui.loading.inProgress', false, state);

    case FETCH_FOLDER_SUCCESS: {
      const attributes = action.folder.data.attributes;
      const messages = action.messages.data.map(message => message.attributes);
      const persistFolder = action.folder.data.attributes.folderId;

      const meta = action.messages.meta;
      const filter = meta.filter;
      const pagination = meta.pagination;
      const sort = meta.sort;
      const sortValue = Object.keys(sort)[0];
      const sortOrder = sort[sortValue];

      const newState = set('data.currentItem', {
        attributes,
        filter,
        messages,
        pagination,
        persistFolder,
        sort: {
          value: sortValue,
          order: sortOrder
        },
      }, state);

      return set('ui', merge(initialState.ui, {
        loading: {
          request: _.get(newState, 'ui.loading.request')
        },
        nav: {
          foldersExpanded: _.get(newState, 'ui.nav.foldersExpanded')
        }
      }), newState);
    }<commit_msg>Default values for _.get of persisted values after fetch folder success.<commit_after>import _ from 'lodash';
import merge from 'lodash/fp/merge';
import set from 'lodash/fp/set';

import { folderUrl } from '../utils/helpers';

import {
  CREATE_FOLDER_SUCCESS,
  DELETE_COMPOSE_MESSAGE,
  DELETE_FOLDER_SUCCESS,
  DELETE_MESSAGE_SUCCESS,
  FETCH_FOLDER_FAILURE,
  FETCH_FOLDER_SUCCESS,
  FETCH_FOLDERS_SUCCESS,
  LOADING_FOLDER,
  MOVE_MESSAGE_SUCCESS,
  RESET_REDIRECT,
  SAVE_DRAFT_SUCCESS,
  SEND_MESSAGE_SUCCESS,
  SET_CURRENT_FOLDER,
  TOGGLE_FOLDER_MOVE_TO,
  TOGGLE_FOLDER_NAV,
  TOGGLE_MANAGED_FOLDERS
} from '../utils/constants';

const initialState = {
  data: {
    currentItem: {
      attributes: {},
      filter: {},
      messages: [],
      pagination: {
        currentPage: 0,
        perPage: 0,
        totalEntries: 0,
        totalPages: 0
      },
      persistFolder: 0,
      sort: {
        value: 'sentDate',
        order: 'DESC'
      }
    },
    items: new Map()
  },
  ui: {
    loading: {
      inProgress: false,
      request: null
    },
    moveToId: null,
    nav: {
      foldersExpanded: false,
      visible: false
    },
    redirect: null
  }
};

const folderKey = (folderName) => _.kebabCase(folderName);

export default function folders(state = initialState, action) {
  switch (action.type) {
    // TODO: Handle the response in an appropriate way
    case CREATE_FOLDER_SUCCESS: {
      const folder = action.folder;
      const newFolders = new Map(state.data.items);
      newFolders.set(folderKey(folder.name), folder);
      return set('data.items', newFolders, state);
    }

    case DELETE_FOLDER_SUCCESS: {
      const folder = action.folder;
      const newFolders = new Map(state.data.items);
      newFolders.delete(folderKey(folder.name));
      return set('data.items', newFolders, state);
    }

    case FETCH_FOLDER_FAILURE:
      return set('ui.loading.inProgress', false, state);

    case FETCH_FOLDER_SUCCESS: {
      const attributes = action.folder.data.attributes;
      const messages = action.messages.data.map(message => message.attributes);
      const persistFolder = action.folder.data.attributes.folderId;

      const meta = action.messages.meta;
      const filter = meta.filter;
      const pagination = meta.pagination;
      const sort = meta.sort;
      const sortValue = Object.keys(sort)[0];
      const sortOrder = sort[sortValue];

      const newState = set('data.currentItem', {
        attributes,
        filter,
        messages,
        pagination,
        persistFolder,
        sort: {
          value: sortValue,
          order: sortOrder
        },
      }, state);

      return set('ui', merge(initialState.ui, {
        loading: {
          request: _.get(newState, 'ui.loading.request', null)
        },
        nav: {
          foldersExpanded: _.get(newState, 'ui.nav.foldersExpanded', false)
        }
      }), newState);
    }
{'fraction_non_alphanumeric': 0.10900310451879959, 'fraction_numerical': 0.002069679199724043, 'mean_word_length': 2.5774213448488585, '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': '6523356', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1852, 'n_words': 384}