Skip to content
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

HUM-99: available jobs service refactor #3032

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { useJobsFilterStore } from '@/modules/worker/hooks/use-jobs-filter-store
import { Alert } from '@/shared/components/ui/alert';
import { getNetworkName } from '@/modules/smart-contracts/get-network-name';
import { getErrorMessageForError } from '@/shared/errors';
import type { AvailableJob } from '@/modules/worker/services/available-jobs-data';
import { useInfiniteGetAvailableJobsData } from '@/modules/worker/services/available-jobs-data';
import type { AvailableJob } from '@/modules/worker/services/fetch-available-jobs';
import { useInfiniteAvailableJobsQuery } from '@/modules/worker/hooks/use-infinite-available-jobs-query';
import { Loader } from '@/shared/components/ui/loader';
import { EvmAddress } from '@/modules/worker/components/jobs/evm-address';
import { Chip } from '@/shared/components/ui/chip';
Expand Down Expand Up @@ -37,7 +37,7 @@ export function AvailableJobsTableMobile({
error: tableError,
fetchNextPage,
hasNextPage,
} = useInfiniteGetAvailableJobsData();
} = useInfiniteAvailableJobsQuery();
const { filterParams, setPageParams, resetFilterParams } =
useJobsFilterStore();
const { t } = useTranslation();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable camelcase -- api response*/
import { useQuery } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import { useJobsFilterStore } from '@/modules/worker/hooks/use-jobs-filter-store';
import { fetchAvailableJobs } from '@/modules/worker/services/fetch-available-jobs';
import type { AvailableJobsSuccessResponse } from '@/modules/worker/services/fetch-available-jobs';

export function useAvailableJobsQuery() {
const { filterParams } = useJobsFilterStore();
const { address: oracleAddress } = useParams<{ address: string }>();

const queryParams = {
...filterParams,
oracle_address: oracleAddress ?? '',
};

return useQuery<AvailableJobsSuccessResponse>({
queryKey: ['availableJobs', queryParams],
queryFn: ({ signal }) => fetchAvailableJobs(queryParams, signal),
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable camelcase -- api response*/
import { useInfiniteQuery } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import { useJobsFilterStore } from '@/modules/worker/hooks/use-jobs-filter-store';
import { fetchAvailableJobs } from '@/modules/worker/services/fetch-available-jobs';
import type { AvailableJobsSuccessResponse } from '@/modules/worker/services/fetch-available-jobs';

export function useInfiniteAvailableJobsQuery() {
const { filterParams } = useJobsFilterStore();
const { address: oracleAddress } = useParams<{ address: string }>();

const queryParams = {
...filterParams,
oracle_address: oracleAddress ?? '',
};

return useInfiniteQuery<AvailableJobsSuccessResponse>({
queryKey: ['availableJobsInfinite', queryParams],
queryFn: ({ signal }) => fetchAvailableJobs(queryParams, signal),
initialPageParam: 0,
getNextPageParam: (lastPage) =>
lastPage.total_pages - 1 <= lastPage.page ? undefined : lastPage.page,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable camelcase -- api response*/
import { z } from 'zod';
import { createPaginationSchema } from '@/shared/helpers/pagination';

export const availableJobSchema = z.object({
escrow_address: z.string(),
chain_id: z.number(),
job_type: z.string(),
status: z.string(),
job_description: z.string().optional(),
reward_amount: z.string().optional(),
reward_token: z.string().optional(),
});

export const availableJobsSuccessResponseSchema =
createPaginationSchema(availableJobSchema);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { apiClient } from '@/api/api-client';
import { apiPaths } from '@/api/api-paths';
import { stringifyUrlQueryObject } from '@/shared/helpers/transfomers';
import type {
AvailableJobsSuccessResponse,
JobTableQueryParams,
} from '@/modules/worker/services/fetch-available-jobs/fetch-available-jobs.types';
import { availableJobsSuccessResponseSchema } from '@/modules/worker/services/fetch-available-jobs/fetch-available-jobs.schema';

export async function fetchAvailableJobs(
params: JobTableQueryParams,
abortSignal: AbortSignal
): Promise<AvailableJobsSuccessResponse> {
const endpoint = `${apiPaths.worker.jobs.path}?${stringifyUrlQueryObject(params)}`;

return apiClient(
endpoint,
{
authenticated: true,
successSchema: availableJobsSuccessResponseSchema,
options: { method: 'GET' },
},
abortSignal
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type z } from 'zod';
import type { JobsFilterStoreProps } from '@/modules/worker/hooks/use-jobs-filter-store';
import {
type availableJobSchema,
type availableJobsSuccessResponseSchema,
} from '@/modules/worker/services/fetch-available-jobs/fetch-available-jobs.schema';

export type AvailableJob = z.infer<typeof availableJobSchema>;
export type AvailableJobsSuccessResponse = z.infer<
typeof availableJobsSuccessResponseSchema
>;

export type JobTableQueryParams = JobsFilterStoreProps['filterParams'] & {
oracle_address: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './fetch-available-jobs.types';
export * from './fetch-available-jobs.schema';
export * from './fetch-available-jobs';
Loading