import { redirect } from "react-router-dom"; import { ApiClient } from "./api_client"; export async function loader({ request }) { const client = new ApiClient(); const url = new URL(request.url) const response = await client.get(`${url.pathname}${url.search}`); return response; } export async function addressLoader({ params }) { const client = new ApiClient(); const path = `/account/addresses/${params.addressId}`; const request = await client.authenticatedGet(path); if(request.response) return redirect("/account"); return request; } export async function accountLoader() { const client = new ApiClient(); const account_response = await client.authenticatedGet("/account"); const addresses_response = await client.authenticatedGet("/account/addresses"); const cards_response = await client.authenticatedGet("/account/cards"); // Authentication error handling. if(account_response.response || addresses_response.response || cards_response.response) return redirect("/products"); return [account_response, addresses_response, cards_response]; } export async function productLoader({ params }) { const client = new ApiClient(); const response = await client.getProduct(params.productId); return [response[0], response[1]]; }