mirror of
https://github.com/IgnatZakalinsky/home-works.git
synced 2025-12-16 20:39:24 +00:00
31 lines
794 B
TypeScript
31 lines
794 B
TypeScript
import { UserType } from '../HW8'
|
|
|
|
type ActionType =
|
|
| { type: 'sort'; payload: 'up' | 'down' }
|
|
| { type: 'check'; payload: number }
|
|
|
|
export const homeWorkReducer = (
|
|
state: UserType[],
|
|
action: ActionType
|
|
): UserType[] => {
|
|
// need to fix any
|
|
switch (
|
|
action.type // делают студенты
|
|
) {
|
|
case 'sort': {
|
|
const newState = [...state].sort((a, b) => {
|
|
if (a.name > b.name) return 1
|
|
else if (a.name < b.name) return -1
|
|
else return 0
|
|
})
|
|
|
|
return action.payload === 'up' ? newState : newState.reverse()
|
|
}
|
|
case 'check': {
|
|
return state.filter((a) => a.age >= action.payload)
|
|
}
|
|
default:
|
|
return state
|
|
}
|
|
}
|