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