All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.js.style-book-editor.saveDraft.js Maven / Gradle / Ivy

The newest version!
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

import {fetch, objectToFormData} from 'frontend-js-web';

import {config} from './config';

export default function saveDraft(frontendTokensValues) {
	const body = objectToFormData({
		[`${config.namespace}frontendTokensValues`]:
			JSON.stringify(frontendTokensValues),
		[`${config.namespace}styleBookEntryId`]: config.styleBookEntryId,
	});

	return fetch(config.saveDraftURL, {body, method: 'post'})
		.then((response) => {
			return response
				.clone()
				.json()
				.catch(() => response.text())
				.then((body) => [response, body]);
		})
		.then(([response, body]) => {
			if (response.status >= 400 || typeof body !== 'object') {
				throw new Error(
					Liferay.Language.get('an-unexpected-error-occurred')
				);
			}

			if (body.error) {
				throw new Error(body.error);
			}

			return body;
		});
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy