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

109 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-12-01 22:12:13 +01:00
import { select, put } from 'redux-saga/effects';
import { fetchIssues, addLabel, removeLabel } from '../actions/issues';
import { fetchIssuesSaga } from './issues';
2020-04-30 13:02:56 +02:00
import { BUILD_KANBOARD_SUCCESS, buildKanboard, BUILD_KANBOARD_FAILURE } from '../actions/kanboards';
2019-12-01 22:12:13 +01:00
2020-04-30 13:02:56 +02:00
export function* moveCardSaga(action: any) {
2019-12-01 22:12:13 +01:00
const {
boardID, fromLaneID,
fromPosition, toLaneID,
toPosition,
} = action;
2019-12-05 13:10:43 +01:00
if (fromLaneID === toLaneID) return;
2019-12-01 22:12:13 +01:00
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;
2019-12-06 10:02:30 +01:00
yield put(addLabel(card.project, card.issue.number, board.lanes[toLaneID].issueLabel));
yield put(removeLabel(card.project, card.issue.number, board.lanes[fromLaneID].issueLabel));
2019-12-01 22:12:13 +01:00
}
2020-04-30 13:02:56 +02:00
export function* buildKanboardSaga(action: any) {
2019-12-01 22:12:13 +01:00
const { board } = action;
let kanboard;
try {
for (let p, i = 0; (p = board.projects[i]); i++) {
2020-04-30 13:02:56 +02:00
const { project } = yield fetchIssues(p);
yield fetchIssuesSaga({ project });
2019-12-01 22:12:13 +01:00
}
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 });
2019-12-05 22:37:09 +01:00
}
2019-12-01 22:12:13 +01:00
2020-04-30 13:02:56 +02:00
export function* refreshKanboardSaga(action: any) {
2019-12-05 22:37:09 +01:00
const { project } = action;
const boards = yield select(state => state.boards);
const boardValues = Object.values(boards.byID);
2020-04-30 13:02:56 +02:00
for (let b: any, i = 0; (b = boardValues[i]); i++) {
2019-12-05 22:37:09 +01:00
const hasProject = b.projects.indexOf(project) !== -1;
if (!hasProject) continue;
yield put(buildKanboard(b));
}
2019-12-01 22:12:13 +01:00
}
2020-04-30 13:02:56 +02:00
function createCards(projects: any[], issues: any, lane: any) {
2019-12-01 22:12:13 +01:00
return projects.reduce((laneCards, p) => {
const projectIssues = p in issues.byProject ? issues.byProject[p] : [];
2020-04-30 13:02:56 +02:00
return projectIssues.reduce((projectCards: any, issue: any) => {
const hasLabel = issue.labels.some((l: any) => l.name === lane.issueLabel);
2019-12-01 22:12:13 +01:00
if (hasLabel) {
projectCards.push({
id: issue.id,
2019-12-05 17:09:11 +01:00
title: issue.title,
2019-12-01 22:12:13 +01:00
project: p,
2019-12-05 17:09:11 +01:00
issue: issue,
2019-12-01 22:12:13 +01:00
});
}
return projectCards;
}, laneCards);
}, []);
}
2020-04-30 13:02:56 +02:00
function createLane(projects: any, issues: any, lane: any, index: any) {
2019-12-01 22:12:13 +01:00
return {
id: index,
title: lane.title,
cards: createCards(projects, issues, lane)
}
}
2020-04-30 13:02:56 +02:00
function createKanboard(board: any, issues: any) {
2019-12-01 22:12:13 +01:00
if (!board) return null;
const kanboard = {
id: board.id,
lanes: board.lanes.map(createLane.bind(null, board.projects, issues)),
};
return kanboard;
}