Router.js
179 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import { Box, Spinner, useToast } from '@chakra-ui/react'; |
| 5 | import { useQuery } from '@tanstack/react-query'; |
| 6 | import apiFetch from '@wordpress/api-fetch'; |
| 7 | import { __ } from '@wordpress/i18n'; |
| 8 | import { useEffect } from 'react'; |
| 9 | import { Route, Routes, useLocation } from 'react-router-dom'; |
| 10 | |
| 11 | /** |
| 12 | * Internal Dependencies |
| 13 | */ |
| 14 | import { Header } from '../components'; |
| 15 | import { |
| 16 | Analytics, |
| 17 | FreeVsPro, |
| 18 | Help, |
| 19 | Modules, |
| 20 | Products, |
| 21 | Settings, |
| 22 | SiteAssistant, |
| 23 | } from '../screens'; |
| 24 | import SiteAssistantSkeleton from '../skeleton/SiteAssistantSkeleton'; |
| 25 | |
| 26 | const Router = () => { |
| 27 | const toast = useToast(); |
| 28 | const location = useLocation(); |
| 29 | |
| 30 | /* global _EVF_DASHBOARD_ */ |
| 31 | const { |
| 32 | isPro, |
| 33 | settingsURL, |
| 34 | evfRestApiNonce, |
| 35 | restURL, |
| 36 | allStepsCompleted, |
| 37 | adminURL, |
| 38 | } = |
| 39 | typeof _EVF_DASHBOARD_ !== 'undefined' |
| 40 | ? _EVF_DASHBOARD_ |
| 41 | : { |
| 42 | isPro: false, |
| 43 | settingsURL: '', |
| 44 | evfRestApiNonce: '', |
| 45 | restURL: '', |
| 46 | adminURL: '', |
| 47 | }; |
| 48 | |
| 49 | const siteAssistantQuery = useQuery({ |
| 50 | queryKey: ['siteAssistant'], |
| 51 | queryFn: async () => { |
| 52 | const response = await apiFetch({ |
| 53 | path: `${restURL}everest-forms/v1/site-assistant`, |
| 54 | method: 'GET', |
| 55 | headers: { |
| 56 | 'X-WP-Nonce': evfRestApiNonce, |
| 57 | }, |
| 58 | }); |
| 59 | return response; |
| 60 | }, |
| 61 | cacheTime: Infinity, |
| 62 | staleTime: Infinity, |
| 63 | retry: 1, |
| 64 | onError: (error) => { |
| 65 | console.error('Error fetching site assistant data:', error); |
| 66 | toast({ |
| 67 | title: __('Error', 'everest-forms'), |
| 68 | description: __('Failed to load setup status.', 'everest-forms'), |
| 69 | status: 'error', |
| 70 | duration: 3000, |
| 71 | isClosable: true, |
| 72 | }); |
| 73 | }, |
| 74 | }); |
| 75 | |
| 76 | const isAllStepsCompleted = siteAssistantQuery?.isLoading |
| 77 | ? Boolean(allStepsCompleted === '1') |
| 78 | : siteAssistantQuery?.data?.data?.all_steps_completed; |
| 79 | |
| 80 | const RedirectToPhpPage = ({ page }) => { |
| 81 | useEffect(() => { |
| 82 | let cleanURL = adminURL; |
| 83 | if (cleanURL.endsWith('/')) { |
| 84 | cleanURL = cleanURL.slice(0, -1); |
| 85 | } |
| 86 | if (cleanURL.endsWith('/admin.php')) { |
| 87 | cleanURL = cleanURL.slice(0, -10); |
| 88 | } |
| 89 | |
| 90 | window.location.href = `${cleanURL}/admin.php?page=${page}`; |
| 91 | }, [page]); |
| 92 | |
| 93 | return null; |
| 94 | }; |
| 95 | |
| 96 | const DefaultRedirect = () => { |
| 97 | // Show Site Assistant skeleton while fetching data |
| 98 | if (siteAssistantQuery.isLoading) { |
| 99 | return <SiteAssistantSkeleton />; |
| 100 | } |
| 101 | |
| 102 | if (isAllStepsCompleted) { |
| 103 | if (isPro) { |
| 104 | return <RedirectToPhpPage page="evf-analytics" />; |
| 105 | } else { |
| 106 | return <RedirectToPhpPage page="evf-entries" />; |
| 107 | } |
| 108 | } else { |
| 109 | return <SiteAssistant siteAssistantQuery={siteAssistantQuery} />; |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | // Determine if we should show the loader (only when redirecting from root path) |
| 114 | const isOnRootPath = location.pathname === '/' || location.pathname === ''; |
| 115 | const showLoader = |
| 116 | isOnRootPath && |
| 117 | ((siteAssistantQuery.isLoading && Boolean(allStepsCompleted === '1')) || |
| 118 | (!siteAssistantQuery.isLoading && isAllStepsCompleted)); |
| 119 | |
| 120 | // Show header on all pages except when redirecting from root |
| 121 | const shouldShowHeader = !(isOnRootPath && isAllStepsCompleted); |
| 122 | |
| 123 | // If showing loader, show spinner and hidden routes for redirect |
| 124 | if (showLoader) { |
| 125 | return ( |
| 126 | <> |
| 127 | <Box |
| 128 | display="flex" |
| 129 | justifyContent="center" |
| 130 | alignItems="center" |
| 131 | minHeight="calc(100vh - 32px)" |
| 132 | bg="white" |
| 133 | > |
| 134 | <Spinner |
| 135 | thickness="4px" |
| 136 | speed="0.65s" |
| 137 | emptyColor="gray.200" |
| 138 | color="primary.500" |
| 139 | size="xl" |
| 140 | /> |
| 141 | </Box> |
| 142 | <Box display="none"> |
| 143 | <Routes> |
| 144 | <Route path="/" element={<DefaultRedirect />} /> |
| 145 | <Route path="*" element={<DefaultRedirect />} /> |
| 146 | </Routes> |
| 147 | </Box> |
| 148 | </> |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | return ( |
| 153 | <> |
| 154 | {shouldShowHeader && <Header hideSiteAssistant={isAllStepsCompleted} />} |
| 155 | <Routes> |
| 156 | <Route path="/" element={<DefaultRedirect />} /> |
| 157 | <Route |
| 158 | path="/analytics" |
| 159 | element={ |
| 160 | isPro ? ( |
| 161 | <RedirectToPhpPage page="evf-analytics" /> |
| 162 | ) : ( |
| 163 | <Analytics /> |
| 164 | ) |
| 165 | } |
| 166 | /> |
| 167 | <Route path="/settings" element={<Settings to={settingsURL} />} /> |
| 168 | <Route path="/features" element={<Modules />} /> |
| 169 | {!isPro && <Route path="/free-vs-pro" element={<FreeVsPro />} />} |
| 170 | <Route path="/help" element={<Help />} /> |
| 171 | <Route path="/products" element={<Products />} /> |
| 172 | <Route path="*" element={<DefaultRedirect />} /> |
| 173 | </Routes> |
| 174 | </> |
| 175 | ); |
| 176 | }; |
| 177 | |
| 178 | export default Router; |
| 179 |