| 1 |
import axios from "axios"; |
| 2 |
import { jsonStringify } from "../../blocks/shared/helpFn"; |
| 3 |
|
| 4 |
const useCreateBlockPage = ({ actionType, templateType, id, status, attributes, setAttributes }) => { |
| 5 |
const data = new FormData(); |
| 6 |
const queryData = { |
| 7 |
actionType: actionType, |
| 8 |
templateId: id, |
| 9 |
status: status, |
| 10 |
templateType: templateType, |
| 11 |
}; |
| 12 |
|
| 13 |
data.append("nonce", sp_pcp_block_settings.nonce); |
| 14 |
data.append("action", "sp_smart_post_create_page"); |
| 15 |
data.append("data", jsonStringify(queryData)); |
| 16 |
|
| 17 |
const fetchApi = async (data) => { |
| 18 |
try { |
| 19 |
const response = await axios.post(ajaxurl, data); |
| 20 |
if ("createBlankTemplate" === actionType) { |
| 21 |
const { newID } = response.data; |
| 22 |
location.href = `${sp_pcp_block_settings.homeUrl}wp-admin/post.php?post=${newID}&action=edit`; |
| 23 |
} else { |
| 24 |
setAttributes({ |
| 25 |
...attributes, |
| 26 |
showTemplate: true, |
| 27 |
templateLists: response.data?.templateLists, |
| 28 |
}); |
| 29 |
} |
| 30 |
} catch (error) { |
| 31 |
console.error("Error fetching posts:", error.message); |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
return fetchApi(data); |
| 36 |
}; |
| 37 |
|
| 38 |
export default useCreateBlockPage; |
| 39 |
|