import React, {FC, useEffect, useState} from "react"; import {colors} from "./Colors"; import {EmulatedButton} from "./EmulatedButton"; import {__} from "../globals"; import {useNodesStore} from "./store"; import {AppExporter} from "./export/AppExporter"; import {GroveExporter} from "./export/GroveExporter"; import {ExportableDataFormatPreset, ExportableDataFormatServer} from "./export/AppImporter"; import js from "@eslint/js"; import toast from "react-hot-toast"; import classNames from "classnames"; import {Superballs} from 'ldrs/react' import 'ldrs/react/Superballs.css' export type SaveButtonProps = { compact?: boolean; } export const SaveButton: FC = ({compact = false}) => { const [isSaving, setIsSaving] = useState(false); useEffect(() => { // Check if WordPress success notice exists in the DOM const wordpressNotice = document.querySelector('.notice.notice-success.updated'); if (wordpressNotice) { showSavedNotification(); } }, []); return { if (!isSaving) { setIsSaving(true); save(); } }}> {false &&
}
{__('Save')}
} function save() { const state = useNodesStore.getState(); const appExporter = new AppExporter(state, new GroveExporter(state)) let {version, grove, testablePartialRelations, preloadedData, preconditions, predates, autoApply, model} = appExporter.export('server') as ExportableDataFormatServer const serializedPreconditions = JSON.stringify(preconditions ?? null) const serializedPredates = JSON.stringify(predates ?? null) return triggerSaveWithData({ version: version.toString(), grove: JSON.stringify(grove), testable_partial_relations: JSON.stringify(testablePartialRelations), preloaded_data: JSON.stringify(preloadedData), preconditions: serializedPreconditions, predates: serializedPredates, coupon_auto_apply_is_enabled: autoApply, model }); } function triggerSaveWithData(dataEntries: Record) { const form = document.getElementById('post') as HTMLFormElement; if (form) { const originalSubmit = form.onsubmit; form.onsubmit = function (e) { for (let [key, value] of Object.entries(dataEntries)) { if (value === undefined) { continue; } key = `couponsplus_${key}`; let input = document.getElementById(`cp-hidden-${key}`) as HTMLInputElement; if (input) { input.value = value; } else { input = document.createElement('input'); input.type = 'hidden'; input.name = key; input.value = value; input.id = `cp-hidden-${key}`; form.appendChild(input); } } form.onsubmit = originalSubmit; return true; }; const publishButton = document.getElementById('publish') as HTMLButtonElement; if (publishButton) { publishButton.click(); } } } function showSavedNotification() { toast.custom((t) => (
{__('Saved')}
), { id: 'saved-notification', duration: 3000, }) }