/** @odoo-module **/ import { patch } from "@web/core/utils/patch"; import { useService } from "@web/core/utils/hooks"; import { rpc } from "@web/core/network/rpc"; import { Component, xml, useRef, useState } from "@odoo/owl"; import { AddSnippetDialog } from "@web_editor/js/editor/add_snippet_dialog"; // Define a custom Owl component for the AI Prompt Modal class AIPromptModal extends Component { setup() { this.orm = useService("orm"); this.notification = useService("notification"); this.textAreaRef = useRef('textAreaRef'); this.nameRef = useRef('snippetNameRef'); this.state = useState({ isLoading: false, }); } // Close the modal (disabled if a request is loading) onClose() { if (this.state.isLoading) { return; } this.props.close(); } async onSubmit() { const promptValue = this.textAreaRef.el.value; const snippetName = this.nameRef.el.value; if (!promptValue || !snippetName) { this.notification.add( "Please fill in both the prompt and snippet name", { type: "warning" } ); return; } try { this.state.isLoading = true; const result = await rpc('/website/generate_snippet', { prompt: promptValue, name: snippetName }); if (result.error) { this.notification.add(result.error, { type: "danger" }); } else { this.notification.add( "Snippet generated successfully! Refreshing snippet panel...", { type: "success" } ); window.location.reload(); } } catch (error) { this.notification.add( "Failed to generate snippet: " + error, { type: "danger" } ); } finally { this.state.isLoading = false; } this.props.close(); } } // Define the template for the modal AIPromptModal.template = xml`