2026-02-14 02:20:19 +00:00

124 lines
4.0 KiB
TypeScript

import { define } from "../../utils.ts";
import RentalsOwned from "../../islands/RentalsOwned.tsx";
import { page } from "fresh";
import {
createProperty,
getProperties,
} from "../../services/propertyService.ts";
import { SessionCookie } from "../auth/callback.ts";
import WelcomeBar from "../../islands/WelcomeBar/WelcomeBar.tsx";
import { createPublicId } from "../../helpers.ts";
import { Property } from "../../db/interfaces.ts";
export const handler = define.handlers({
GET: async (ctx) => {
const stateData: SessionCookie = ctx.state.data;
let properties: Property[] = [];
if (stateData?.user_id) {
properties = await getProperties(stateData.user_id);
}
const user_name = stateData?.user_name;
const isEncrypted = stateData?.user_encrypted;
const publicId = stateData.user_id
? await createPublicId(stateData.user_id)
: "";
return page({
stateData,
properties,
user_name,
isEncrypted,
publicId,
stateId: ctx.state.stateId,
});
},
POST: async (ctx) => {
const stateData: SessionCookie = ctx.state.data;
const user_name = stateData?.user_name;
const isEncrypted = stateData?.user_encrypted;
const publicId = stateData.user_id
? await createPublicId(stateData.user_id)
: "";
const form = await ctx.req.formData();
const properties = JSON.parse(form.get("properties")?.toString() || "[]");
const timestampNow = Date.now();
const property = {
id: crypto.randomUUID(),
owner_id: stateData.user_id,
title: form.get("title")?.toString() || null,
description: form.get("description")?.toString() || null,
address_1: form.get("address_1")?.toString() || null,
address_2: form.get("address_2")?.toString() || null,
city: form.get("city")?.toString() || null,
state: form.get("state")?.toString() || null,
zip: form.get("zip")?.toString() || null,
country: form.get("country")?.toString() || null,
bedrooms: form.get("bedrooms") ? Number(form.get("bedrooms")) : null,
bathrooms: form.get("bathrooms") ? Number(form.get("bathrooms")) : null,
square_footage: form.get("square_footage")
? Number(form.get("square_footage"))
: null,
lot_size: form.get("lot_size") ? Number(form.get("lot_size")) : null,
year_built: form.get("year_built")
? Number(form.get("year_built"))
: null,
price: form.get("price") ? Number(form.get("price")) : null,
property_type: form.get("property_type")?.toString() || null,
listing_status: form.get("listing_status")?.toString() || null,
image_url: form.get("image_url")?.toString() || null,
rental_status: "draft",
created_on: timestampNow,
updated_on: timestampNow,
};
function _emptyToNull(obj: Record<string, unknown>) {
for (const key in obj) {
if (obj[key] === "") obj[key] = null;
}
return obj;
}
//const cleanedProperty = emptyToNull(property);
const [newProperty] = await createProperty(property);
if (newProperty) {
properties.push(newProperty);
}
return page({
stateData,
properties,
user_name,
isEncrypted,
publicId,
stateId: ctx.state.stateId,
});
},
});
export default define.page(
(
{ data }: {
data: {
stateData: SessionCookie;
properties: Property[];
user_name: string;
isEncrypted: boolean;
publicId: string;
stateId: string;
};
},
) => (
<main class="min-h-screen bg-gradient-to-br from-slate-50 to-blue-100 flex flex-col items-center justify-start pt-0">
<WelcomeBar
publicId={data.publicId}
user_name={data.user_name}
isEncrypted={data.isEncrypted}
currentRoute="/rental-property"
stateId={data.stateId}
/>
<div class="w-full max-w-4xl px-8 sm:px-12 lg:px-24 mt-8">
<RentalsOwned properties={data.properties || []} />
</div>
</main>
),
);