generated from muratkeremozcan/react-cypress-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Heroes.tsx
85 lines (78 loc) · 2.57 KB
/
Heroes.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, {useState} from 'react'
import {useNavigate, Routes, Route} from 'react-router-dom'
import ListHeader from 'components/ListHeader'
import ModalYesNo from 'components/ModalYesNo'
import ErrorComp from 'components/ErrorComp'
import HeroList from './HeroList'
import HeroDetail from './HeroDetail'
import {useGetEntities} from 'hooks/useGetEntities'
import {useDeleteEntity} from 'hooks/useDeleteEntity'
import type {Hero} from 'models/Hero'
import {curry} from 'ramda'
export default function Heroes() {
const [showModal, setShowModal] = useState<boolean>(false)
const {entities: heroes, getError} = useGetEntities('heroes')
const [heroToDelete, setHeroToDelete] = useState<Hero | null>(null)
const {deleteEntity: deleteHero, isDeleteError} = useDeleteEntity('hero')
const navigate = useNavigate()
const addNewHero = () => navigate('/heroes/add-hero')
const handleRefresh = () => navigate('/heroes')
const handleCloseModal = () => {
setHeroToDelete(null)
setShowModal(false)
}
// currying: the outer fn takes our custom arg and returns a fn that takes the event
// const handleDeleteHero = (hero: Hero) => () => {
// setHeroToDelete(hero)
// setShowModal(true)
// }
// we can use Ramda curry instead, we have to pass the unused event argument though
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const handleDeleteHero = curry((hero: Hero, e: React.MouseEvent) => {
setHeroToDelete(hero)
setShowModal(true)
})
const handleDeleteFromModal = () => {
heroToDelete ? deleteHero(heroToDelete) : null
setShowModal(false)
}
if (getError || isDeleteError) {
return <ErrorComp />
}
return (
<div data-cy="heroes">
<ListHeader
title="Heroes"
handleAdd={addNewHero}
handleRefresh={handleRefresh}
/>
<div>
<div>
<Routes>
<Route
path=""
element={
<HeroList heroes={heroes} handleDeleteHero={handleDeleteHero} />
}
/>
<Route path="/add-hero" element={<HeroDetail />} />
<Route path="/edit-hero/:id" element={<HeroDetail />} />
<Route
path="*"
element={
<HeroList heroes={heroes} handleDeleteHero={handleDeleteHero} />
}
/>
</Routes>
</div>
</div>
{showModal && (
<ModalYesNo
message="Would you like to delete the hero?"
onNo={handleCloseModal}
onYes={handleDeleteFromModal}
/>
)}
</div>
)
}