| 1 |
import {__} from "@wordpress/i18n"; |
| 2 |
import {FormBuilder, useBuilderContext} from "quickbuilder"; |
| 3 |
import React, {useCallback, useEffect} from "react"; |
| 4 |
import "react-toastify/dist/ReactToastify.css"; |
| 5 |
import {apiFetch, licenseInactiveAlert, proAlert} from "../../utils"; |
| 6 |
import toastAlert from "../../utils/toastAlert"; |
| 7 |
import {doAction} from "@wordpress/hooks"; |
| 8 |
|
| 9 |
const setToolbarMenuUrl = ({builtin_doc_page, docs_slug, docs_page}) => { |
| 10 |
let hrefLink = document.querySelector('#wp-admin-bar-view-docs .ab-item'); |
| 11 |
|
| 12 |
if (hrefLink == null) { |
| 13 |
let adminBarSiteNameMenu = document.querySelector('#wp-admin-bar-site-name'); |
| 14 |
if (adminBarSiteNameMenu != null) { |
| 15 |
let hrefLinkLi = document.createElement('li'); |
| 16 |
hrefLinkLi.id = 'wp-admin-bar-view-docs'; |
| 17 |
|
| 18 |
hrefLink = document.createElement('a'); |
| 19 |
hrefLink.classList.add('ab-item'); |
| 20 |
hrefLink.textContent = 'Visit Documentation'; |
| 21 |
|
| 22 |
let windowUrl = new URL(window.location.href); |
| 23 |
hrefLink.href = windowUrl.origin; |
| 24 |
|
| 25 |
hrefLinkLi.append(hrefLink); |
| 26 |
adminBarSiteNameMenu.querySelector('#wp-admin-bar-site-name-default').append(hrefLinkLi); |
| 27 |
// adminBarSiteNameMenu.classList.add('hover'); |
| 28 |
} else { |
| 29 |
return; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
let url = new URL(hrefLink.href); |
| 34 |
let pathname = docs_slug?.length > 0 ? docs_slug : 'docs'; |
| 35 |
|
| 36 |
if (builtin_doc_page) { |
| 37 |
url.searchParams.delete('page_id'); |
| 38 |
} else if (!builtin_doc_page && docs_page > 0) { |
| 39 |
pathname = ''; |
| 40 |
url.searchParams.set('page_id', docs_page) |
| 41 |
} |
| 42 |
|
| 43 |
url.pathname = pathname; |
| 44 |
|
| 45 |
hrefLink.href = url.toString(); |
| 46 |
} |
| 47 |
|
| 48 |
const QuickBuild = (props) => { |
| 49 |
const builder = useBuilderContext(); |
| 50 |
|
| 51 |
useEffect(() => { |
| 52 |
builder.registerAlert("pro_alert", proAlert); |
| 53 |
builder.registerAlert("toast", toastAlert); |
| 54 |
builder.registerAlert("licenseInactiveAlert", licenseInactiveAlert); |
| 55 |
}, []); |
| 56 |
|
| 57 |
builder.submit.onSubmit = useCallback((event, context) => { |
| 58 |
if (context?.isSubmitting) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
context.setSubmitting(true); |
| 63 |
|
| 64 |
setToolbarMenuUrl(context.values); |
| 65 |
|
| 66 |
doAction('betterdocs_settings_saved', context); |
| 67 |
|
| 68 |
// Tell the server which tab is being saved so tab-scoped validation |
| 69 |
// (e.g. the AI min-token floor) only runs for the relevant tab and |
| 70 |
// doesn't block saves from unrelated settings pages. |
| 71 |
// Prefer the live URL hash (always current) over builder.config.active, which is |
| 72 |
// captured at mount and can be stale (defaults to 'tab-general'), causing tab-scoped |
| 73 |
// validation like the AI min-token floor to be skipped. Mirror hashToTabId(): strip |
| 74 |
// the leading '#', then prefix 'tab-' unless the slug already has it. |
| 75 |
const hashSlug = (window.location.hash || "").replace(/^#/, "").trim(); |
| 76 |
const activeTab = hashSlug |
| 77 |
? (hashSlug.startsWith("tab-") ? hashSlug : `tab-${hashSlug}`) |
| 78 |
: (builder?.config?.active || ""); |
| 79 |
|
| 80 |
apiFetch |
| 81 |
.post("settings", { ...context.values, _active_tab: activeTab }) |
| 82 |
.then((res) => { |
| 83 |
if (res?.success) { |
| 84 |
toastAlert( |
| 85 |
"success", |
| 86 |
__('Changes Saved Successfully.', "betterdocs") |
| 87 |
); |
| 88 |
} else { |
| 89 |
toastAlert( |
| 90 |
res?.code === 'nothing_changed' ? "info" : "error", |
| 91 |
__(res?.message ?? 'Something went wrong.', "betterdocs") |
| 92 |
); |
| 93 |
} |
| 94 |
}) |
| 95 |
.catch((err) => { |
| 96 |
toastAlert("error", __('Something went wrong.', "betterdocs")); |
| 97 |
}).finally(() => { |
| 98 |
context.setSubmitting(false); |
| 99 |
}); |
| 100 |
}, []); |
| 101 |
|
| 102 |
return ( |
| 103 |
<div className="betterdocs-quick-builder-wrapper"> |
| 104 |
<FormBuilder {...builder} /> |
| 105 |
</div> |
| 106 |
); |
| 107 |
}; |
| 108 |
|
| 109 |
export default QuickBuild; |
| 110 |
|