| 1 |
import apiFetch from "@wordpress/api-fetch"; |
| 2 |
import { useEffect, useState } from "@wordpress/element"; |
| 3 |
|
| 4 |
const useIconList = () => { |
| 5 |
const [icon, setIcon] = useState([]); |
| 6 |
|
| 7 |
const fetchIconList = async () => { |
| 8 |
await apiFetch({ |
| 9 |
path: `/smart-post/v2/icon-list`, |
| 10 |
method: "POST", |
| 11 |
headers: { |
| 12 |
"X-WP-Nonce": sp_smart_post_block_localize.ajaxNonce, |
| 13 |
}, |
| 14 |
data: { |
| 15 |
someParam: "example", |
| 16 |
}, |
| 17 |
}) |
| 18 |
.then((response) => { |
| 19 |
setIcon(JSON.parse(response)); |
| 20 |
}) |
| 21 |
.catch((error) => { |
| 22 |
console.error("Error fetching icon list:", error); |
| 23 |
}); |
| 24 |
}; |
| 25 |
|
| 26 |
useEffect(() => { |
| 27 |
if (icon.length < 1) { |
| 28 |
fetchIconList(); |
| 29 |
} |
| 30 |
}, [icon]); |
| 31 |
|
| 32 |
return icon; |
| 33 |
}; |
| 34 |
|
| 35 |
export default useIconList; |
| 36 |
|