mirror of
https://github.com/IgnatZakalinsky/home-works.git
synced 2026-01-30 04:52:05 +00:00
hw8
This commit is contained in:
21
src/s2-homeworks/hw08/bll/homeWorkReducer.ts
Normal file
21
src/s2-homeworks/hw08/bll/homeWorkReducer.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user