| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
|
| 5 |
/** |
| 6 |
* WordPress dependencies |
| 7 |
*/ |
| 8 |
import { __ } from "@wordpress/i18n"; |
| 9 |
import { createContext, useState } from "@wordpress/element"; |
| 10 |
import { useEntityRecords } from "@wordpress/core-data"; |
| 11 |
|
| 12 |
/** |
| 13 |
* Internal dependencies |
| 14 |
*/ |
| 15 |
import { useApiFetch } from "sdk/utils"; |
| 16 |
|
| 17 |
export const useGlobalData = () => { |
| 18 |
const { loading, error, data: { data: docs } = {} } = useApiFetch( |
| 19 |
"boldblocks/v1/getDocs" |
| 20 |
); |
| 21 |
|
| 22 |
let nonce = ""; |
| 23 |
let purged = false; |
| 24 |
try { |
| 25 |
nonce = CBBSettings?.nonce; |
| 26 |
purged = CBBSettings?.isPurgedCache; |
| 27 |
} catch (error) { |
| 28 |
log("The nonce is not defined!", "error"); |
| 29 |
} |
| 30 |
const [isPurged, setIsPurged] = useState(purged); |
| 31 |
|
| 32 |
const messages = { |
| 33 |
UpdateSettings: __("Update Settings", "content-blocks-builder"), |
| 34 |
Success: __("Setting Saved!", "content-blocks-builder"), |
| 35 |
Error: __( |
| 36 |
"Something went wrong, please contact the author for support!", |
| 37 |
"content-blocks-builder" |
| 38 |
), |
| 39 |
}; |
| 40 |
|
| 41 |
const { |
| 42 |
records: pages, |
| 43 |
isResolving: isResolvingPages, |
| 44 |
} = useEntityRecords("postType", "page", { per_page: 100 }); |
| 45 |
|
| 46 |
return { |
| 47 |
Docs: { loading, error, docs }, |
| 48 |
Debug: { nonce, isPurged, setIsPurged }, |
| 49 |
Messages: messages, |
| 50 |
pages, |
| 51 |
isResolvingPages, |
| 52 |
}; |
| 53 |
}; |
| 54 |
|
| 55 |
export const DataContext = createContext(); |
| 56 |
|