47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { winofyFetch, type PaginatedResponse } from "./winofy";
|
|
import { callApi, type ActionResult } from "./result";
|
|
import type { Address, AddressLabel } from "@/types/api";
|
|
|
|
export async function getAddresses(): Promise<Address[]> {
|
|
const res = await winofyFetch<PaginatedResponse<Address>>("/v1/addresses/");
|
|
return res.results;
|
|
}
|
|
|
|
export interface AddressInput {
|
|
label: AddressLabel;
|
|
title: string;
|
|
full_address: string;
|
|
plaque: string;
|
|
floor: string;
|
|
unit: string;
|
|
recipient_name: string;
|
|
recipient_phone: string;
|
|
city_uuid: string;
|
|
neighborhood_uuid?: string;
|
|
latitude: number;
|
|
longitude: number;
|
|
is_default: boolean;
|
|
}
|
|
|
|
export async function createAddress(input: AddressInput): Promise<ActionResult<Address>> {
|
|
const result = await callApi(() => winofyFetch<Address>("/v1/addresses/", { method: "POST", body: input }));
|
|
if (result.ok) revalidatePath("/addresses");
|
|
return result;
|
|
}
|
|
|
|
export async function updateAddress(uuid: string, input: Partial<AddressInput>): Promise<ActionResult<Address>> {
|
|
const result = await callApi(() =>
|
|
winofyFetch<Address>(`/v1/addresses/${uuid}/`, { method: "PATCH", body: input }),
|
|
);
|
|
if (result.ok) revalidatePath("/addresses");
|
|
return result;
|
|
}
|
|
|
|
export async function deleteAddress(uuid: string): Promise<ActionResult> {
|
|
const result = await callApi(() => winofyFetch<undefined>(`/v1/addresses/${uuid}/`, { method: "DELETE" }));
|
|
if (result.ok) revalidatePath("/addresses");
|
|
return result;
|
|
}
|