gengitkan/client/src/store/sagas/kanboards.js

99 lines
2.3 KiB
JavaScript

import { select, put } from 'redux-saga/effects';
import { fetchIssues, addLabel, removeLabel } from '../actions/issues';
import { fetchIssuesSaga } from './issues';
import { BUILD_KANBOARD_SUCCESS } from '../actions/kanboards';
export function* moveCardSaga(action) {
const {
boardID, fromLaneID,
fromPosition, toLaneID,
toPosition,
} = action;
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 });
}
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.number} - ${issue.title}`,
description: "",
project: p,
labels: issue.labels,
assignee: issue.assignee,
number: issue.number
});
}
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;
}