import { call , put , select } from 'redux-saga/effects' ; import { TOpticonResponse , RequesterMap , SearchResult } from '../types' ; import { FetchTOpticonRequest , FetchTOpticonSuccess , FetchTOpticonFailure , fetchTOpticonSuccess , fetchTOpticonFailure , fetchTOpticonRequest } from '../actions/turkopticon' ; import { topticonMapFromTO , selectHitRequesterId } from '../utils/turkopticon' ; import { fetchTurkopticonData } from '../api/turkopticon' ; import { SearchSuccess } from 'actions/search' ; import { loggedRequestersSelector } from 'selectors' ; param action export function * requestDataForUnseenRequesters ( action ) { const loggedRequesters = yield select ( loggedRequestersSelector ) ; const searchResultsWithNoTOData = action . data . filterNot ( ( hit ) => loggedRequesters . has ( hit . requester . id ) ) ; yield put < FetchTOpticonRequest > ( fetchTOpticonRequest ( searchResultsWithNoTOData . map ( selectHitRequesterId ) . toList ( ) ) ) ; } export function * resolveFetchTurkopticonRequest ( { data } ) { if ( data . size === 0 ) { return ; } try { const rawTopticonData = yield call ( fetchTurkopticonData , data ) ; const topticonData = topticonMapFromTO ( rawTopticonData ) ; yield put < FetchTOpticonSuccess > ( fetchTOpticonSuccess ( topticonData ) ) ; } catch ( e ) { yield put < FetchTOpticonFailure > ( fetchTOpticonFailure ( ) ) ; } }