Fixed announcement typing

Expired -> obsolete filter front
Removed useless despose utils
This commit is contained in:
Dmitriy Shishkov 2023-08-08 18:18:57 +03:00
parent 6c5c7aa0c2
commit 4135c29160
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
4 changed files with 22 additions and 39 deletions

View File

@ -1,20 +1,28 @@
import { isObject } from '../../utils/types'
import { Category, isCategory } from '../../assets/category'
type AnnouncementResponse = {
type Announcement = {
id: number,
user_id: number,
userId: number,
name: string,
category: Category,
best_by: string,
bestBy: string,
address: string,
longtitude: number,
latitude: number,
description: string,
lng: number,
lat: number,
description: string | null,
src: string | null,
metro: string,
trashId: number | null,
booked_by: number,
bookedBy: number,
}
type AnnouncementResponse = Omit<Announcement, 'userId' | 'bestBy' | 'bookedBy' | 'lat' | 'lng'> & {
user_id: Announcement['userId'],
best_by: Announcement['bestBy'],
longtitude: Announcement['lng'],
latitude: Announcement['lat'],
booked_by: Announcement['bookedBy'],
}
const isAnnouncementResponse = (obj: unknown): obj is AnnouncementResponse => (
@ -35,22 +43,6 @@ const isAnnouncementResponse = (obj: unknown): obj is AnnouncementResponse => (
})
)
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,

View File

@ -16,18 +16,18 @@ const userCategoriesInfos: Record<UserCategory, (ann: Announcement) => string> =
`Годен до ${new Date(ann.bestBy).toLocaleDateString('ru')}`
),
needDispose: (ann: Announcement) => (
`Были заинтересованы: ${ann.bookedBy} чел.`
`Было заинтересно ${ann.bookedBy} чел.`
),
}
const composeUserCategoriesFilters: Record<UserCategory, () => FiltersType> = {
givingOut: () => ({
userId: getId(),
expired: false,
obsolete: false,
}),
needDispose: () => ({
userId: getId(),
expired: true,
obsolete: true,
}),
}

View File

@ -1,9 +0,0 @@
import { Announcement } from '../api/announcement/types'
const DAY_MS = 24 * 60 * 60 * 1000
const isAnnExpired = (ann: Announcement) => (
(ann.bestBy - Date.now()) < DAY_MS
)
export { isAnnExpired }

View File

@ -2,17 +2,17 @@ import { Announcement } from '../api/announcement/types'
import { isCategory } from '../assets/category'
import { fallbackToUndefined, isBoolean, isInt } from './types'
const filterNames = ['userId', 'category', 'metro', 'expired'] as const
const filterNames = ['userId', 'category', 'metro', 'obsolete'] as const
type FilterNames = typeof filterNames[number]
type FiltersType = Partial<
Pick<Announcement, FilterNames & keyof Announcement> &
{
expired: boolean,
obsolete: boolean,
}
>
const defaultFilters: FiltersType = { userId: undefined, category: undefined, metro: undefined, expired: false }
const defaultFilters: FiltersType = { userId: undefined, category: undefined, metro: undefined, obsolete: false }
const URLEncodeFilters = (filters: FiltersType) => (
Object.fromEntries(
@ -39,7 +39,7 @@ const URLDecoreFilters = (params: URLSearchParams): FiltersType => {
userId: fallbackToUndefined(Number.parseInt(strFilters['userId']), isInt),
category: fallbackToUndefined(strFilters['category'], isCategory),
metro: strFilters['metro'],
expired: fallbackToUndefined(strFilters['expired'] === 'true', isBoolean),
obsolete: fallbackToUndefined(strFilters['obsolete'] === 'true', isBoolean),
}
}