refactor: use react router loader to fetch data to eliminate the use of useEffect #283
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request includes major changes to the routing and data fetching in the client-side codebase, as well as a small change to the server-side settings controller. The changes mainly involve moving the data fetching logic from individual components to the router, and using the
useLoaderData
hook to access the fetched data in the components. This simplifies the components by removing unnecessary state and effect hooks related to data fetching, and centralizes the data fetching logic in the router.Changes to the router:
src/client/routes.jsx
: Added aloader
prop to eachRoute
component under the/account
path. Each loader is an asynchronous function that imports and calls the appropriate API function to fetch the necessary data for the route.Changes to components:
src/client/routes/account/account.jsx
: Removed thegetUser
call and the associated state and effect hook. Now usesuseLoaderData
to get the user data. [1] [2] [3]src/client/routes/account/index.jsx
: Similar changes asaccount.jsx
. [1] [2]src/client/routes/account/secrets.jsx
: Similar changes asaccount.jsx
, but for fetching secrets. [1] [2] [3] [4]src/client/routes/account/settings.jsx
: Similar changes asaccount.jsx
, but for fetching settings. [1] [2]src/client/routes/account/users.jsx
: Similar changes asaccount.jsx
, but for fetching users. Also moved the user row rendering logic to a separateUserRows
component. [1] [2] [3] [4] [5] [6]Change to server-side controller:
src/server/controllers/admin/settings.js
: Changed thefindMany
call tofindFirst
when fetching admin settings, as there should only be one set of admin settings.