Skip to content

Commit

Permalink
[TM-1531] join both connections
Browse files Browse the repository at this point in the history
  • Loading branch information
egrojMonroy committed Dec 23, 2024
1 parent 4d9a28e commit 3ec5111
Showing 1 changed file with 41 additions and 62 deletions.
103 changes: 41 additions & 62 deletions src/connections/DelayedJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,64 @@ import { useEffect } from "react";
import { createSelector } from "reselect";

import { bulkUpdateJobs, listDelayedJobs } from "@/generated/v3/jobService/jobServiceComponents";
import {
bulkUpdateJobsFetchFailed,
bulkUpdateJobsIsFetching,
listDelayedJobsFetchFailed
} from "@/generated/v3/jobService/jobServicePredicates";
import { bulkUpdateJobsFetchFailed, bulkUpdateJobsIsFetching } from "@/generated/v3/jobService/jobServicePredicates";
import { DelayedJobData, DelayedJobDto } from "@/generated/v3/jobService/jobServiceSchemas";
import { useConnection } from "@/hooks/useConnection";
import { ApiDataStore } from "@/store/apiSlice";
import { Connection } from "@/types/connection";
import { connectionLoader } from "@/utils/connectionShortcuts";

type DelayedJobsConnection = {
delayedJobs?: DelayedJobDto[];
type DelayedJobCombinedConnection = {
delayedJobs?: DelayedJobDto[] | undefined;
delayedJobsIsLoading: boolean;
delayedJobsHasFailed: boolean;
bulkUpdateJobsIsLoading: boolean;
bulkUpdateJobsHasFailed: boolean;
updatedJobsResponse?: DelayedJobDto[];
};

const delayedJobsSelector = (store: ApiDataStore) =>
Object.values(store.delayedJobs ?? {}).map(resource => resource.attributes);

const delayedJobsLoadFailedSelector = (store: ApiDataStore) => listDelayedJobsFetchFailed(store) != null;
const bulkUpdateJobsSelector = (store: ApiDataStore) => ({
bulkUpdateJobsIsLoading: bulkUpdateJobsIsFetching(store),
bulkUpdateJobsHasFailed: bulkUpdateJobsFetchFailed(store) != null,
updatedJobsResponse: Object.values(store.delayedJobs ?? {}).map(resource => resource.attributes as DelayedJobDto)
});

const delayedJobsIsLoaded = ({ delayedJobs, delayedJobsHasFailed }: DelayedJobsConnection) =>
delayedJobs != null || delayedJobsHasFailed;
const combinedSelector = createSelector(
[delayedJobsSelector, bulkUpdateJobsSelector],
(delayedJobs, { bulkUpdateJobsIsLoading, bulkUpdateJobsHasFailed, updatedJobsResponse }) => ({
delayedJobs,
delayedJobsIsLoading: delayedJobs == null && !bulkUpdateJobsHasFailed,
delayedJobsHasFailed: bulkUpdateJobsHasFailed,
bulkUpdateJobsIsLoading,
bulkUpdateJobsHasFailed,
updatedJobsResponse
})
);

const combinedLoad = (connection: DelayedJobCombinedConnection) => {
if (!combinedIsLoaded(connection)) {
listDelayedJobs();
}
};

const delayedJobsConnection: Connection<DelayedJobsConnection> = {
load: connection => {
if (!delayedJobsIsLoaded(connection)) {
listDelayedJobs();
}
},
isLoaded: delayedJobsIsLoaded,
selector: createSelector(
[delayedJobsSelector, delayedJobsLoadFailedSelector],
(delayedJobs, delayedJobsHasFailed) => ({
delayedJobs,
delayedJobsIsLoading: delayedJobs == null && !delayedJobsHasFailed,
delayedJobsHasFailed
})
)
const combinedIsLoaded = ({
delayedJobs,
delayedJobsHasFailed,
bulkUpdateJobsIsLoading,
bulkUpdateJobsHasFailed
}: DelayedJobCombinedConnection) =>
(delayedJobs != null || delayedJobsHasFailed) && !bulkUpdateJobsIsLoading && !bulkUpdateJobsHasFailed;

const delayedJobsCombinedConnection: Connection<DelayedJobCombinedConnection> = {
load: combinedLoad,
isLoaded: combinedIsLoaded,
selector: combinedSelector
};

export const useDelayedJobs = () => {
const connection = useConnection(delayedJobsConnection);
const connection = useConnection(delayedJobsCombinedConnection);

useEffect(() => {
const intervalId = setInterval(() => {
Expand All @@ -59,39 +73,4 @@ export const useDelayedJobs = () => {

return connection;
};

type BulkUpdateJobsConnection = {
bulkUpdateJobsIsLoading: boolean;
bulkUpdateJobsHasFailed: boolean;
updatedJobsResponse?: DelayedJobDto[];
};

const bulkUpdateJobsSelector = (store: ApiDataStore) => ({
bulkUpdateJobsIsLoading: bulkUpdateJobsIsFetching(store),
bulkUpdateJobsHasFailed: bulkUpdateJobsFetchFailed(store) != null,
updatedJobsResponse: Object.values(store.delayedJobs ?? {}).map(resource => resource.attributes as DelayedJobDto)
});

const bulkUpdateJobsIsLoaded = ({ bulkUpdateJobsIsLoading, bulkUpdateJobsHasFailed }: BulkUpdateJobsConnection) =>
!bulkUpdateJobsIsLoading && !bulkUpdateJobsHasFailed;

const bulkUpdateJobsConnection: Connection<BulkUpdateJobsConnection, { jobs: DelayedJobData[] }> = {
load: (connection, { jobs }) => {
if (!bulkUpdateJobsIsLoaded(connection)) {
bulkUpdateJobs({ body: { data: jobs } });
}
},
isLoaded: bulkUpdateJobsIsLoaded,
selector: createSelector(
bulkUpdateJobsSelector,
({ bulkUpdateJobsIsLoading, bulkUpdateJobsHasFailed, updatedJobsResponse }) => ({
bulkUpdateJobsIsLoading,
bulkUpdateJobsHasFailed,
updatedJobsResponse
})
)
};

export const useBulkUpdateJobs = connectionLoader(bulkUpdateJobsConnection);

export const triggerBulkUpdate = (jobs: DelayedJobData[]) => bulkUpdateJobs({ body: { data: jobs } });

0 comments on commit 3ec5111

Please sign in to comment.