/
index.tsx
103 lines (89 loc) · 2.13 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
/** @module Components.Organizations.Delete */
import { useCallback, useContext, useState } from 'react'
import { Typography } from 'antd'
import {
IFrontMutateOrganizationsItem,
IFrontOrganizationsItem
} from '@/api/index.d'
import { NotificationContext } from '@/context/notification'
import { addError } from '@/context/notification/actions'
import { DeleteButton } from '@/components/assets/button'
import OrganizationAPI from '@/api/organization'
/**
* Props
*/
export interface IProps {
organization: Pick<IFrontOrganizationsItem, 'id' | 'name'>
swr: {
delOneOrganization: (
organization: IFrontMutateOrganizationsItem
) => Promise<void>
}
}
/**
* Errors
*/
export const errors = {
del: 'Unable to delete organization'
}
/**
* On delete
* @param organization Organization
* @param swr SWR
*/
export const _onDelete = async (
organization: Pick<IFrontOrganizationsItem, 'id' | 'name'>,
swr: {
delOneOrganization: (
organization: IFrontMutateOrganizationsItem
) => Promise<void>
}
): Promise<void> => {
// API
await OrganizationAPI.del({ id: organization.id })
// Local
await swr.delOneOrganization({ id: organization.id })
}
/**
* Delete
* @param props Props
* @returns Delete
*/
const Delete: React.FunctionComponent<IProps> = ({ organization, swr }) => {
// State
const [loading, setLoading] = useState<boolean>(false)
// Context
const { dispatch } = useContext(NotificationContext)
/**
* On delete
*/
const onDelete = useCallback(async (): Promise<void> => {
setLoading(true)
try {
await _onDelete(organization, swr)
} catch (err: any) {
dispatch(addError({ title: errors.del, err }))
throw err
} finally {
setLoading(false)
}
}, [organization, swr, dispatch])
/**
* Render
*/
return (
<DeleteButton
bordered
title="Delete organization"
text={
<>
Are you sure you want to delete the organization{' '}
<Typography.Text strong>{organization.name}</Typography.Text>?
</>
}
onDelete={onDelete}
loading={loading}
/>
)
}
export default Delete