forked from polka_billy/porridger
0
front/src/api/announcement/index.ts
Normal file
0
front/src/api/announcement/index.ts
Normal file
59
front/src/api/announcement/types.ts
Normal file
59
front/src/api/announcement/types.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { isObject } from '../../utils/types'
|
||||
import { Category, isCategory } from '../../assets/category'
|
||||
|
||||
type AnnouncementResponse = {
|
||||
id: number,
|
||||
user_id: number,
|
||||
name: string,
|
||||
category: Category,
|
||||
best_by: number,
|
||||
address: string,
|
||||
longtitude: number,
|
||||
latitude: number,
|
||||
description: string,
|
||||
src: string | null,
|
||||
metro: string,
|
||||
trashId: number | null,
|
||||
booked_by: number
|
||||
}
|
||||
|
||||
const isAnnouncementResponse = (obj: unknown): obj is AnnouncementResponse => isObject(obj, {
|
||||
'id': 'number',
|
||||
'user_id': 'number',
|
||||
'name': 'string',
|
||||
'category': isCategory,
|
||||
'best_by': 'number',
|
||||
'address': 'string',
|
||||
'longtitude': 'number',
|
||||
'latitude': 'number',
|
||||
'description': 'string',
|
||||
'src': 'string?',
|
||||
'metro': 'string',
|
||||
'trashId': 'number?',
|
||||
'booked_by': 'number'
|
||||
})
|
||||
|
||||
type Announcement = {
|
||||
id: number,
|
||||
userId: number,
|
||||
name: string,
|
||||
category: Category,
|
||||
bestBy: number,
|
||||
address: string,
|
||||
lng: number,
|
||||
lat: number,
|
||||
description: string | null,
|
||||
src: string | null,
|
||||
metro: string,
|
||||
trashId: number | null,
|
||||
bookedBy: number
|
||||
}
|
||||
|
||||
export type {
|
||||
Announcement,
|
||||
AnnouncementResponse,
|
||||
}
|
||||
|
||||
export {
|
||||
isAnnouncementResponse,
|
||||
}
|
24
front/src/api/announcements/index.ts
Normal file
24
front/src/api/announcements/index.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { API_URL } from '../../config'
|
||||
import { FiltersType, URLEncodeFilters } from '../../utils/filters'
|
||||
import { Announcement } from '../announcement/types'
|
||||
import { AnnouncementsResponse } from './types'
|
||||
|
||||
const initialAnnouncements: Announcement[] = []
|
||||
|
||||
const composeAnnouncementsURL = (filters: FiltersType) =>
|
||||
API_URL + '/announcements?' + new URLSearchParams(URLEncodeFilters(filters)).toString()
|
||||
|
||||
const processAnnouncements = (data: AnnouncementsResponse): Announcement[] => {
|
||||
const annList = data.list_of_announcements
|
||||
|
||||
return annList.map(ann => ({
|
||||
...ann,
|
||||
lat: ann.latitude,
|
||||
lng: ann.longtitude,
|
||||
bestBy: ann.best_by,
|
||||
bookedBy: ann.booked_by,
|
||||
userId: ann.user_id
|
||||
}))
|
||||
}
|
||||
|
||||
export { initialAnnouncements, composeAnnouncementsURL, processAnnouncements }
|
20
front/src/api/announcements/types.ts
Normal file
20
front/src/api/announcements/types.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { isArrayOf, isObject } from '../../utils/types'
|
||||
import { AnnouncementResponse, isAnnouncementResponse } from '../announcement/types'
|
||||
|
||||
type AnnouncementsResponse = {
|
||||
list_of_announcements: AnnouncementResponse[],
|
||||
Success: boolean
|
||||
}
|
||||
|
||||
const isAnnouncementsResponse = (obj: unknown): obj is AnnouncementsResponse => isObject(obj, {
|
||||
'list_of_announcements': obj => isArrayOf<AnnouncementResponse>(obj, isAnnouncementResponse),
|
||||
'Success': 'boolean'
|
||||
})
|
||||
|
||||
export type {
|
||||
AnnouncementsResponse,
|
||||
}
|
||||
|
||||
export {
|
||||
isAnnouncementsResponse,
|
||||
}
|
11
front/src/api/trashbox/index.ts
Normal file
11
front/src/api/trashbox/index.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { LatLng } from 'leaflet'
|
||||
|
||||
import { API_URL } from '../../config'
|
||||
|
||||
const composeTrashboxURL = (position: LatLng) =>
|
||||
API_URL + '/trashbox?' + new URLSearchParams({
|
||||
lat: position.lat.toString(),
|
||||
lng: position.lng.toString()
|
||||
}).toString()
|
||||
|
||||
export { composeTrashboxURL }
|
22
front/src/api/trashbox/types.ts
Normal file
22
front/src/api/trashbox/types.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { isArrayOf, isObject, isString } from '../../utils/types'
|
||||
|
||||
type Trashbox = {
|
||||
Lat: number,
|
||||
Lng: number,
|
||||
Address: string,
|
||||
Categories: string[]
|
||||
}
|
||||
|
||||
const isTrashbox = (obj: unknown): obj is Trashbox => isObject(obj, {
|
||||
'Lat': 'number',
|
||||
'Lng': 'number',
|
||||
'Address': 'string',
|
||||
'Categories': obj => isArrayOf<string>(obj, isString)
|
||||
})
|
||||
|
||||
type TrashboxResponse = Trashbox[]
|
||||
|
||||
const isTrashboxResponse = (obj: unknown): obj is Trashbox[] => isArrayOf(obj, isTrashbox)
|
||||
|
||||
export type { Trashbox, TrashboxResponse }
|
||||
export { isTrashbox, isTrashboxResponse }
|
Reference in New Issue
Block a user