Skip to content

Commit

Permalink
loop sorting content by mc status (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav-Eikaas authored Aug 7, 2024
1 parent b06dccd commit 7a181f3
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions libs/loopsidesheet/src/lib/utils-sidesheet/useGetLoopContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,39 @@ import { useContextId } from '@cc-components/shared';
import { useQuery } from '@tanstack/react-query';
import { LoopContent } from '../types';

type McStatus = "OS" | "PB" | "PA" | "OK";

const sortOrderMcStatusRanks: Record<McStatus, number> = {
"OS": 1,
"PB": 2,
"PA": 3,
"OK": 4
};

const compareMcStatus = (x: McStatus | undefined | null, y: McStatus | undefined | null): number => {
const xRank = x ? sortOrderMcStatusRanks[x] : -1;
const yRank = y ? sortOrderMcStatusRanks[y] : -1;
return xRank - yRank;
};

export const useGetLoopContent = (loopNo: string) => {
const client = useHttpClient('cc-api');
const contextId = useContextId();

const { data, isLoading, error } = useQuery<LoopContent[], Error>({
queryKey: ['loop', loopNo, 'content'],
queryFn: async ({ signal }) => {
const respons = await client.fetch(
const response = await client.fetch(
`/api/contexts/${contextId}/loop/${loopNo}/content`,
{ signal }
);
if (!respons.ok) {
if (!response.ok) {
throw new Error();
}
return respons.json();
const loopContent = await response.json() as LoopContent[]
return loopContent.sort((a, b) => {
return compareMcStatus(a.mechanicalCompletionStatus as McStatus | undefined, b.mechanicalCompletionStatus as McStatus | undefined)
})
},
});

Expand Down

0 comments on commit 7a181f3

Please sign in to comment.