AlkantarClanX12

Your IP : 216.73.217.24


Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/src/inventory/
Upload File :
Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/src/inventory/utils.js

export function toOptions(options, transform = (item, key) => item) {
	return Object
		.keys(options)
		.map(key => {
			const option = options[key];
			const item = { label: option, value: option };
			transform(item, key);
			return item;
		});
}

export function resetAll({ setAttributes, attributes }) {

	const { year_min, year_max, price_min, price_max, mileage_min, mileage_max } = attributes;

	setAttributes({
		filter: false,
		slick: false,
		sort_by: 'year',
		sort_by_condition: 'desc',
		sort_by_secondary: 'none',
		sort_by_condition_secondary: 'desc',
		limit: 0,
		year: 'all',
		year_from: year_min,
		year_to: year_max,
		make: 'all',
		make_selected: [],
		model: 'all',
		model_selected: [],
		trim: 'all',
		trim_selected: [],
		location: 'all',
		location_selected: [],
		price: 'all',
		price_from: price_min,
		price_to: price_max,
		mileage: 'all',
		mileage_from: mileage_min,
		mileage_to: mileage_max,
		type: 'all',
		type_selected: [],
		status: 'all',
		status_selected: [],
		tags: 'all',
		tags_selected: [],
		exclude_tags: '',
		exclude_tags_selected: [],
		stock: 'all',
		stock_selected: [],
    sort_makes: [],
		sort_models: [],
		sort_locations: [],
		sort_stocks: []
	});
}

export function resetSlider({ attributes, setAttributes }, propName) {
	return setAttributes({
		[propName]: 'all',
		[propName + '_from']: [attributes[propName + '_min']],
		[propName + '_to']: [attributes[propName + '_max']]
	})
}

export function resetSelect({ attributes, setAttributes }, propName) {
	return setAttributes({
		[propName]: 'all',
		[propName + '_selected']: []
	})
}

export function resetSort({ setAttributes }) {
	return setAttributes({
		sort_by: 'year',
		sort_by_condition: 'desc',
		sort_by_secondary: 'none',
		sort_by_condition_secondary: 'desc'
	})
}

export function fromSliderValues({ attributes, setAttributes }, propName, value) {
	const fromName = propName + '_from';
	const toName = propName + '_to';

	const from = parseInt(attributes[fromName]);
	const to = parseInt(attributes[toName]);
	const newAttributes = {};

	value = parseInt(value);

	newAttributes[fromName] = (value > to) ? to : value;
	newAttributes[propName] = (from == to) ? from.toString() : `${value}-${to}`;

	return setAttributes(newAttributes);
}

export function toSliderValues({ attributes, setAttributes }, propName, value) {

	const fromName = propName + '_from';
	const toName = propName + '_to';

	const from = parseInt(attributes[fromName]);
	const to = parseInt(attributes[toName]);
	const newAttributes = {};

	value = parseInt(value);

	newAttributes[toName] = (value < from) ? from : value;
	newAttributes[propName] = (from == to) ? to.toString() : `${from}-${value}`;

	return setAttributes(newAttributes);
}

export function setFlags({ attributes, setAttributes }, key, value) {

	let { flags_items, flags_selected } = attributes;

	if (value) {
		flags_selected.push(key);
	} else {
		flags_selected = flags_selected.filter(flag => key !== flag);
	}

	const flags = flags_selected.join(',') || 'none';

	return setAttributes({ flags, flags_selected });
}

export function resetFlags({ setAttributes }) {
	return setAttributes({ flags: 'none', flags_selected: [] });
}