-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): Display all selected location levels
- Loading branch information
1 parent
668cabd
commit 61a55ed
Showing
4 changed files
with
72 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useQueries } from "@tanstack/react-query"; | ||
|
||
import { getGetLocationsQueryKey, getLocations } from "@/types/generated/location"; | ||
import { GetLocationsParams, LocationListResponse } from "@/types/generated/strapi.schemas"; | ||
|
||
type LocationByCode = { | ||
id: number; | ||
name: string; | ||
code: string; | ||
}; | ||
|
||
export function useLocationByCodes(codes: string[]) { | ||
const { data, isLoading } = useQueries({ | ||
queries: codes.map((code) => { | ||
const queryParams: GetLocationsParams = { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore-error | ||
fields: ["name", "code"], | ||
filters: { | ||
code: { | ||
$eq: code, | ||
}, | ||
}, | ||
sort: "name", | ||
"pagination[limit]": 1, | ||
}; | ||
|
||
return { | ||
queryKey: getGetLocationsQueryKey(queryParams), | ||
queryFn: () => getLocations(queryParams), | ||
placeholderData: { data: undefined }, | ||
select: (data: LocationListResponse) => { | ||
if (!data?.data || data.data.length === 0) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
id: data.data[0].id, | ||
name: data.data[0].attributes!.name!, | ||
code: data.data[0].attributes!.code!, | ||
} as LocationByCode; | ||
}, | ||
}; | ||
}), | ||
combine: (results) => { | ||
const isLoading = results.some(({ isPending }) => isPending); | ||
|
||
return { | ||
data: isLoading ? undefined : results.map(({ data }) => data!), | ||
isLoading, | ||
}; | ||
}, | ||
}); | ||
|
||
return { data, isLoading }; | ||
} |