import { select, put } from 'redux-saga/effects'; import { fetchIssues, addLabel, removeLabel } from '../actions/issues'; import { fetchIssuesSaga } from './issues'; import { BUILD_KANBOARD_SUCCESS, buildKanboard } from '../actions/kanboards'; export function* moveCardSaga(action) { const { boardID, fromLaneID, fromPosition, toLaneID, toPosition, } = action; if (fromLaneID === toLaneID) return; const { board, kanboard} = yield select(state => { return { kanboard: state.kanboards.byID[boardID], board: state.boards.byID[boardID] } }); const toLane = kanboard.lanes[toLaneID]; const card = toLane.cards[toPosition]; if (!card) return; yield put(addLabel(card.project, card.number, board.lanes[toLaneID].issueLabel)); yield put(removeLabel(card.project, card.number, board.lanes[fromLaneID].issueLabel)); } export function* buildKanboardSaga(action) { const { board } = action; let kanboard; try { for (let p, i = 0; (p = board.projects[i]); i++) { yield* fetchIssuesSaga(fetchIssues(p)); } const issues = yield select(state => state.issues); kanboard = createKanboard(board, issues); } catch(error) { yield put({ type: BUILD_KANBOARD_FAILURE, error }); return } yield put({ type: BUILD_KANBOARD_SUCCESS, kanboard }); } export function* refreshKanboardSaga(action) { const { project } = action; const boards = yield select(state => state.boards); const boardValues = Object.values(boards.byID); for (let b, i = 0; (b = boardValues[i]); i++) { const hasProject = b.projects.indexOf(project) !== -1; if (!hasProject) continue; yield put(buildKanboard(b)); } } function createCards(projects, issues, lane) { return projects.reduce((laneCards, p) => { const projectIssues = p in issues.byProject ? issues.byProject[p] : []; return projectIssues.reduce((projectCards, issue) => { const hasLabel = issue.labels.some(l => l.name === lane.issueLabel); if (hasLabel) { projectCards.push({ id: issue.id, title: issue.title, project: p, issue: issue, }); } return projectCards; }, laneCards); }, []); } function createLane(projects, issues, lane, index) { return { id: index, title: lane.title, cards: createCards(projects, issues, lane) } } function createKanboard(board, issues) { if (!board) return null; const kanboard = { id: board.id, lanes: board.lanes.map(createLane.bind(null, board.projects, issues)), }; return kanboard; }