<commit_before>import reject from 'lodash/reject'

const newDefaultUser = {
  name: '',
  password: '',
  roles: [],
  permissions: [],
  isNew: true,
}

const initialState = {
  users: [],
  roles: [],
  ephemeral: {
    editingUser: {},
  },
  queries: [],
  queryIDToKill: null,
}

export default function admin(state = initialState, action) {
  switch (action.type) {
    case 'LOAD_USERS': {
      return {...state, ...action.payload}
    }

    case 'LOAD_ROLES': {
      return {...state, ...action.payload}
    }

    case 'ADD_USER': {
      const newUser = {...newDefaultUser, isEditing: true}
      return {
        ...state,
        users: [
          newUser,
          ...state.users,
        ],
      }
    }

    case 'SET_EDITING_MODE': {
      const newState = {...state}
      newState.ephemeral.isEditing = action.payload.isEditing
      return newState
    }

    case 'RESET_EDITING_USER': {
      return {...state, ephemeral: {...state.ephemeral, editingUser: {...newDefaultUser}}}
    }

    case 'EDIT_USER': {
      const {user, updates} = action.payload
      const newState = {
        users: state.users.map(u => {
          const output = u.name === user.name ? {...u, ...updates} : u
          return output
        }),
      }
      return {...state, ...newState}
    }

    case 'CLEAR_EDITING_MODE': {
      const newState = {
        users: state.users.map(u => {
          u.isEditing = false
          return u
        }),
      }
      return {...state, ...newState}
    }

    // case 'CREATE_USER': {
    //   const {user, source} = action.payload
    //   const userLink = `${this.props.source.links.users}/${user.name}`
    //   Object.assign(user, {links: {self: userLink}})
    // }

    case 'DELETE_ROLE': {
      const {role} = action.payload
      const newState = {
        roles: state.roles.filter(r => r.name !== role.name),
      }

      return {...state, ...newState}
    }

    case 'DELETE_USER': {
      const {user} = action.payload
      const newState = {
        users: state.users.filter(u => u.name !== user.name),
      }

      return {...state, ...newState}
    }

    case 'LOAD_QUERIES': {
      return {...state, ...action.payload}
    }

    case 'FILTER_ROLES': {<commit_msg>Fix deleting and editing existing users on edit with same name by using links.self as unique key<commit_after>import reject from 'lodash/reject'

const newDefaultUser = {
  name: '',
  password: '',
  roles: [],
  permissions: [],
  links: {self: ''},
  isNew: true,
}

const initialState = {
  users: [],
  roles: [],
  ephemeral: {
    editingUser: {},
  },
  queries: [],
  queryIDToKill: null,
}

export default function admin(state = initialState, action) {
  switch (action.type) {
    case 'LOAD_USERS': {
      return {...state, ...action.payload}
    }

    case 'LOAD_ROLES': {
      return {...state, ...action.payload}
    }

    case 'ADD_USER': {
      const newUser = {...newDefaultUser, isEditing: true}
      return {
        ...state,
        users: [
          newUser,
          ...state.users,
        ],
      }
    }

    case 'SET_EDITING_MODE': {
      const newState = {...state}
      newState.ephemeral.isEditing = action.payload.isEditing
      return newState
    }

    case 'RESET_EDITING_USER': {
      return {...state, ephemeral: {...state.ephemeral, editingUser: {...newDefaultUser}}}
    }

    case 'EDIT_USER': {
      const {user, updates} = action.payload
      const newState = {
        users: state.users.map(u => {
          const output = u.links.self === user.links.self ? {...u, ...updates} : u
          return output
        }),
      }
      return {...state, ...newState}
    }

    case 'CLEAR_EDITING_MODE': {
      const newState = {
        users: state.users.map(u => {
          u.isEditing = false
          return u
        }),
      }
      return {...state, ...newState}
    }

    // case 'CREATE_USER': {
    //   const {user, source} = action.payload
    //   const userLink = `${this.props.source.links.users}/${user.name}`
    //   Object.assign(user, {links: {self: userLink}})
    // }

    case 'DELETE_ROLE': {
      const {role} = action.payload
      const newState = {
        roles: state.roles.filter(r => r.name !== role.name),
      }

      return {...state, ...newState}
    }

    case 'DELETE_USER': {
      const {user} = action.payload
      const newState = {
        users: state.users.filter(u => u.links.self !== user.links.self),
      }

      return {...state, ...newState}
    }

    case 'LOAD_QUERIES': {
      return {...state, ...action.payload}
    }

    case 'FILTER_ROLES': {
{'fraction_non_alphanumeric': 0.16313284292514063, 'fraction_numerical': 0.0, 'mean_word_length': 1.883967560823456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4098378', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1497, 'n_words': 320}