import { call, put } from 'redux-saga/effects';
import { post } from 'services/requests';
import { Api } from 'routes/Api';
import { handleError } from 'modules/error-handler';
import * as actions from 'lib/redux/actions/flags-actions';
import { message } from 'antd';



export function* flagContent(action): Generator {
	const hide = message.loading('Submitting your report');
	try {
		const res: Record<string, any> = yield call(() => post({
			url: Api.flags,
			data: action.payload
		}));

		yield put(actions.flaggedContent({ data: res.data }));
		message.success('Report submitted');

	}
	catch (error) {
		yield put(actions.failedFlagContent({ error: error.message }));

		if (error.response?.status == 401) {
			return message.error('You are currently not logged in');
		} else if (error.response?.status == 403) {
			return message.error('You don\'t have enough permissions to carry out this action');
		}

		message.error('Unable to submit your report at this time');
	}
	finally {
		hide();
	}
}


