-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor other entity API functions and hooks to use new pattern #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an important file to check over since it's now used by all entities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repetitious but still important to check over the use of the new entity-api hook/Api pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repetitious but still important to check over the use of the new entity-api hook/Api pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repetitious but still important to check over the use of the new entity-api hook/Api pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repetitious but still important to check over the use of the new entity-api hook/Api pattern.
| organization | ||
| ), | ||
|
|
||
| createNested: async (id: Id, entity: Organization): Promise<Organization> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on having the same API for all entities, but some of them currently don't (or never will make sense to) implement certain functions? This is about having a standard and predictable set of entity verbs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - because it's a means to achieve standardization this seems fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a comment above around making it optional but I agree with this as well. Either works for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repetitious but still important to check over the use of the new entity-api hook/Api pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repetitious but still important to check over the use of the new entity-api hook/Api pattern.
| // Type-safe transformation function with runtime validation that ensures | ||
| // that raw ISO date time stamps are transformed into valid ts-luxon DateTime | ||
| // instances. | ||
| export const transformEntityDates = (data: any): any => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This transformation function is new and is used by some of the entity *List hooks/API functions. Since this is new, definitely worth checking over to see if it's easy to use and implemented well.
| const sessionIds = coachingSessions?.map((session) => session.id) || []; | ||
| const goalsPromises = sessionIds.map((id) => | ||
| fetchOverarchingGoalsByCoachingSessionId(id) | ||
| OverarchingGoalApi.list(id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This component and others should ideally be updated to use hooks instead of the direct function calls, but I decided it was out of the scope of this PR to refactor to this level since it would be a pretty major component code restructuring change.
The main reason being, this will give us automatic updates of list of entities when they're changed locally by the current user without having to refresh the page. For example, in the CoachingSessionList component.
…pattern. Also add a new entity-api method, createNested which creates a new entity nested under the ID of another entity.
…ormation function parameter to useEntityList() and listFn().
…mAgreement function
…ransformAgreement() function into general.ts as a generic, reusable function called transformEntityDates()
…d use it everywhere in the entity's Api
… type since it's no longer used
fabdb1f to
c74c9c7
Compare
…-auth-error-handling
…ndling Set error state with message rather than with error object
calebbourg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhodapp this is super cool and cleans things up a lot! Thank you for putting it together <3
I left a few thoughts and questions.
The main thing I'd just like to talk through is making config argument required vs returning an empty Array
| create: async (agreement: Agreement): Promise<Agreement> => | ||
| EntityApi.createFn<Agreement, Agreement>(AGREEMENTS_BASEURL, agreement), | ||
|
|
||
| createNested: async (_id: Id, _entity: Agreement): Promise<Agreement> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious what these might be used for.
src/lib/api/entity-api.ts
Outdated
| transform?: (data: T) => U | ||
| ): Promise<U> => { | ||
| if (!config) { | ||
| return [] as unknown as U; // Return empty array if config is null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to make the config argument required? I'm worried that this might start returning [] because of this and we think it's an issue on the BE and spend an unneeded amount of time troubleshooting before finding this line of code.
If we require config would the type system protect us here? I'm not 100% on how typescript works in that situation. If it's at compilation we should be protected I would think.
An alternative would be to log something to the console here so we would at least be able to get a hint at what happened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to Caleb's idea. Definitely feels more sound to use the type system to enforce/require a config arg than an early return in the function body.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a good catch, it was an easy change to make.
| }); | ||
|
|
||
| const rawData = response.data.data; | ||
| return transform ? transform(rawData) : (rawData as unknown as U); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this option a lot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too, it works super well. There if one needs it, otherwise ignore.
| baseUrl: string, | ||
| api: { | ||
| create: (entity: T) => Promise<T>; | ||
| createNested: (id: Id, entity: T) => Promise<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One option that I think might exist, though I don't know how to do this in typescript, would be to extract the api object into it's own type or interface and then have createNested be optional.
I don't have a strong opinion on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way useEntityMutation is set up, there's no easy way of not having the same functions across all entity types unfortunately.
| organization | ||
| ), | ||
|
|
||
| createNested: async (id: Id, entity: Organization): Promise<Organization> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a comment above around making it optional but I agree with this as well. Either works for me.
…erSession from CoachingNotes' return value
calebbourg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
Description
This PR refactors all of the other entity API functions and hooks to use new pattern established with Organization.
Note: this is a big PR but I'm going to leave some comments showing where to really hone your review with much of the rest of the changes just being quite repetitive and uninteresting.
Note 2: this PR requires this small backend PR to function correctly for logout.
GitHub Issue: None
Changes
useEntityList()andlistFn()Screenshots / Videos Showing UI Changes (if applicable)
Testing Strategy
Concerns
None