{"size": 1418, "ext": "js", "max_stars_repo_path": "app/containers/ColumnUserIllust/saga.js", "max_stars_repo_name": "austinhuang0131/PixivDeck", "max_stars_repo_stars_event_min_datetime": "2020-08-06T05:35:11.000Z", "max_stars_repo_stars_event_max_datetime": "2020-08-06T05:35:11.000Z", "max_issues_repo_path": "app/containers/ColumnUserIllust/saga.js", "max_issues_repo_name": "austinhuang0131/PixivDeck", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/containers/ColumnUserIllust/saga.js", "max_forks_repo_name": "austinhuang0131/PixivDeck", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 35.45, "max_line_length": 80, "alphanum_fraction": 0.6967559944}
// @flow
import { addTable } from 'containers/ColumnManager/actions'
import * as fetchColumn from '../Column/sagas'
import * as Actions from './constants'
import * as actions from './actions'
import type { ColumnId } from './reducer'
import { makeSelectColumn, makeSelectIds } from './selectors'
import { put, select, call, takeEvery } from 'redux-saga/effects'

type Action = { id: ColumnId }

export function* addColumn({ id }: Action): Generator<*, void, *> {
  const ids: Array<?ColumnId> = yield select(makeSelectIds())
  if (ids.every(v => v !== id)) {
    yield put(actions.addColumnSuccess(id))
  }

  yield put(
    addTable(`user-illust-${id}`, { columnId: String(id), type: 'USER_ILLUST' })
  )
}

const endpoint = id => `/v1/user/illusts?type=illust&user_id=${id}`

export function* fetchUserIllust({ id }: Action): Generator<*, void, *> {
  const { ids } = yield select(makeSelectColumn(), { id })
  yield call(fetchColumn.fetchColumn, endpoint(id), id, actions, ids)
}

export function* fetchNextUserIllust({ id }: Action): Generator<*, void, *> {
  const { ids, nextUrl } = yield select(makeSelectColumn(), { id })
  yield call(fetchColumn.fetchColumn, nextUrl, id, actions, ids)
}

export default function* root(): Generator<*, void, void> {
  yield takeEvery(Actions.ADD_COLUMN, addColumn)
  yield takeEvery(Actions.FETCH, fetchUserIllust)
  yield takeEvery(Actions.FETCH_NEXT, fetchNextUserIllust)
}
