import { call, put } from 'redux-saga/effects';
import { get } from 'services/requests';
import { Api } from 'routes/Api';
import * as actions from 'lib/redux/actions/search-actions';
import { Action } from 'lib/types/interfaces';

export function* fetchSearchSuggestions(action: Action): Generator {
	try {
		const res: Record<string, any> = yield call(() => get({
			url: Api.search + '/suggestions/' + action.searchKey
		}));

		yield put(actions.fetchedSearchSuggestions({ data: res.data }));

	}
	catch (error) {
		yield put(actions.failedFetchSearchSuggestions({ error: error.message }));
	}
}
