import { showLoading, hideLoading } from './loading_bar_ducks'

const defaultTypeSuffixes = ['PENDING', 'FULFILLED', 'REJECTED']

export default function loadingBarMiddleware(config = {}) {
  const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypeSuffixes

  return ({ dispatch }) => next => action => {
    next(action)

    if (action.type === undefined) {
      return
    }

    const [PENDING, FULFILLED, REJECTED] = promiseTypeSuffixes

    const isPending = `_${PENDING}`
    const isFulfilled = `_${FULFILLED}`
    const isRejected = `_${REJECTED}`

    if (action.type.indexOf(isPending) !== -1) {
      dispatch(showLoading())
    } else if (action.type.indexOf(isFulfilled) !== -1 ||
               action.type.indexOf(isRejected) !== -1) {
      dispatch(hideLoading())
    }
  }
}

@@ -472,18 +472,28 @@
 nding = 
+new RegExp(
 %60
-_
 $%7BPENDIN
@@ -494,17 +494,24 @@
 PENDING%7D
-%60
+$%60, 'g')
 %0A    con
@@ -527,18 +527,28 @@
 illed = 
+new RegExp(
 %60
-_
 $%7BFULFIL
@@ -551,17 +551,24 @@
 LFILLED%7D
-%60
+$%60, 'g')
 %0A    con
@@ -587,10 +587,20 @@
 d = 
+new RegExp(
 %60
-_
 $%7BRE
@@ -606,17 +606,24 @@
 EJECTED%7D
-%60
+$%60, 'g')
 %0A%0A    if
@@ -616,32 +616,34 @@
 , 'g')%0A%0A    if (
+!!
 action.type.inde
@@ -638,23 +638,21 @@
 on.type.
-indexOf
+match
 (isPendi
@@ -654,23 +654,16 @@
 Pending)
- !== -1
 ) %7B%0A    
@@ -699,24 +699,26 @@
  %7D else if (
+!!
 action.type.
@@ -717,23 +717,21 @@
 on.type.
-indexOf
+match
 (isFulfi
@@ -735,23 +735,16 @@
 lfilled)
- !== -1
  %7C%7C%0A    
@@ -754,16 +754,18 @@
         
+!!
 action.t
@@ -772,15 +772,13 @@
 ype.
-indexOf
+match
 (isR
@@ -789,15 +789,8 @@
 ted)
- !== -1
 ) %7B%0A

Update the way we identify suffixes to be more precise