3.2 KiB
3.2 KiB
Implementation Details
React‑Auth
- File Structure
src/auth/AuthContext.tsx– ProvidesAuthContextandAuthProvider.src/auth/useAuth.ts– Custom hook returning context values and actions.src/auth/ProtectedRoute.tsx– Wrapper component that checksisAuthenticatedand redirects.src/auth/api.ts– Thin wrapper aroundaxiosfor login, logout, refresh.
- Logic
- On
login, POST credentials → storeaccessToken& user info in context andlocalStorage. - An
axiosinterceptor attaches the token to every request. refreshTokenruns on 401 responses; it attempts a silent refresh and updates the context.logoutclears context and storage, navigating back to/login.
- On
- UI Components
LoginForm– MUITextFields, validation, and submit handling.AuthLoading– Full‑screen spinner displayed while session restoration runs on app boot.
React‑OpenAPI
- Core Files
src/react-openapi/types/config.ts– Already definesResourceFieldwithdisplayFormat.src/react-openapi/utils/options.ts– HelperresolveTemplateparses{{field}}placeholders using the item data.src/react-openapi/components/EnhancedTable.tsx– Renders a MUIDataGrid. UsesgetFormattedDisplayValueto compute readable labels for relation fields based ondisplayFormat.src/react-openapi/components/FilterBar.tsx– Generates filter inputs; extracts option labels using the samedisplayFormatlogic.
- Data Fetching
useResource(resourceName)– TanStackuseQueryhook that builds the endpoint URL fromconfig.endpointand fetches data via the shared Axios instance.
- Customization
componentsprop passed toEnhancedTable/FilterBarallows overriding cell renderers, filter widgets, and action buttons.
- Error Handling
- Centralized error toast (
useToast) displays API errors. - Table shows “No data” state when an empty array is returned.
- Centralized error toast (
src (Application Core)
- src/main.tsx – Sets up MUI theme, React Router,
AuthProvider, andQueryClientProvider. - src/App.tsx – Defines routes:
<Routes> <Route path="/login" element={<LoginForm />} /> <Route element={<ProtectedRoute />}> <Route path="/admin/:resource" element={<ResourceList />} /> <Route path="/admin/:resource/edit/:id" element={<ResourceForm />} /> </Route> </Routes> - src/pages/ResourceList.tsx – Reads
resourcefrom URL, loads itsResourceConfig, callsuseResource, and rendersEnhancedTable+FilterBar. - src/pages/ResourceForm.tsx – Dynamically builds a form based on
ResourceFielddefinitions, usingdisplayFormatfor default values. - State Management – TanStack Query caches paginated results;
AuthProviderensures all API calls include a valid JWT. - Theming –
ThemeProvidertoggles light/dark mode via a context hook that persists the preference inlocalStorage.
These implementation notes detail the concrete file layout, data flow, and core logic that power the UI generated from OpenAPI specifications while maintaining authenticated access. They can be directly adapted for the lovable platform to provide a richer UI and better handling of auth and data rendering.