import { call, put } from 'redux-saga/effects';
import { get } from 'services/requests';
import { Api } from 'routes/Api';
import { handleError } from 'modules/error-handler';
import * as actions from 'lib/redux/actions/directories-actions';



export function* fetchDirectories() {
	try {
		const res = yield call(() => get({
			url: Api.directories
		}));

		// dispatch a success action to the store with the fetched classifieds
		yield put(actions.fetchedDirectories({ data: res.data }));
	}
	catch (error) {
		// dispatch a failure action to the store with the error
		yield put(actions.failedFetchDirectories({ error: error.message }));

		handleError(error);
	}
}


export function* fetchSubDirectories(action) {
	try {
		const res = yield call(() => get({
			url: Api.directories+'/'+action.slug
		}));

		// dispatch a success action to the store with the fetched classifieds
		yield put(actions.fetchedSubDirectories({ data: res.data }));
	}
	catch (error) {
		// dispatch a failure action to the store with the error
		yield put(actions.failedFetchSubDirectories({ error: error.message }));

		handleError(error);
	}
}


// export function* fetchDirectoryListing(action) {
// 	try {
// 		const res = yield call(() => get({
// 			url: Api.directory_listings+'/'+action.slug
// 		}));
// 
// 		// dispatch a success action to the store with the fetched classifieds
// 		yield put(actions.fetchedDirectoryListing({ data: res.data }));
// 	}
// 	catch (error) {
// 		// dispatch a failure action to the store with the error
// 		yield put(actions.failedFetchDirectoryListing({ error: error.message }));
// 
// 		handleError(error);
// 	}
// }

