<commit_before>// @flow

import moment from 'moment-timezone';
import { createSelector } from 'reselect';
import { Event } from '../actions/ActionTypes';
import { mutateComments } from 'app/reducers/comments';
import createEntityReducer from 'app/utils/createEntityReducer';
import joinReducers from 'app/utils/joinReducers';
import { normalize } from 'normalizr';
import { eventSchema } from 'app/reducers';
import mergeObjects from 'app/utils/mergeObjects';
import { groupBy } from 'lodash';

export type EventEntity = {
  id: number,
  name: string,
  comments: Array<number>
};

function mutateEvent(state: any, action: any) {
  switch (action.type) {
    case Event.DELETE.SUCCESS: {
      return {
        ...state,
        items: state.items.filter(id => id !== action.meta.id)
      };
    }
    case Event.SOCKET_EVENT_UPDATED: {
      const events = normalize(action.payload, eventSchema).entities.events;
      return {
        ...state,
        byId: mergeObjects(state.byId, events)
      };
    }
    case Event.CLEAR: {
      return {
        ...state,
        items: [],
        pagination: {}
      };
    }
    case Event.REGISTER.BEGIN: {
      return {
        ...state,
        byId: {
          ...state.byId,
          [action.meta.id]: {
            ...state.byId[action.meta.id],
            loading: true
          }
        }
      };
    }
    case Event.SOCKET_REGISTRATION.SUCCESS: {
      const eventId = action.meta.eventId;
      const registration = action.payload;
      const stateEvent = state.byId[eventId];
      if (!stateEvent) {
        return state;
      }
      let waitingRegistrations = stateEvent.waitingRegistrations || [];
      if (!registration.pool) {
        waitingRegistrations = [...waitingRegistrations, registration.id];
      }
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...stateEvent,
            loading: false,
            registrationCount: registration.pool
              ? stateEvent.registrationCount + 1
              : stateEvent.registrationCount,
            waitingRegistrations
          }
        }
      };
    }
    case Event.SOCKET_UNREGISTRATION.SUCCESS: {
      const { eventId, activationTime, fromPool } = action.meta;
      const stateEvent = state.byId[eventId];
      if (!stateEvent) {
        return state;
      }
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...stateEvent,
            loading: false,
            activationTime,
            registrationCount: fromPool
              ? stateEvent.registrationCount - 1
              : stateEvent.registrationCount,
            waitingRegistrations: (stateEvent.waitingRegistrations || []
            ).filter(id => id !== action.payload.id)
          }
        }
      };
    }
    case Event.SOCKET_REGISTRATION.FAILURE: {
      const { eventId } = action.meta;
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...state.byId[eventId],
            loading: false
          }
        }
      };
    }
    case Event.REGISTER.FAILURE: {
      return {
        ...state,
        byId: {
          ...state.byId,
          [action.meta.id]: {
            ...state.byId[action.meta.id],
            loading: false
          }
        }
      };
    }
    case Event.FOLLOW.SUCCESS: {
      const eventId = action.payload.target;
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...state.byId[eventId],
            isUserFollowing: action.payload
          }
        }
      };
    }
    case Event.UNFOLLOW.SUCCESS: {
      const eventId = action.meta.eventId;
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...state.byId[eventId],
            isUserFollowing: undefined
          }
        }
      };
    }
    case Event.IS_USER_FOLLOWING.SUCCESS: {
      // NOTE: assume we've only asked for a single event.
      if (action.payload.length > 0) {
        const eventId = action.payload[0].target;
        return {
          ...state,
          byId: {
            ...state.byId,
            [eventId]: {
              ...state.byId[eventId],
              isUserFollowing: action.payload[0]
            }
          }
        };
      } else {
        // leave undefined to be false.
        return state;
      }
    }
    default:
      return state;
  }
}

const mutate = joinReducers(mutateComments('events'), mutateEvent);

export default createEntityReducer({
  key: 'events',
  types: {
    fetch: Event.FETCH
  },
  mutate
});

function transformEvent(event) {
  return {
    ...event,
    startTime: moment(event.startTime),
    endTime: moment(event.endTime),
    mergeTime: moment(event.mergeTime)
  };
}

function transformRegistration(registration) {
  return {
    ...registration,
    registrationDate: moment(registration.registrationDate),
    unregistrationDate: moment(registration.unregistrationDate)
  };
}

export const selectEvents = createSelector(
  state => state.events.byId,
  state => state.events.items,
  (eventsById, eventIds) => eventIds.map(id => transformEvent(eventsById[id]))
);

export const selectSortedEvents = createSelector(selectEvents, events =>
  events.sort((a, b) => a.startTime - b.startTime)
);<commit_msg>Fix merge time being an invalid moment object<commit_after>// @flow

import moment from 'moment-timezone';
import { createSelector } from 'reselect';
import { Event } from '../actions/ActionTypes';
import { mutateComments } from 'app/reducers/comments';
import createEntityReducer from 'app/utils/createEntityReducer';
import joinReducers from 'app/utils/joinReducers';
import { normalize } from 'normalizr';
import { eventSchema } from 'app/reducers';
import mergeObjects from 'app/utils/mergeObjects';
import { groupBy } from 'lodash';

export type EventEntity = {
  id: number,
  name: string,
  comments: Array<number>
};

function mutateEvent(state: any, action: any) {
  switch (action.type) {
    case Event.DELETE.SUCCESS: {
      return {
        ...state,
        items: state.items.filter(id => id !== action.meta.id)
      };
    }
    case Event.SOCKET_EVENT_UPDATED: {
      const events = normalize(action.payload, eventSchema).entities.events;
      return {
        ...state,
        byId: mergeObjects(state.byId, events)
      };
    }
    case Event.CLEAR: {
      return {
        ...state,
        items: [],
        pagination: {}
      };
    }
    case Event.REGISTER.BEGIN: {
      return {
        ...state,
        byId: {
          ...state.byId,
          [action.meta.id]: {
            ...state.byId[action.meta.id],
            loading: true
          }
        }
      };
    }
    case Event.SOCKET_REGISTRATION.SUCCESS: {
      const eventId = action.meta.eventId;
      const registration = action.payload;
      const stateEvent = state.byId[eventId];
      if (!stateEvent) {
        return state;
      }
      let waitingRegistrations = stateEvent.waitingRegistrations || [];
      if (!registration.pool) {
        waitingRegistrations = [...waitingRegistrations, registration.id];
      }
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...stateEvent,
            loading: false,
            registrationCount: registration.pool
              ? stateEvent.registrationCount + 1
              : stateEvent.registrationCount,
            waitingRegistrations
          }
        }
      };
    }
    case Event.SOCKET_UNREGISTRATION.SUCCESS: {
      const { eventId, activationTime, fromPool } = action.meta;
      const stateEvent = state.byId[eventId];
      if (!stateEvent) {
        return state;
      }
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...stateEvent,
            loading: false,
            activationTime,
            registrationCount: fromPool
              ? stateEvent.registrationCount - 1
              : stateEvent.registrationCount,
            waitingRegistrations: (stateEvent.waitingRegistrations || []
            ).filter(id => id !== action.payload.id)
          }
        }
      };
    }
    case Event.SOCKET_REGISTRATION.FAILURE: {
      const { eventId } = action.meta;
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...state.byId[eventId],
            loading: false
          }
        }
      };
    }
    case Event.REGISTER.FAILURE: {
      return {
        ...state,
        byId: {
          ...state.byId,
          [action.meta.id]: {
            ...state.byId[action.meta.id],
            loading: false
          }
        }
      };
    }
    case Event.FOLLOW.SUCCESS: {
      const eventId = action.payload.target;
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...state.byId[eventId],
            isUserFollowing: action.payload
          }
        }
      };
    }
    case Event.UNFOLLOW.SUCCESS: {
      const eventId = action.meta.eventId;
      return {
        ...state,
        byId: {
          ...state.byId,
          [eventId]: {
            ...state.byId[eventId],
            isUserFollowing: undefined
          }
        }
      };
    }
    case Event.IS_USER_FOLLOWING.SUCCESS: {
      // NOTE: assume we've only asked for a single event.
      if (action.payload.length > 0) {
        const eventId = action.payload[0].target;
        return {
          ...state,
          byId: {
            ...state.byId,
            [eventId]: {
              ...state.byId[eventId],
              isUserFollowing: action.payload[0]
            }
          }
        };
      } else {
        // leave undefined to be false.
        return state;
      }
    }
    default:
      return state;
  }
}

const mutate = joinReducers(mutateComments('events'), mutateEvent);

export default createEntityReducer({
  key: 'events',
  types: {
    fetch: Event.FETCH
  },
  mutate
});

function transformEvent(event) {
  return {
    ...event,
    startTime: moment(event.startTime),
    endTime: moment(event.endTime),
    mergeTime: event.mergeTime && moment(event.mergeTime)
  };
}

function transformRegistration(registration) {
  return {
    ...registration,
    registrationDate: moment(registration.registrationDate),
    unregistrationDate: moment(registration.unregistrationDate)
  };
}

export const selectEvents = createSelector(
  state => state.events.byId,
  state => state.events.items,
  (eventsById, eventIds) => eventIds.map(id => transformEvent(eventsById[id]))
);

export const selectSortedEvents = createSelector(selectEvents, events =>
  events.sort((a, b) => a.startTime - b.startTime)
);
{'fraction_non_alphanumeric': 0.12353754030400738, 'fraction_numerical': 0.0009212344541685859, 'mean_word_length': 1.7462686567164178, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '1421309', 'n_tokens_mistral': 3195, 'n_tokens_neox': 3140, 'n_words': 608}