The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
import type { StaticCountry } from "@lib/data/countries"
import type { RegionCountryDTO } from "@medusajs/types"
const acceptedOrderKeys = ["name", "code"]
/**
* Since countries cannot be retrieved from the API, we need to create a hook
* that can be used to filter and sort the static list of countries.
*/
export const useCountries = ({
countries,
q,
order = "name",
limit,
offset = 0,
}: {
countries: StaticCountry[]
limit: number
offset?: number
order?: string
q?: string
}) => {
const data = countries.slice(offset, offset + limit)
if (order) {
const direction = order.startsWith("-") ? -1 : 1
const key = order.replace("-", "")
if (!acceptedOrderKeys.includes(key)) {
console.log("The key ${key} is not a valid order key")
throw new Response(
JSON.stringify({ message: `The key ${key} is not a valid order key` }),
{ status: 500, headers: { "Content-Type": "application/json" } }
)
}
const sortKey: keyof RegionCountryDTO = key === "code" ? "iso_2" : "name"
data.sort((a, b) => {
if (a[sortKey] === null && b[sortKey] === null) {
return 0
}
if (a[sortKey] === null) {
return direction
}
if (b[sortKey] === null) {
return -direction
}
return a[sortKey]! > b[sortKey]! ? direction : -direction
})
}
if (q) {
const query = q.toLowerCase()
const results = countries.filter(
(c) =>
c.name.toLowerCase().includes(query) ||
c.iso_2.toLowerCase().includes(query)
)
return {
countries: results,
count: results.length,
}
}
return {
countries: data,
count: countries.length,
}
}