import {
	SELECT_ITEM,
	LOAD_DATA,
	DATA_LOADING_SUCCESS,
	DATA_LOADING_ERROR,
	DELETE_ITEM,
} from './constants';

/**
 * Select an item
 *
 * @param  {String} itemId The item ID
 */
export function selectItem (itemId) {
	return {
		type: SELECT_ITEM,
		id: itemId,
	};
}

/**
 * Load the item data of the current item
 */
export function loadItemData () {
	return (dispatch, getState) => {
		// Crete an empty object to reference a point in memory.
		// Dispatch this reference to our redux store to hold on to as a 'loadingRef'.
		const thisLoadRef = {};
		dispatch({
			type: LOAD_DATA,
			loadingRef: thisLoadRef,
		});
		const state = getState();
		const list = state.lists.currentList;
		// Load a specific item with the utils/List.js helper
		list.loadItem(state.item.id, { drilldown: true }, (err, itemData) => {
			// Once this async request has fired this callback, check that
			// the point in memory referenced by thisLoadRef is the same point in memory
			// referenced by loadingRef in the redux store.

			// If it is, then this is the latest request, and it is safe to resolve it normally.
			// If it is not a reference the same point in memory however,
			// this means that this request is NOT the latest fired request,
			// and so we'll bail out of it early.
			if (getState().item.loadingRef !== thisLoadRef) return;
			if (err || !itemData) {
				console.log('Error loading item data', err);
				dispatch(dataLoadingError(err));
			} else {
				dispatch(dataLoaded(itemData));
			}
		});
	};
}

/**
 * Called when data of the current item is loaded
 *
 * @param  {Object} data The item data
 */
export function dataLoaded (data) {
	return {
		type: DATA_LOADING_SUCCESS,
		loadingRef: null,
		data,
	};
}

/**
 * Called when there was an error during the loading of the current item data,
 * will retry loading the data ever NETWORK_ERROR_RETRY_DELAY milliseconds
 *
 * @param  {Object} error The error
 */
export function dataLoadingError (err) {
	return {
		type: DATA_LOADING_ERROR,
		loadingRef: null,
		error: err,
	};
}

/**
 * Deletes an item and optionally redirects to the current list URL
 *
 * @param  {String} id     The ID of the item we want to delete
 * @param  {Object} router A react-router router object. If this is passed, we
 *                         redirect to Keystone.adminPath/currentList.path!
 */
export function deleteItem (id, router) {
	return (dispatch, getState) => {
		const list = getState().lists.currentList;
		list.deleteItem(id, (err) => {
			// If a router is passed, redirect to the current list path,
			// otherwise stay where we are
			if (router) {
				router.push(`${Keystone.adminPath}/${list.path}`);
			}
			// TODO Proper error handling
			if (err) {
				alert('Error deleting item, please try again!');
			} else {
				dispatch({
					type: DELETE_ITEM,
					id,
				});
			}
		});
	};
}

@@ -686,16 +686,48 @@
 ntList;%0A
+%09%09const itemID = state.item.id;%0A
 %09%09// Loa
@@ -1334,34 +1334,21 @@
 tem.
-loadingRef !== thisLoadRef
+id !== itemID
 ) re

use item ids instead of obj ref