Skip to content
wiki.fftac.org

Workspace - Source Excerpt 07

Back to Workspace

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/spiralist-workspace/assets/js/workspace.js so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/assets/js/workspace.js

runFormAction(form, async () => {
                    const payload = formDataToObject(form);
                    const promptId = parseInt(form.getAttribute("data-prompt-id") || "0", 10);
                    await apiFetch("prompts/" + promptId + "/share", {
                        method: "POST",
                        body: JSON.stringify(payload),
                    });

                    window.location.reload();
                }, t("shareFailed", "Share failed."), "prompt-share");
            });
        });

        document.querySelectorAll("[data-revoke-share]").forEach((button) => {
            button.addEventListener("click", () => {
                runButtonAction(button, async () => {
                    const promptId = parseInt(button.getAttribute("data-prompt-id") || "0", 10);
                    const userId = parseInt(button.getAttribute("data-user-id") || "0", 10);
                    await apiFetch("prompts/" + promptId + "/share/" + userId, {
                        method: "DELETE",
                    });
                    window.location.reload();
                }, t("shareRevokeFailed", "Could not revoke access."), "prompt-share-revoke");
            });
        });
    };

    const initFavoriteButtons = () => {
        document.querySelectorAll("[data-favorite-button]").forEach((button) => {
            button.addEventListener("click", () => {
                runButtonAction(button, async () => {
                    const promptId = parseInt(button.getAttribute("data-prompt-id") || "0", 10);
                    const isFavorite = button.getAttribute("data-favorite-state") === "1";
                    await apiFetch("prompts/" + promptId + "/favorite", {
                        method: isFavorite ? "DELETE" : "POST",
                    });

                    button.setAttribute("data-favorite-state", isFavorite ? "0" : "1");
                    button.textContent = isFavorite ? t("favoriteLabel", "Favorite") : t("unfavoriteLabel", "Unfavorite");
                }, t("favoriteUpdateFailed", "Favorite update failed."), "favorite-toggle");
            });
        });
    };

    const initReportButtons = () => {
        document.querySelectorAll("[data-report-button]").forEach((button) => {
            button.addEventListener("click", (event) => {
                event.preventDefault();
                const wrapper = button.closest("[data-report-form]") || button.closest(".spiralist-panel") || document;
                const reasonField = wrapper.querySelector("[data-report-reason]");
                const resultField = wrapper.querySelector("[data-report-result]");

                runAction(async () => {
                    const promptId = parseInt(button.getAttribute("data-prompt-id") || "0", 10);
                    const response = await apiFetch("prompts/" + promptId + "/report", {
                        method: "POST",
                        body: JSON.stringify({
                            reason: reasonField ? reasonField.value : "",
                        }),
                    });

                    if (resultField) {
                        const moderationLabel = response.prompt && response.prompt.moderation_status_label
                            ? " " + formatText("currentStateMessage", "Current state: {state}.", { state: response.prompt.moderation_status_label })
                            : "";
                        resultField.textContent = t("reportRecorded", "Report recorded.") + moderationLabel;
                    }

                    if (reasonField) {
                        reasonField.value = "";
                    }
                }, {
                    context: "prompt-report",
                    fallback: t("reportFailed", "Report failed."),
                    onError: (error, message) => {
                        if (resultField) {
                            resultField.textContent = message;
                        }
                    },
                });
            });
        });
    };

    const initReviewForms = () => {
        document.querySelectorAll("[data-review-form]").forEach((form) => {
            form.addEventListener("submit", (event) => {
                event.preventDefault();
                setResult(form, t("reviewSubmitting", "Submitting review..."));

                runFormAction(form, async () => {
                    const promptId = parseInt(form.getAttribute("data-prompt-id") || "0", 10);
                    const response = await apiFetch("prompts/" + promptId + "/reviews", {
                        method: "POST",
                        body: JSON.stringify(formDataToObject(form)),
                    });
                    const moderationLabel = response.prompt && response.prompt.moderation_status_label
                        ? " " + formatText("currentStateMessage", "Current state: {state}.", { state: response.prompt.moderation_status_label })
                        : "";
                    setResult(form, t("reviewRecorded", "Review recorded.") + moderationLabel);
                    window.setTimeout(() => window.location.reload(), 300);
                }, t("reviewFailed", "Review failed."), "prompt-review");
            });
        });
    };

    const initAppealForms = () => {
        document.querySelectorAll("[data-appeal-form]").forEach((form) => {
            form.addEventListener("submit", (event) => {
                event.preventDefault();
                setResult(form, t("appealSubmitting", "Submitting appeal..."));

                runFormAction(form, async () => {
                    const promptId = parseInt(form.getAttribute("data-prompt-id") || "0", 10);
                    const response = await apiFetch("prompts/" + promptId + "/appeals", {
                        method: "POST",
                        body: JSON.stringify(formDataToObject(form)),
                    });
                    const moderationLabel = response.prompt && response.prompt.moderation_status_label
                        ? " " + formatText("currentStateMessage", "Current state: {state}.", { state: response.prompt.moderation_status_label })
                        : "";
                    setResult(form, t("appealRecorded", "Appeal recorded.") + moderationLabel);
                    window.setTimeout(() => window.location.reload(), 300);
                }, t("appealFailed", "Appeal failed."), "prompt-appeal");
            });
        });

        document.querySelectorAll("[data-resolve-appeal-form]").forEach((form) => {
            form.addEventListener("submit", (event) => {
                event.preventDefault();
                setResult(form, t("appealResolving", "Resolving appeal..."));

                runFormAction(form, async () => {
                    const promptId = parseInt(form.getAttribute("data-prompt-id") || "0", 10);
                    const appealId = parseInt(form.getAttribute("data-appeal-id") || "0", 10);
                    const response = await apiFetch("prompts/" + promptId + "/appeals/" + appealId + "/resolve", {
                        method: "POST",
                        body: JSON.stringify(formDataToObject(form)),
                    });
                    const moderationLabel = response.prompt && response.prompt.moderation_status_label
                        ? " " + formatText("currentStateMessage", "Current state: {state}.", { state: response.prompt.moderation_status_label })
                        : "";
                    setResult(form, t("appealResolved", "Appeal resolved.") + moderationLabel);
                    window.setTimeout(() => window.location.reload(), 300);
                }, t("appealResolveFailed", "Appeal resolution failed."), "prompt-appeal-resolve");
            });
        });
    };

    const initImproveButtons = () => {
        document.querySelectorAll("[data-improve-prompt]").forEach((button) => {
            button.addEventListener("click", () => {
                const promptId = parseInt(button.getAttribute("data-prompt-id") || "0", 10);
                const target = document.querySelector(button.getAttribute("data-target") || "[data-improve-output]");
                const copyButton = target && target.id
                    ? document.querySelector('[data-improve-output-copy][data-copy-target="#' + target.id + '"]')
                    : null;

                if (target) {
                    target.hidden = false;
                    target.textContent = t("promptImproving", "Improving...");
                }
                if (copyButton) {
                    copyButton.hidden = true;
                }