import axios from 'axios';
import { BASE_URL } from '../config/config';

export const clearCurrentContent = (cb) => (dispatch) => {
	dispatch({
		type: 'CLEAR_CONTENT',
		payload: {
			content: [],
			count: 0,
			queryString: '',
			offset: 1,
			searching: false
		}
	});

	cb(null);
};

export const searchContent = (queryString = '', limit, offset, cb) => (dispatch) => {
	dispatch({
		type: 'SEARCHING_CONTENT',
		payload: true
	});

	axios.get(`${BASE_URL}/cloud/search?query=${queryString}&limit=${limit}&offset=${offset}`)
		.then(({ data }) => {
			if(data.success) {
				dispatch({
					type: 'SEARCHED_CONTENT',
					payload: {
						content: data.hits.content,
						count: data.hits.count,
						queryString,
						offset,
						searching: false
					}
				});

				cb(null);
			} else {
				throw new Error(data.err);
			}
		})
		.catch(err => {
			cb(err);
		});
};

const sortByStatus = (downloads, statusMap) => {
	return downloads.sort((a, b) => statusMap[a.status] - statusMap[b.status]);
};

export const updateDownloadQueue = (downloads, cb) => (dispatch) => {
	const statusMap = {
		'ongoing': 1,
		'failed': 2,
		'done': 3
	};

	downloads = sortByStatus(downloads, statusMap);
	
	dispatch({
		type: 'UPDATE_DOWNLOAD_QUEUE',
		payload: downloads
	});

	cb(null);
};
@@ -1260,28 +1260,293 @@
 ownloads%0A%09%7D);%0A%0A%09cb(null);%0A%7D;
+%0A%0Aexport const loadContent = (cb) =%3E (dispatch) =%3E %7B%0A%09axios.put(%60$%7BBASE_URL%7D/file/apply%60)%0A%09%09.then(response =%3E %7B%0A%09%09%09if(response.data.success) %7B%0A%09%09%09%09cb(null);%0A%09%09%09%7D else %7B%0A%09%09%09%09throw new Error(response.data.message);%0A%09%09%09%7D%0A%09%09%7D)%0A%09%09.catch(error =%3E %7B%0A%09%09%09cb(error);%0A%09%09%7D);%0A%7D;

Add action for loading downloaded content into the plugin