1- import { forEach } from 'lodash'
1+ import { find , forEach , sortBy } from 'lodash'
22import Config from '../../../config/config'
3- import { getMeBoards } from '../../data/trello'
3+ import { getBoard , getMeBoards } from '../../data/trello'
44
55const actions = {
66 RESET_BOARDS : 'RESET_BOARDS' ,
@@ -23,20 +23,48 @@ const requestBoards = () => async dispatch => {
2323 try {
2424 const result = await getMeBoards ( )
2525 const boards = [ ]
26- forEach ( result , board => {
27- forEach ( Config . boards , configuredBoard => {
28- if ( configuredBoard . board === board . name ) {
29- const boardObj = {
30- board,
31- config : configuredBoard ,
32- }
33- boards . push ( boardObj )
26+
27+ const additionalBoards = [ ]
28+ forEach ( Config . boards , ( configuredBoard , idx ) => {
29+ // get a board by it's name (works for boards a user subscribed to)
30+ const nameBoard = find ( result , b => b . name === configuredBoard . name )
31+ const idBoard = find ( result , b => b . id === configuredBoard . id )
32+
33+ if ( nameBoard || idBoard ) {
34+ const boardObj = {
35+ idx,
36+ board : nameBoard ,
37+ config : configuredBoard ,
3438 }
35- } )
39+ boards . push ( boardObj )
40+ } else if ( configuredBoard . id && ! idBoard ) {
41+ // TODO: write test for this case
42+ // if the .id is set, but the board was not found we query boards
43+ // (eg. for public boards, where people are not member of it, which means
44+ // you would not find it in their me/boards result)
45+ additionalBoards . push (
46+ new Promise ( resolve => {
47+ getBoard ( configuredBoard . id )
48+ . then ( data => resolve ( { configuredBoard, data, idx } ) )
49+ . catch ( error => resolve ( { configuredBoard, error, idx } ) )
50+ } ) ,
51+ )
52+ }
53+ } )
54+
55+ const idBoards = await Promise . all ( additionalBoards )
56+ forEach ( idBoards , ( { configuredBoard, data, idx } ) => {
57+ const boardObj = {
58+ idx,
59+ board : data ,
60+ config : configuredBoard ,
61+ }
62+ boards . push ( boardObj )
3663 } )
3764
3865 // then update the state once we got all boards
39- dispatch ( receiveBoards ( boards ) )
66+ // TODO: evaluate if board.idx should be removed
67+ dispatch ( receiveBoards ( sortBy ( boards , [ 'idx' ] ) ) )
4068 } catch ( e ) {
4169 dispatch ( receiveBoards ( e , true ) )
4270 }
0 commit comments