Added expiration filter to front

This commit is contained in:
Dmitriy Shishkov 2023-07-31 17:23:24 +03:00
parent 6724a97352
commit 24bd39f689
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
3 changed files with 29 additions and 14 deletions

View File

@ -2,26 +2,33 @@ import { Announcement } from '../api/announcement/types'
import { getId } from '../utils/auth' import { getId } from '../utils/auth'
import { FiltersType } from '../utils/filters' import { FiltersType } from '../utils/filters'
const userCategories = ['givingOut'] as const const userCategories = ['givingOut', 'needDispose'] as const
type UserCategory = typeof userCategories[number] type UserCategory = typeof userCategories[number]
const UserCategoriesNames: Record<UserCategory, string> = { const UserCategoriesNames: Record<UserCategory, string> = {
givingOut: 'Раздача', givingOut: 'Раздача',
needDispose: 'Нужно утилизировать',
} }
const userCategoriesInfos: Record<UserCategory, (ann: Announcement) => string> = { const userCategoriesInfos: Record<UserCategory, (ann: Announcement) => string> = {
givingOut: (ann: Announcement) => ( givingOut: (ann: Announcement) => (
`Годен до ${new Date(ann.bestBy).toLocaleDateString('ru')}` `Годен до ${new Date(ann.bestBy).toLocaleDateString('ru')}`
), ),
needDispose: (ann: Announcement) => (
`Были заинтересованы: ${ann.bookedBy} чел.`
),
} }
const composeUserCategoriesFilters: Record<UserCategory, () => FiltersType> = { const composeUserCategoriesFilters: Record<UserCategory, () => FiltersType> = {
givingOut: () => { givingOut: () => ({
const userId = getId() userId: getId(),
expired: false,
return ({ userId }) }),
}, needDispose: () => ({
userId: getId(),
expired: true,
}),
} }
export type { UserCategory } export type { UserCategory }

View File

@ -1,13 +1,18 @@
import { Announcement } from '../api/announcement/types' import { Announcement } from '../api/announcement/types'
import { isCategory } from '../assets/category' import { isCategory } from '../assets/category'
import { fallbackToUndefined, isInt } from './types' import { fallbackToUndefined, isBoolean, isInt } from './types'
const filterNames = ['userId', 'category', 'metro', 'bookedBy'] as const const filterNames = ['userId', 'category', 'metro', 'expired'] as const
type FilterNames = typeof filterNames[number] type FilterNames = typeof filterNames[number]
type FiltersType = Partial<Pick<Announcement, FilterNames>> type FiltersType = Partial<
Pick<Announcement, FilterNames & keyof Announcement> &
{
expired: boolean,
}
>
const defaultFilters: FiltersType = { userId: undefined, category: undefined, metro: undefined, bookedBy: undefined } const defaultFilters: FiltersType = { userId: undefined, category: undefined, metro: undefined, expired: false }
const URLEncodeFilters = (filters: FiltersType) => ( const URLEncodeFilters = (filters: FiltersType) => (
Object.fromEntries( Object.fromEntries(
@ -31,17 +36,16 @@ const URLDecoreFilters = (params: URLSearchParams): FiltersType => {
) )
return { return {
bookedBy: fallbackToUndefined(Number.parseInt(strFilters['bookedBy']), isInt), userId: fallbackToUndefined(Number.parseInt(strFilters['userId']), isInt),
category: fallbackToUndefined(strFilters['category'], isCategory), category: fallbackToUndefined(strFilters['category'], isCategory),
metro: strFilters['metro'], metro: strFilters['metro'],
userId: fallbackToUndefined(Number.parseInt(strFilters['userId']), isInt), expired: fallbackToUndefined(strFilters['bookedBy'] === 'true', isBoolean),
} }
} }
const convertFilterNames = (input: Record<string, string>) => ({ const convertFilterNames = (input: Record<string, string>) => ({
...input, ...input,
...(input['userId'] === undefined ? {} : { 'user_id': input['userId'] }), ...(input['userId'] === undefined ? {} : { 'user_id': input['userId'] }),
...(input['bookedBy'] === undefined ? {} : { 'booked_by': input['bookedBy'] }),
}) })
export type { FilterNames, FiltersType } export type { FilterNames, FiltersType }

View File

@ -64,6 +64,10 @@ const isInt = (obj: unknown): obj is number => (
Number.isSafeInteger(obj) Number.isSafeInteger(obj)
) )
const isBoolean = (obj: unknown): obj is boolean => (
typeof obj === 'boolean'
)
function fallbackToUndefined<T>(obj: unknown, isT: ((obj: unknown) => obj is T)) { function fallbackToUndefined<T>(obj: unknown, isT: ((obj: unknown) => obj is T)) {
if (!isT(obj)) return undefined if (!isT(obj)) return undefined
@ -80,4 +84,4 @@ type SetState<T> = React.Dispatch<React.SetStateAction<T>>
export type { SetState } export type { SetState }
export { isRecord, isObject, isConst, isLiteralUnion, isArrayOf, isString, isInt, fallbackToUndefined, fallbackTo } export { isRecord, isObject, isConst, isLiteralUnion, isArrayOf, isString, isInt, isBoolean, fallbackToUndefined, fallbackTo }