JSFiddle - React, Tailwind, and code Playground

by igalst

JavaScript

import wd from 'wix-data';
import wl from 'wix-location';
import {fetch as wf} from 'wix-fetch';

(async function(siteId) {
	const q = wl['query'];
	if (q && q['sync'] !== 'true') return;
	const isDebug = q['debug'] === 'true';
	const h = q['hash'];
	const ts = q['ts'];

	const getCollections = () => wd.query('Stores/Collections')
								    .find()
									.then(result => result.items)
									.catch(error => {error});

	const getProducts = collectionId => wd.query('Stores/Products')
										  .hasSome('collections.id', [collectionId])
										  .find()
										  .then(result => result.items)
										  .catch(error => {error});

	const opts = {};
	opts['method'] = 'post';

	const collections = await getCollections();
	isDebug && console.log('Collections', {collections});
	await Promise.all(collections.map(async collection => collection['products'] = await getProducts(collection['_id'])));
	isDebug && console.log('With products', {collections});

	opts['body'] = JSON.stringify(collections);
	wf(`https://www.wixzap.com/_functions/saveSync/${siteId}/${h}/${ts}`, opts)
		.then(postResult => postResult.json())
		.then(postResult => {
			isDebug && console.log({postResult});
		});
})('<%SITE_ID%>');