You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
TypeScript
61 lines
1.3 KiB
TypeScript
import { Component } from "solid-js";
|
|
import z, { Infer } from "myzod";
|
|
|
|
export const addressSchema = z.object({
|
|
type: z.literals("S", "K").optional(),
|
|
name: z.string(),
|
|
line1: z.string().optional(),
|
|
line2: z.string().optional(),
|
|
city: z.string().optional(),
|
|
zip: z.number().optional(),
|
|
country: z.string(),
|
|
});
|
|
|
|
export type AddressData = Infer<typeof addressSchema>;
|
|
|
|
export const createAddress = (data: Partial<AddressData> = {}): AddressData => {
|
|
return {
|
|
name: "",
|
|
line1: "",
|
|
line2: "",
|
|
city: "",
|
|
zip: undefined,
|
|
country: "CH",
|
|
...data,
|
|
};
|
|
};
|
|
|
|
export const isStructuredAddress = (address: AddressData) => {
|
|
if (address.type) {
|
|
return address.type === "S";
|
|
}
|
|
|
|
return address.city || address.zip != null;
|
|
};
|
|
|
|
export const getLine1 = (address: AddressData) => {
|
|
if (isStructuredAddress(address)) {
|
|
return `${address.line1 || ""} ${address.line2 || ""}`;
|
|
}
|
|
|
|
return address.line1;
|
|
};
|
|
|
|
export const getLine2 = (address: AddressData) => {
|
|
if (isStructuredAddress(address)) {
|
|
return `${address.zip || ""} ${address.city || ""}`;
|
|
}
|
|
|
|
return address.line2;
|
|
};
|
|
|
|
const Address: Component<{ address: AddressData }> = (props) => (
|
|
<>
|
|
<div>{props.address.name}</div>
|
|
<div>{getLine1(props.address)}</div>
|
|
<div>{getLine2(props.address)}</div>
|
|
</>
|
|
);
|
|
|
|
export default Address;
|