| 1 |
import { Button } from "@wordpress/components"; |
| 2 |
import { __ } from "@wordpress/i18n"; |
| 3 |
import { useBuilderContext } from "quickbuilder"; |
| 4 |
import React, { useCallback, useState } from "react"; |
| 5 |
import { apiFetch } from "../../utils"; |
| 6 |
import toastAlert from "../../utils/toastAlert"; |
| 7 |
import { getLayoutPicks, hasLayoutPicks } from "./layoutStore"; |
| 8 |
|
| 9 |
const Launch = (props) => { |
| 10 |
const [isSubmit, setIsSubmit] = useState(false); |
| 11 |
const context = useBuilderContext(); |
| 12 |
|
| 13 |
const handleSubmit = useCallback( |
| 14 |
(event) => { |
| 15 |
if (context?.isSubmitting) { |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
context.setSubmitting(true); |
| 20 |
|
| 21 |
if(context?.values?.enable_sample_data) { |
| 22 |
apiFetch.post("create-sample-docs", {action:'create-dummy-data'}); |
| 23 |
} |
| 24 |
|
| 25 |
// Persist the chosen Customize-step layouts as theme_mods (only if the |
| 26 |
// user visited that step), alongside the rest of the wizard's save. |
| 27 |
if (hasLayoutPicks()) { |
| 28 |
apiFetch.post("customize/layouts", { layouts: getLayoutPicks() }); |
| 29 |
} |
| 30 |
|
| 31 |
// Dark mode is a client cookie (utils/darkMode), never a saved setting — |
| 32 |
// do NOT persist it here, or it would write `betterdocs_settings` and the |
| 33 |
// theme would diverge from the cookie the other screens read. |
| 34 |
apiFetch |
| 35 |
.post("settings", { ...context.values }) |
| 36 |
.then((res) => { |
| 37 |
if (res?.success) { |
| 38 |
document.cookie = "betterdocs_insights_allowed=; expires=Thu, 01 Jan 2000 00:00:00 UTC; path=/;"; |
| 39 |
// Setup complete — clear the persisted sample-docs preview. |
| 40 |
try { localStorage.removeItem("betterdocs_setup_generated_docs"); } catch (e) {} |
| 41 |
toastAlert( |
| 42 |
"success", |
| 43 |
__('Changes Saved Successfully.', "betterdocs") |
| 44 |
).then(() => { |
| 45 |
const url = new URL(window.location.href); |
| 46 |
url.search = ""; |
| 47 |
url.searchParams.set("page", "betterdocs-admin"); |
| 48 |
window.location.replace(url.href); |
| 49 |
}); |
| 50 |
} else { |
| 51 |
toastAlert( |
| 52 |
res?.code === "nothing_changed" ? "info" : "error", |
| 53 |
__( |
| 54 |
res?.message ?? 'Something went wrong.', |
| 55 |
"betterdocs" |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
}) |
| 60 |
.catch((err) => { |
| 61 |
toastAlert( |
| 62 |
"error", |
| 63 |
__('Something went wrong.', "betterdocs") |
| 64 |
); |
| 65 |
}) |
| 66 |
.finally(() => { |
| 67 |
context.setSubmitting(false); |
| 68 |
if (context?.config?.save_locally) { |
| 69 |
localStorage.removeItem("quickbuilder_active_tab"); |
| 70 |
} |
| 71 |
}); |
| 72 |
}, |
| 73 |
[context.values] |
| 74 |
); |
| 75 |
|
| 76 |
return ( |
| 77 |
<> |
| 78 |
<Button |
| 79 |
disabled={isSubmit} |
| 80 |
className="wprf-btn wprf-step-btn-publish" |
| 81 |
onClick={handleSubmit} |
| 82 |
> |
| 83 |
{isSubmit && __("Finishing...", "betterdocs")} |
| 84 |
{!isSubmit && __("Finish", "betterdocs")} |
| 85 |
</Button> |
| 86 |
</> |
| 87 |
); |
| 88 |
}; |
| 89 |
|
| 90 |
export default Launch; |
| 91 |
|