@ -2,6 +2,7 @@ import _ from 'lodash';
import $ from 'jquery' ;
import { createAction } from 'redux-actions' ;
import { batchActions } from 'redux-batched-actions' ;
import createAjaxRequest from 'Utilities/createAjaxRequest' ;
import getSectionState from 'Utilities/State/getSectionState' ;
import updateSectionState from 'Utilities/State/updateSectionState' ;
import getNewArtist from 'Utilities/Artist/getNewArtist' ;
@ -15,14 +16,14 @@ import { fetchRootFolders } from './rootFolderActions';
export const section = 'importArtist' ;
let concurrentLookups = 0 ;
let abortCurrentLookup = null ;
const queue = [ ] ;
//
// State
export const defaultState = {
isFetching : false ,
isPopulated : false ,
error : null ,
isLookingUpArtist : false ,
isImporting : false ,
isImported : false ,
importError : null ,
@ -34,9 +35,10 @@ export const defaultState = {
export const QUEUE _LOOKUP _ARTIST = 'importArtist/queueLookupArtist' ;
export const START _LOOKUP _ARTIST = 'importArtist/startLookupArtist' ;
export const CLEAR _IMPORT _ARTIST = 'importArtist/importArtist' ;
export const SET _IMPORT _ARTIST _VALUE = 'importArtist/clearImportArtist' ;
export const IMPORT _ARTIST = 'importArtist/setImportArtistValue' ;
export const CANCEL _LOOKUP _ARTIST = 'importArtist/cancelLookupArtist' ;
export const CLEAR _IMPORT _ARTIST = 'importArtist/clearImportArtist' ;
export const SET _IMPORT _ARTIST _VALUE = 'importArtist/setImportArtistValue' ;
export const IMPORT _ARTIST = 'importArtist/importArtist' ;
//
// Action Creators
@ -45,10 +47,10 @@ export const queueLookupArtist = createThunk(QUEUE_LOOKUP_ARTIST);
export const startLookupArtist = createThunk ( START _LOOKUP _ARTIST ) ;
export const importArtist = createThunk ( IMPORT _ARTIST ) ;
export const clearImportArtist = createAction ( CLEAR _IMPORT _ARTIST ) ;
export const cancelLookupArtist = createAction ( CANCEL _LOOKUP _ARTIST ) ;
export const setImportArtistValue = createAction ( SET _IMPORT _ARTIST _VALUE , ( payload ) => {
return {
section ,
... payload
} ;
@ -63,7 +65,8 @@ export const actionHandlers = handleThunks({
const {
name ,
path ,
term
term ,
topOfQueue = false
} = payload ;
const state = getState ( ) . importArtist ;
@ -84,8 +87,20 @@ export const actionHandlers = handleThunks({
items : [ ]
} ) ) ;
const itemIndex = queue . indexOf ( item . id ) ;
if ( itemIndex >= 0 ) {
queue . splice ( itemIndex , 1 ) ;
}
if ( topOfQueue ) {
queue . unshift ( item . id ) ;
} else {
queue . push ( item . id ) ;
}
if ( term && term . length > 2 ) {
dispatch ( startLookupArtist ( ) ) ;
dispatch ( startLookupArtist ( { start : true } )) ;
}
} ,
@ -95,13 +110,27 @@ export const actionHandlers = handleThunks({
}
const state = getState ( ) . importArtist ;
const queued = _ . find ( state . items , { queued : true } ) ;
if ( ! queued ) {
const {
isLookingUpArtist ,
items
} = state ;
const queueId = queue [ 0 ] ;
if ( payload . start && ! isLookingUpArtist ) {
dispatch ( set ( { section , isLookingUpArtist : true } ) ) ;
} else if ( ! isLookingUpArtist ) {
return ;
} else if ( ! queueId ) {
dispatch ( set ( { section , isLookingUpArtist : false } ) ) ;
return ;
}
concurrentLookups ++ ;
queue . splice ( 0 , 1 ) ;
const queued = items . find ( ( i ) => i . id === queueId ) ;
dispatch ( updateItem ( {
section ,
@ -109,14 +138,16 @@ export const actionHandlers = handleThunks({
isFetching : true
} ) ) ;
const promise = $ . ajax ( {
const { request , abortRequest } = createAjaxRequest ( {
url : '/artist/lookup' ,
data : {
term : queued . term
}
} ) ;
promise . done ( ( data ) => {
abortCurrentLookup = abortRequest ;
request . done ( ( data ) => {
dispatch ( updateItem ( {
section ,
id : queued . id ,
@ -125,23 +156,26 @@ export const actionHandlers = handleThunks({
error : null ,
items : data ,
queued : false ,
selectedArtist : queued . selectedArtist || data [ 0 ]
selectedArtist : queued . selectedArtist || data [ 0 ] ,
updateOnly : true
} ) ) ;
} ) ;
p romis e. fail ( ( xhr ) => {
request . fail ( ( xhr ) => {
dispatch ( updateItem ( {
section ,
id : queued . id ,
isFetching : false ,
isPopulated : false ,
error : xhr ,
queued : false
queued : false ,
updateOnly : true
} ) ) ;
} ) ;
p romis e. always ( ( ) => {
request . always ( ( ) => {
concurrentLookups -- ;
dispatch ( startLookupArtist ( ) ) ;
} ) ;
} ,
@ -159,7 +193,7 @@ export const actionHandlers = handleThunks({
// Make sure we have a selected artist and
// the same artist hasn't been added yet.
if ( selectedArtist && ! _ . some ( acc , { tvdb Id: selectedArtist . tvdb Id } ) ) {
if ( selectedArtist && ! _ . some ( acc , { foreignArtis tId: selectedArtist . foreignArtis tId } ) ) {
const newArtist = getNewArtist ( _ . cloneDeep ( selectedArtist ) , item ) ;
newArtist . path = item . path ;
@ -216,7 +250,19 @@ export const actionHandlers = handleThunks({
export const reducers = createHandleActions ( {
[ CANCEL _LOOKUP _ARTIST ] : function ( state ) {
return Object . assign ( { } , state , { isLookingUpArtist : false } ) ;
} ,
[ CLEAR _IMPORT _ARTIST ] : function ( state ) {
if ( abortCurrentLookup ) {
abortCurrentLookup ( ) ;
abortCurrentLookup = null ;
}
queue . splice ( 0 , queue . length ) ;
return Object . assign ( { } , state , defaultState ) ;
} ,