export const CHANGE_THEME = "CHANGE_THEME";
export const PELAPORAN_LIST_REQUEST = "PELAPORAN_LIST_REQUEST";
export const PELAPORAN_LIST_SUCCESS = "PELAPORAN_LIST_SUCCESS";
export const PELAPORAN_LIST_FAIL = "PELAPORAN_LIST_FAIL";
export const PELAPORAN_DETAILS_REQUEST = "PELAPORAN_DETAILS_REQUEST";
export const PELAPORAN_DETAILS_SUCCESS = "PELAPORAN_DETAILS_SUCCESS";
export const PELAPORAN_DETAILS_FAIL = "PELAPORAN_DETAILS_FAIL";
export const PELAPORAN_CREATE_REQUEST = "PELAPORAN_DETAILS_REQUEST";
export const PELAPORAN_CREATE_SUCCESS = "PELAPORAN_DETAILS_SUCCESS";
export const PELAPORAN_CREATE_FAIL = "PELAPORAN_DETAILS_FAIL";
export const PELAPORAN_CREATE_RESET = "PELAPORAN_CREATE_RESET";

/**
 * Change current theme path
 */

export const listPelaporan = () => async (dispatch) => {
	try {
		dispatch({ type: PELAPORAN_LIST_REQUEST });

		const res = await fetch("http://localhost:5000/pelaporan");
		const { data } = await res.json();

		dispatch({
			type: PELAPORAN_LIST_SUCCESS,
			payload: data,
		});
	} catch (error) {
		dispatch({
			type: PELAPORAN_LIST_FAIL,
			payload: error.response && error.response.data.message ? error.response.data.message : error.message,
		});
	}
};

export const listPelaporanDetails = (number, ptId) => async (dispatch) => {
	try {
		dispatch({ type: PELAPORAN_DETAILS_REQUEST });

		const res = await fetch(`http://localhost:5000/pelaporan?number=${number}&ptId=${ptId}`);
		const { data } = await res.json();

		dispatch({
			type: PELAPORAN_DETAILS_SUCCESS,
			payload: data,
		});
	} catch (error) {
		dispatch({
			type: PELAPORAN_DETAILS_FAIL,
			payload: error.response && error.response.data.message ? error.response.data.message : error.message,
		});
	}
};

export const createPelaporan = (pelaporanResult) => async (dispatch) => {
	try {
		dispatch({
			type: PELAPORAN_CREATE_REQUEST,
		});

		const res = await fetch("http://localhost:5000/pelaporan/create", {
			method: "POST",
			body: pelaporanResult,
		});
		const data = await res.json();

		dispatch({
			type: PELAPORAN_CREATE_SUCCESS,
			payload: data,
		});
	} catch (error) {
		dispatch({
			type: PELAPORAN_CREATE_FAIL,
			payload: error.response && error.response.data.message ? error.response.data.message : error.message,
		});
	}
};
