| 1 |
import { useLaunchDataStore } from '@auto-launch/state/launch-data'; |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
import { Spinner } from '@wordpress/components'; |
| 4 |
import { useState } from '@wordpress/element'; |
| 5 |
import { __, sprintf } from '@wordpress/i18n'; |
| 6 |
import { chevronRight, Icon } from '@wordpress/icons'; |
| 7 |
|
| 8 |
export const RestartLaunchModal = ({ pages }) => { |
| 9 |
const { resetSiteInformation } = window.extLaunchData; |
| 10 |
const { navigationIds, templatePartsIds, pageWithTitleTemplateId } = |
| 11 |
resetSiteInformation || {}; |
| 12 |
const globalStylesPostID = window.extSharedData.globalStylesPostID; |
| 13 |
|
| 14 |
const { reset: resetLaunchData } = useLaunchDataStore(); |
| 15 |
const [processing, setProcessing] = useState(false); |
| 16 |
const handleExit = () => { |
| 17 |
window.location.href = `${window.extSharedData.adminUrl}admin.php?page=extendify-assist`; |
| 18 |
}; |
| 19 |
|
| 20 |
const handleOk = async () => { |
| 21 |
setProcessing(true); |
| 22 |
resetLaunchData({ exclude: ['descriptionBackup'] }); |
| 23 |
// remove any workflow info |
| 24 |
localStorage.removeItem( |
| 25 |
`extendify-agent-workflows-${window.extSharedData.siteId}`, |
| 26 |
); |
| 27 |
for (const pageId of pages) { |
| 28 |
try { |
| 29 |
await apiFetch({ |
| 30 |
path: `/wp/v2/pages/${pageId}`, |
| 31 |
method: 'DELETE', |
| 32 |
}); |
| 33 |
} catch (responseError) { |
| 34 |
console.warn( |
| 35 |
`delete pages failed to delete a page (id: ${pageId}) with the following error`, |
| 36 |
responseError, |
| 37 |
); |
| 38 |
} |
| 39 |
} |
| 40 |
// They could be posts |
| 41 |
for (const pageId of pages) { |
| 42 |
try { |
| 43 |
await apiFetch({ |
| 44 |
path: `/wp/v2/posts/${pageId}`, |
| 45 |
method: 'DELETE', |
| 46 |
}); |
| 47 |
} catch (responseError) { |
| 48 |
console.warn( |
| 49 |
`delete posts failed to delete a page (id: ${pageId}) with the following error`, |
| 50 |
responseError, |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
// delete the wp_navigation posts created by Launch |
| 55 |
for (const navigationId of navigationIds || []) { |
| 56 |
try { |
| 57 |
await apiFetch({ |
| 58 |
path: `/wp/v2/navigation/${navigationId}`, |
| 59 |
method: 'DELETE', |
| 60 |
}); |
| 61 |
} catch (responseError) { |
| 62 |
console.warn( |
| 63 |
`delete navigation failed to delete a navigation (id: ${navigationId}) with the following error`, |
| 64 |
responseError, |
| 65 |
); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
for (const template of templatePartsIds || []) { |
| 70 |
try { |
| 71 |
await apiFetch({ |
| 72 |
path: `/wp/v2/template-parts/${template}?force=true`, |
| 73 |
method: 'DELETE', |
| 74 |
}); |
| 75 |
} catch (responseError) { |
| 76 |
console.warn( |
| 77 |
`delete template failed to delete template (id: ${template}) with the following error`, |
| 78 |
responseError, |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
try { |
| 84 |
if (pageWithTitleTemplateId) { |
| 85 |
await apiFetch({ |
| 86 |
path: `/wp/v2/templates/${pageWithTitleTemplateId}?force=true`, |
| 87 |
method: 'DELETE', |
| 88 |
}); |
| 89 |
} |
| 90 |
} catch (responseError) { |
| 91 |
console.warn('Failed to delete page-with-title template:', responseError); |
| 92 |
} |
| 93 |
|
| 94 |
// Reset global styles |
| 95 |
try { |
| 96 |
if (globalStylesPostID) { |
| 97 |
await apiFetch({ |
| 98 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 99 |
method: 'POST', |
| 100 |
body: JSON.stringify({ settings: {}, styles: {} }), |
| 101 |
}); |
| 102 |
} |
| 103 |
} catch (styleResetError) { |
| 104 |
console.warn( |
| 105 |
'Failed to reset global styles with the following error:', |
| 106 |
styleResetError, |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
window.location.reload(); |
| 111 |
}; |
| 112 |
|
| 113 |
return ( |
| 114 |
<div className="flex w-full flex-col gap-6 p-6"> |
| 115 |
<h2 className="text-2xl leading-8 text-left text-gray-900 font-medium py-0 m-0"> |
| 116 |
{__('Start Over?', 'extendify-local')} |
| 117 |
</h2> |
| 118 |
|
| 119 |
<div className="flex flex-col gap-3"> |
| 120 |
<p className="text-base leading-6 text-left text-gray-900 m-0"> |
| 121 |
{__( |
| 122 |
'It looks like you have been here before. We need to clean up some things before we can continue.', |
| 123 |
'extendify-local', |
| 124 |
)} |
| 125 |
</p> |
| 126 |
<p className="text-base leading-6 text-left text-gray-900 font-medium m-0"> |
| 127 |
{sprintf( |
| 128 |
// translators: %3$s is the number of old pages |
| 129 |
__('%s pages/posts will be deleted.', 'extendify-local'), |
| 130 |
pages.length, |
| 131 |
)} |
| 132 |
</p> |
| 133 |
</div> |
| 134 |
|
| 135 |
<div className="flex justify-between items-center mt-2"> |
| 136 |
<Nav |
| 137 |
handleOk={handleOk} |
| 138 |
handleExit={handleExit} |
| 139 |
processing={processing} |
| 140 |
/> |
| 141 |
</div> |
| 142 |
</div> |
| 143 |
); |
| 144 |
}; |
| 145 |
|
| 146 |
const Nav = ({ handleOk, handleExit, processing }) => { |
| 147 |
return ( |
| 148 |
<> |
| 149 |
<button |
| 150 |
type="button" |
| 151 |
onClick={handleExit} |
| 152 |
disabled={processing} |
| 153 |
className="inline-flex items-center gap-2 rounded-full ring-1 ring-gray-800 px-3 py-2.5 text-sm leading-5 font-normal text-gray-800 transition-colors hover:bg-gray-600/5 disabled:opacity-40" |
| 154 |
> |
| 155 |
<span className="px-1">{__('Exit', 'extendify-local')}</span> |
| 156 |
</button> |
| 157 |
{processing ? ( |
| 158 |
<button |
| 159 |
type="button" |
| 160 |
disabled |
| 161 |
className="inline-flex items-center justify-center gap-2 rounded-full border-0 bg-design-main px-3 py-2 text-sm leading-5 font-normal text-design-text disabled:opacity-40" |
| 162 |
> |
| 163 |
<Spinner className="m-0" /> |
| 164 |
<span className="px-1">{__('Processing...', 'extendify-local')}</span> |
| 165 |
</button> |
| 166 |
) : ( |
| 167 |
<button |
| 168 |
type="button" |
| 169 |
onClick={handleOk} |
| 170 |
className="inline-flex items-center justify-center rounded-full border-0 bg-design-main px-3 py-2 text-sm leading-5 font-normal text-design-text group hover:opacity-90 transition-opacity" |
| 171 |
> |
| 172 |
<span className="px-1"> |
| 173 |
{__('Delete and start over', 'extendify-local')} |
| 174 |
</span> |
| 175 |
<Icon fill="currentColor" icon={chevronRight} size={24} /> |
| 176 |
</button> |
| 177 |
)} |
| 178 |
</> |
| 179 |
); |
| 180 |
}; |
| 181 |
|