<commit_before>import I from 'immutable'
import qs from 'query-string'
import request from 'superagent-bluebird-promise'
import { createReducer } from '../../utils'

const FETCH_LIST_REQUEST = 'FETCH_LIST_REQUEST_NOTES'
const FETCH_LIST_DONE = 'FETCH_LIST_DONE_NOTES'
const CREATE_REQUEST = 'CREATE_REQUEST_NOTES'
const CREATE_DONE = 'CREATE_DONE_NOTES'

const initState = I.fromJS({
  docs: {},
  error: [],
  isLoadingList: false,
  isLoadingCreate: false
})
const notes = createReducer(initState, {
  [FETCH_LIST_REQUEST](state, action) {
    return state.merge({
      error: [],
      isLoadingList: false
    })
  },
  [FETCH_LIST_DONE](state, action) {
    const { payload, error } = action
    if (error) {
      return state.merge({
        error: payload,
        isLoadingList: false
      })
    }
    return state.merge({
      isLoadingList: false
    }).mergeDeepIn(['docs'], payload)
  },
  [CREATE_REQUEST](state, action) {
    return state.merge({
      error: [],
      isLoadingCreate: true
    })
  },
  [CREATE_DONE](state, action) {
    const { payload, error } = action
    if (error) {
      return state.merge({
        error: payload,
        isLoadingCreate: false
      })
    }
    return state.merge({
      isLoadingCreate: false
    }).setIn(['docs', payload.id], payload)
  }
})

export const fetchList = (input, limit=10, skip=0) => dispatch => {
  dispatch(fetchListRequest())
  const filter = qs.stringify({ limit, skip })
  const url = input.includes('/api')
    ? `http://localhost:4000${input}`
    : `http://localhost:4000/api/v2/groups/${input}/notes`
  request
    .get(url)
    .then(res => {
      dispatch(fetchListDone(null, res.body))
    })
    .error(err => {
      console.error(err)
      dispatch(fetchListDone(err.body))
    })
}

const fetchListRequest = () => ({
  type: FETCH_LIST_REQUEST,
  payload: null
})

const fetchListDone = (err, body) => {
  if (err) {
    return {
      type: FETCH_LIST_DONE,
      payload: err.errors,
      error: true
    }
  }
  const flattenedData = body.data.map(item => {
    return {
      id: item.id,
      ...item.attributes,
      creator: {
        id: item.relationships.creator.data.id,
        ...item.relationships.creator.data.attributes
      }
    }
  }).reduce((prev, curr) => {
    prev[curr.id] = curr
    return prev
  }, {})
  return {
    type: FETCH_LIST_DONE,
    payload: flattenedData
  }
}

export const create = ({ token, groupId, link, description }) => dispatch => {
  dispatch(createRequest())
  request
    .post(`http://localhost:4000/api/v2/groups/${groupId}/notes`)
    .type('application/json')
    .set('Authorization', token)
    .send({
      link,
      desc: description
    })
    .then(res => {
      dispatch(createDone(null, res.body))
      dispatch(fetchList(groupId))
    })
    .error(err => {
      console.error(err.body)<commit_msg>refetch group notes after creating note<commit_after>import I from 'immutable'
import qs from 'query-string'
import request from 'superagent-bluebird-promise'
import { createReducer } from '../../utils'
import { groupActs } from '.'

const FETCH_LIST_REQUEST = 'FETCH_LIST_REQUEST_NOTES'
const FETCH_LIST_DONE = 'FETCH_LIST_DONE_NOTES'
const CREATE_REQUEST = 'CREATE_REQUEST_NOTES'
const CREATE_DONE = 'CREATE_DONE_NOTES'

const initState = I.fromJS({
  docs: {},
  error: [],
  isLoadingList: false,
  isLoadingCreate: false
})
const notes = createReducer(initState, {
  [FETCH_LIST_REQUEST](state, action) {
    return state.merge({
      error: [],
      isLoadingList: false
    })
  },
  [FETCH_LIST_DONE](state, action) {
    const { payload, error } = action
    if (error) {
      return state.merge({
        error: payload,
        isLoadingList: false
      })
    }
    return state.merge({
      isLoadingList: false
    }).mergeDeepIn(['docs'], payload)
  },
  [CREATE_REQUEST](state, action) {
    return state.merge({
      error: [],
      isLoadingCreate: true
    })
  },
  [CREATE_DONE](state, action) {
    const { payload, error } = action
    if (error) {
      return state.merge({
        error: payload,
        isLoadingCreate: false
      })
    }
    return state.merge({
      isLoadingCreate: false
    }).setIn(['docs', payload.id], payload)
  }
})

export const fetchList = (input, limit=10, skip=0) => dispatch => {
  dispatch(fetchListRequest())
  const filter = qs.stringify({ limit, skip })
  const url = input.includes('/api')
    ? `http://localhost:4000${input}`
    : `http://localhost:4000/api/v2/groups/${input}/notes`
  request
    .get(url)
    .then(res => {
      dispatch(fetchListDone(null, res.body))
    })
    .error(err => {
      console.error(err)
      dispatch(fetchListDone(err.body))
    })
}

const fetchListRequest = () => ({
  type: FETCH_LIST_REQUEST,
  payload: null
})

const fetchListDone = (err, body) => {
  if (err) {
    return {
      type: FETCH_LIST_DONE,
      payload: err.errors,
      error: true
    }
  }
  const flattenedData = body.data.map(item => {
    return {
      id: item.id,
      ...item.attributes,
      creator: {
        id: item.relationships.creator.data.id,
        ...item.relationships.creator.data.attributes
      }
    }
  }).reduce((prev, curr) => {
    prev[curr.id] = curr
    return prev
  }, {})
  return {
    type: FETCH_LIST_DONE,
    payload: flattenedData
  }
}

export const create = ({ token, groupId, link, description }) => dispatch => {
  dispatch(createRequest())
  request
    .post(`http://localhost:4000/api/v2/groups/${groupId}/notes`)
    .type('application/json')
    .set('Authorization', token)
    .send({
      link,
      desc: description
    })
    .then(res => {
      dispatch(createDone(null, res.body))
      dispatch(groupActs.fetchOne(groupId))
    })
    .error(err => {
      console.error(err.body)
{'fraction_non_alphanumeric': 0.14730683187059027, 'fraction_numerical': 0.005850972293925314, 'mean_word_length': 2.798692810457516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '4993782', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1915, 'n_words': 379}