import Axios from 'axios';
import {
    BOARD_LIST_REQUESTED,
    BOARD_LIST_LOADED,
    BOARD_LIST_INVALIDATED,

    BOARD_LIST_ADD_FAVOURITE,
    BOARD_LIST_REMOVE_FAVOURITE,
} from '../constants';
import { alertMessage } from './StatusActions'

function requestBoardList(provider) {
    return {
        payload: provider,
        type: BOARD_LIST_REQUESTED
    }
}

function receiveBoardList(boardList, provider){
    return {
        type: BOARD_LIST_LOADED,
        payload: boardList.data || [],
        provider
    }
}

function invalidateBoardlist(error) {
    return {
        type: BOARD_LIST_INVALIDATED
    }
}

export function fetchBoardList( provider ) {
    console.log(`Action FetchBoard() to /api/${provider}/boards`);
    return (dispatch, getState) => {
        if (shouldFetchBoardList(getState(), provider)) {
            dispatch(requestBoardList(provider))

            return Axios
                .get(`/api/${provider}/boards`)
                .then(data => dispatch(receiveBoardList(data, provider)))
                .catch( err => {
                    console.error(err)
                    dispatch(alertMessage({
                        message: `Couldn't request ${provider}s boardlist`,
                        type: "error"
                    }))
                    dispatch(invalidateBoard())
                });
        }
    }
}

function shouldFetchBoardList({boardList}, provider) {
    return !boardList.hasOwnProperty(provider)
}

export function addToFavourites(provider, boardID) {
    return (dispatch, getState) => {
        if (boardInFavourites(getState(), provider, boardID)) return

        dispatch({
            type: BOARD_LIST_ADD_FAVOURITE,
            payload: {
                provider,
                boardID
            }
        })
    }
}

export function removeFromFavourites(provider, boardID) {
    return (dispatch, getState) => {
        
        if (boardInFavourites(getState(), provider, boardID)) {
            dispatch({
                type: BOARD_LIST_REMOVE_FAVOURITE,
                payload: {
                    provider,
                    boardID
                }
            })
        }
    }
}

function boardInFavourites({boardList}, provider, boardID) {
    return !!boardList['favourites'].find(el => 
        el.boardID === boardID && el.provider === provider
    )
}

@@ -556,24 +556,49 @@
 st(error) %7B%0A
+    console.error(error)%0A
     return %7B
@@ -634,16 +634,31 @@
 ALIDATED
+,%0A        error
 %0A    %7D%0A%7D
@@ -1011,16 +1011,17 @@
   .then(
+ 
 data =%3E 
@@ -1101,47 +1101,8 @@
 %3E %7B%0A
-                    console.error(err)%0A
     
@@ -1179,47 +1179,57 @@
 e: %60
-Couldn't request $%7Bprovider%7Ds boardlist
+Error fetching boardlist from $%7Bprovider%7D: $%7Berr%7D
 %60,%0A 
@@ -1264,16 +1264,53 @@
  %22error%22
+,%0A                        time: 20000
 %0A       
@@ -1371,16 +1371,19 @@
 teBoard(
+err
 ))%0A     

Improve error handling for board + increase duration user alerted for on error