diff --git a/sublet/interfaces/Offer.ts b/sublet/interfaces/Offer.ts new file mode 100644 index 00000000..e82730ba --- /dev/null +++ b/sublet/interfaces/Offer.ts @@ -0,0 +1,11 @@ +// interfaces/Offers.ts + +export interface OfferInterface { + id?: number; + phone_number: string; + email?: string; + message?: string; + created_date?: Date; + user?: string; + sublet: number; +} diff --git a/sublet/interfaces/Property.ts b/sublet/interfaces/Property.ts index a097163f..c45f3ee4 100644 --- a/sublet/interfaces/Property.ts +++ b/sublet/interfaces/Property.ts @@ -19,6 +19,6 @@ export interface PropertyInterface { negotiable: boolean; start_date: string; end_date: string; - expires_at: string; + expires_at?: string; images: ImageInterface[]; } diff --git a/sublet/services/propertyService.ts b/sublet/services/propertyService.ts index 385eb6e6..f0529381 100644 --- a/sublet/services/propertyService.ts +++ b/sublet/services/propertyService.ts @@ -1,16 +1,78 @@ import { PropertyInterface } from '../interfaces/Property'; -import apiRequest from './fetch'; +import { OfferInterface } from '@/interfaces/Offer'; +import apiRequest from '../utils/fetch'; +//listSublets export const fetchProperties = async (): Promise => { const properties: PropertyInterface[] = await apiRequest("properties", "GET"); return properties; }; +//createSublet export const createProperty = async (property: any): Promise => { const newProperty: PropertyInterface = await apiRequest("properties", "POST", property); return newProperty }; +//retrieveSubletSerializerRead +export const fetchProperty = async (id: string): Promise => { + const property: PropertyInterface = await apiRequest(`properties/${id}`, "GET"); + return property +}; + +//updateSublet +export const updateProperty = async (id: string, property: any): Promise => { + const updatedProperty: PropertyInterface = await apiRequest(`properties/${id}`, "PUT", property); + return updatedProperty +}; + +//partialUpdateSublet +export const partialUpdateProperty = async (id: string, property: any): Promise => { + const updatedProperty: PropertyInterface = await apiRequest(`properties/${id}`, "PATCH", property); + return updatedProperty +}; + +//destroySublet +export const deleteProperty = async (id: string): Promise => { + await apiRequest(`properties/${id}`, "DELETE"); +}; + +//listAmenitys +export const fetchAmenities = async (): Promise => { + const amenities: string[] = await apiRequest("amenities", "GET"); + return amenities; +}; + +//listSubletSerializerSimples +export const fetchFavorites = async (): Promise => { + const properties: PropertyInterface[] = await apiRequest("favorites", "GET"); + return properties; +}; + +//listOffers +export const fetchOffers = async (): Promise => { + const properties: OfferInterface[] = await apiRequest("offers", "GET"); + return properties; +}; + +//listOffers (property) +export const fetchPropertyOffers = async (sublet_id: string): Promise => { + const properties: OfferInterface[] = await apiRequest(`properties/${sublet_id}/offers`, "GET"); + return properties; +}; + +//createOffer +export const createOffer = async (sublet_id: string, offer: OfferInterface): Promise => { + const newOffer: OfferInterface = await apiRequest(`properties/${sublet_id}/offers`, "POST", offer); + return newOffer +}; + +//destroyOffer +export const deleteOffer = async (sublet_id: string): Promise => { + await apiRequest(`properties/${sublet_id}/offers`, "DELETE"); +}; + +//createSubletImage export const createPropertyImage = async (sublet_id: string, image: File): Promise => { // Create FormData object to hold the image data const formData = new FormData(); @@ -18,9 +80,4 @@ export const createPropertyImage = async (sublet_id: string, image: File): Promi formData.append('images', image); // 'image' should be the File object return await apiRequest(`properties/${sublet_id}/images`, 'POST', formData) -}; - -export const fetchAmenities = async (): Promise => { - const amenities: string[] = await apiRequest("amenities", "GET"); - return amenities; }; \ No newline at end of file diff --git a/sublet/services/fetch.ts b/sublet/utils/fetch.ts similarity index 98% rename from sublet/services/fetch.ts rename to sublet/utils/fetch.ts index 48fc82e8..aeea29f6 100644 --- a/sublet/services/fetch.ts +++ b/sublet/utils/fetch.ts @@ -1,4 +1,4 @@ -import getCsrfTokenCookie from '../utils/csrf'; +import getCsrfTokenCookie from './csrf'; /** * Generic API request function. diff --git a/sublet/utils/fetch.tsx b/sublet/utils/fetch.tsx deleted file mode 100644 index b5d166e8..00000000 --- a/sublet/utils/fetch.tsx +++ /dev/null @@ -1,45 +0,0 @@ -import getCsrf from '@/utils/csrf' - -export const SITE_ORIGIN = - process.env.NODE_ENV === 'production' - ? `https://${process.env.DOMAIN || 'portal.pennmobile.org'}` - : `http://localhost:${process.env.PORT || 3000}` - -export const API_BASE_URL = `${SITE_ORIGIN}` - -export function getApiUrl(path: string): string { - if (/^https?:\/\//.test(path)) { - const url = new URL(path) - return url.pathname + url.search - } - return API_BASE_URL + path -} - -/** - * perform a fetch request to the API - * @param path e.g. /api/portal/polls/ - * @param data - * @returns Promise - */ -export function doApiRequest(path: string, data?: any): Promise { - let formattedData = data - if (!formattedData) { - formattedData = {} - } - formattedData.credentials = 'include' - formattedData.mode = 'same-origin' - if (typeof document !== 'undefined') { - formattedData.headers = formattedData.headers || {} - if (!(formattedData.body instanceof FormData)) { - formattedData.headers.Accept = - formattedData.headers.Accept || 'application/json' - formattedData.headers['Content-Type'] = - formattedData.headers['Content-Type'] || 'application/json' - } - formattedData.headers['X-CSRFToken'] = getCsrf() - } - if (formattedData.body && !(formattedData.body instanceof FormData)) { - formattedData.body = JSON.stringify(formattedData.body) - } - return fetch(getApiUrl(path), formattedData) -}