Project

General

Profile

Refactor #30450

Use API hook and Foreman API middleware for all React API requests

Added by Jeremy Lenz over 1 year ago. Updated about 1 month ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Category:
API
Target version:
Branch:
Difficulty:
Triaged:
Yes
Bugzilla link:
Pull request:
Fixed in Releases:
Found in Releases:

Description

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.

History

#1 Updated by Chris Roberts over 1 year ago

  • Triaged changed from No to Yes
  • Target version set to Katello 3.18.0

#2 Updated by Jonathon Turel 10 months ago

  • Target version changed from Katello 3.18.0 to Katello 4.1.0

#3 Updated by Jeremy Lenz 10 months ago

  • Description updated (diff)

#4 Updated by Jeremy Lenz 10 months ago

  • Subject changed from Use Foreman API middleware for all React API requests to Use API hook and Foreman API middleware for all React API requests

#5 Updated by Jeremy Lenz 6 months ago

  • Target version changed from Katello 4.1.0 to Katello 4.2.0

#6 Updated by Justin Sherrill 2 months ago

  • Triaged changed from Yes to No
  • Target version deleted (Katello 4.2.0)

#7 Updated by Samir Jha 2 months ago

  • Triaged changed from No to Yes
  • Target version set to Katello Backlog

#8 Updated by Jeremy Lenz about 1 month ago

  • Target version changed from Katello Backlog to Katello Recycle Bin
  • Status changed from New to Rejected

We will use this pattern for the new pages and for any other refactoring we do. Not enough resources to go back and refactor everything.

Also available in: Atom PDF