-
Notifications
You must be signed in to change notification settings - Fork 1
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
[TM-1531] add job connection #745
base: feat/TM-1531-add-job-service
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { createSelector } from "reselect"; | ||
|
||
import { DelayedJobsFindVariables, listDelayedJobs } from "@/generated/v3/jobService/jobServiceComponents"; | ||
import { delayedJobsFindFetchFailed } from "@/generated/v3/jobService/jobServicePredicates"; | ||
import { DelayedJobDto } from "@/generated/v3/jobService/jobServiceSchemas"; | ||
import { ApiDataStore } from "@/store/apiSlice"; | ||
import { Connection } from "@/types/connection"; | ||
import { connectionHook } from "@/utils/connectionShortcuts"; | ||
import { selectorCache } from "@/utils/selectorCache"; | ||
|
||
type DelayedJobsConnection = { | ||
delayedJobs?: DelayedJobDto[]; | ||
isLoading: boolean; | ||
hasLoadFailed: boolean; | ||
}; | ||
|
||
type DelayedJobsProps = { uuid: string }; | ||
|
||
const delayedJobsSelector = (store: ApiDataStore) => { | ||
const delayedJobsMap = store.delayedJobs || {}; | ||
return Object.values(delayedJobsMap).map(resource => resource.attributes); | ||
}; | ||
|
||
const delayedJobsLoadFailedSelector = (store: ApiDataStore, { uuid }: DelayedJobsProps) => { | ||
const variables: DelayedJobsFindVariables = { | ||
pathParams: { uuid } | ||
}; | ||
|
||
return delayedJobsFindFetchFailed(variables)(store) != null; | ||
}; | ||
const connectionIsLoaded = ({ delayedJobs, hasLoadFailed, isLoading }: DelayedJobsConnection) => { | ||
return (delayedJobs != null && delayedJobs.length > 0) || hasLoadFailed || isLoading; | ||
}; | ||
|
||
const delayedJobsConnection: Connection<DelayedJobsConnection, DelayedJobsProps> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These props aren't used, no reason to include them. |
||
load: (connection, props) => { | ||
const isLoaded = connectionIsLoaded(connection); | ||
if (!isLoaded) { | ||
listDelayedJobs(); | ||
} | ||
}, | ||
isLoaded: connectionIsLoaded, | ||
selector: selectorCache( | ||
props => props.uuid, | ||
props => | ||
createSelector( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there are no props, the selector cache isn't needed - it's only needed when there are props to differentiate different selectors. The |
||
[delayedJobsSelector, store => delayedJobsLoadFailedSelector(store, props)], | ||
(delayedJobs, hasLoadFailed): DelayedJobsConnection => ({ | ||
delayedJobs, | ||
isLoading: delayedJobs == null && !hasLoadFailed, | ||
hasLoadFailed | ||
}) | ||
) | ||
) | ||
}; | ||
|
||
export const useDelayedJobs = connectionHook(delayedJobsConnection); |
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.
Since the API request doesn't use these path params, the load failed selector shouldn't either. The params to those two must always match for this feature to function.