/
index.tsx
139 lines (119 loc) · 2.92 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/** @module Components.Project.Simulation.Run.Sensors.Delete */
import { useCallback, useContext, useMemo, useState } from 'react'
import { Typography } from 'antd'
import {
IFrontMutateSimulationsItem,
IFrontSimulationsItem
} from '@/api/index.d'
import { SelectContext } from '@/context/select'
import { setPoint } from '@/context/select/actions'
import { NotificationContext } from '@/context/notification'
import { addError } from '@/context/notification/actions'
import { DeleteButton } from '@/components/assets/button'
import SimulationAPI from '@/api/simulation'
import Utils from '@/lib/utils'
/**
* Props
*/
export type Simulation = Pick<IFrontSimulationsItem, 'id' | 'scheme'>
export type Swr = {
mutateOneSimulation: (
simulation: IFrontMutateSimulationsItem
) => Promise<void>
}
export interface IProps {
simulation: Simulation
index: number
swr: Swr
}
/**
* Errors
*/
export const errors = {
update: 'Unable to update simulation'
}
/**
* On delete
* @param simulation Simulation
* @param index Index
* @param dispatch Disptach
* @param swr SWR
*/
export const _onDelete = async (
simulation: Simulation,
index: number,
swr: Swr
): Promise<void> => {
// New simulation
const newSimulation = Utils.deepCopy(simulation)
// Local
const run = newSimulation.scheme.configuration.run
// Remove value
run.sensors = [
...run.sensors!.slice(0, index),
...run.sensors!.slice(index + 1)
]
// API
await SimulationAPI.update({ id: simulation.id }, [
{
key: 'scheme',
type: 'json',
method: 'set',
path: ['configuration', 'run'],
value: run
}
])
// Local
await swr.mutateOneSimulation(newSimulation)
}
/**
* Delete
* @param props Props
* @returns Delete
*/
const Delete: React.FunctionComponent<IProps> = ({
simulation,
index,
swr
}) => {
// State
const [loading, setLoading] = useState<boolean>()
// Context
const { dispatch: selectDispatch } = useContext(SelectContext)
const { dispatch: notificationDispatch } = useContext(NotificationContext)
// Run
const run = useMemo(() => simulation.scheme.configuration.run, [simulation])
// Sensor
const sensor = useMemo(() => run.sensors![index], [index, run])
/**
* On delete
*/
const onDelete = useCallback(async (): Promise<void> => {
setLoading(true)
try {
await _onDelete(simulation, index, swr)
// Unselect
selectDispatch(setPoint())
} catch (err: any) {
notificationDispatch(addError({ title: errors.update, err }))
} finally {
setLoading(false)
}
}, [simulation, index, swr, selectDispatch, notificationDispatch])
/**
* Render
*/
return (
<DeleteButton
loading={loading}
text={
<>
Are you sure you want to delete the sensor{' '}
<Typography.Text strong>{sensor.name}</Typography.Text>
</>
}
onDelete={onDelete}
/>
)
}
export default Delete