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/lifestyles-actions';



export function* fetchLifestyleCategories(): Generator {
	try {
		const res: Record<string, any> = yield call(() => get({
			url: Api.lifestyles
		}));

		yield put(actions.fetchedLifestyleCategories({ data: res.data }));

	}
	catch (error) {
		yield put(actions.failedFetchLifestyleCategories({ error: error.message }));

		handleError(error);
	}
}


export function* fetchLifestyles(action): Generator {
	try {
		const res: Record<string, any> = yield call(() => get({
			url: Api.lifestyles + '/' + action.slug
		}));

		yield put(actions.fetchedLifestyles({ data: res.data }));

	}
	catch (error) {
		yield put(actions.failedFetchLifestyles({ error: error.message }));

		handleError(error);
	}
}


export function* fetchLifestyle(action): Generator {
	try {
		const res: Record<string, any> = yield call(() => get({
			url: Api.lifestyles + '/single/' + action.slug
		}));

		yield put(actions.fetchedLifestyle({ data: res.data }));

	}
	catch (error) {
		yield put(actions.failedFetchLifestyle({ error: error.message }));

		handleError(error);
	}
}

