Use API hook and Foreman API middleware for all React API requests
Now that Foreman React API middleware supports all HTTP methods and not just GET (https://github.com/theforeman/foreman/pull/7261), we should start using it for new requests. In addition, we should consider using it for existing requests.
Foreman has also added a useAPI React Hook, which will simplify things even more.In Katello, I found
- 7 PUT requests
- 0 PATCH
- 15 DELETE (but not sure how many are relevant)
- 4 POST
- 41 GET
Currently, we use this redux-thunk pattern to handle most API requests:
1. In a try block, dispatch & await REQUEST action
2. dispatch SUCCESS action
3. dispatch FAILURE action in a catch block
With the API middleware, we would only have to:
1. dispatch API_OPERATIONS.[method] action.
Success and failure actions are automatically dispatched, and the API response is stored in Redux in a standard way. If we need any side effects to happen, handleSuccess and handleFailure callbacks are supplied.
Another benefit is that Redux action creators will no longer have to be thunks (i.e., can return simple objects instead of returning a function.) This makes Redux easier to understand and makes our code more readable.
A third benefit is that it would cut our code base by several dozen lines, since we won't have to repeat the REQUEST -> SUCCESS / FAILURE thunks everywhere.
With the useAPI hook, we can simplify things even further. For components that use hooks, we can just use that instead of creating actions/reducers/etc.