/
index.tsx
180 lines (160 loc) · 3.84 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/** @module Components.Project.Edit */
import {
KeyboardEvent,
useCallback,
useContext,
useEffect,
useRef,
useState
} from 'react'
import { Form, Input, InputRef } from 'antd'
import { LIMIT120, LIMIT50 } from '@/config/string'
import { NotificationContext } from '@/context/notification'
import { addError } from '@/context/notification/actions'
import { EditButton } from '@/components/assets/button'
import Dialog from '@/components/assets/dialog'
import { IFrontMutateProjectsItem, IFrontProjectsItem } from '@/api/index.d'
import ProjectAPI from '@/api/project'
/**
* Props
*/
export type Project = Pick<IFrontProjectsItem, 'id' | 'title' | 'description'>
export type Values = Pick<IFrontProjectsItem, 'title' | 'description'>
export type Swr = {
mutateOneProject: (project: IFrontMutateProjectsItem) => Promise<void>
}
export interface IProps {
disabled?: boolean
project: Project
swr: Swr
}
/**
* Errors
*/
export const errors = {
edit: 'Unable to edit project'
}
/**
* On edit
* @param project Project
* @param values Values
* @param swr SWR
*/
export const _onEdit = async (
project: Project,
values: Values,
swr: Swr
): Promise<void> => {
// Edit
await ProjectAPI.update({ id: project.id }, [
{
key: 'title',
value: values.title
},
{
key: 'description',
value: values.description
}
])
// Mutate projects
await swr.mutateOneProject(project)
}
/**
* Edit project
* @param props Props
* @returns Edit
*/
const Edit: React.FunctionComponent<IProps> = ({ disabled, project, swr }) => {
// Ref
const inputRef = useRef<InputRef>(null)
// State
const [visible, setVisible] = useState<boolean>(false)
const [loading, setLoading] = useState<boolean>(false)
// Context
const { dispatch } = useContext(NotificationContext)
// Autofocus
useEffect(() => {
/* istanbul ignore next */
if (inputRef.current) inputRef.current.focus()
})
/**
* Set visible true
*/
const setVisibleTrue = useCallback((): void => setVisible(true), [])
/**
* Set visible false
*/
const setVisibleFalse = useCallback((): void => setVisible(false), [])
/**
* On ok
* @param values Values
*/
const onOk = useCallback(
async (values: Values): Promise<void> => {
setLoading(true)
try {
await _onEdit(project, values, swr)
// Close
setLoading(false)
setVisible(false)
} catch (err: any) {
dispatch(addError({ title: errors.edit, err }))
setLoading(false)
throw err
}
},
[project, swr, dispatch]
)
/**
* On key up
* @param event Event
*/
const onKeyUp = useCallback(
(event: KeyboardEvent<HTMLTextAreaElement>): void =>
event.stopPropagation(),
[]
)
/**
* Render
*/
return (
<>
<EditButton disabled={disabled} dark onEdit={setVisibleTrue} />
<Dialog
title={'Edit "' + project.title + '" project'}
visible={visible}
onCancel={setVisibleFalse}
onOk={onOk}
loading={loading}
initialValues={{
title: project.title,
description: project.description
}}
>
<Form.Item
label="Name"
name="title"
rules={[
{ required: true, message: 'Name is required' },
{
max: LIMIT50,
message: 'Max ' + LIMIT50 + ' characters'
}
]}
>
<Input ref={inputRef} placeholder="Project's name" />
</Form.Item>
<Form.Item label="Description" name="description">
<Input.TextArea
style={{ marginBottom: '20px' }}
placeholder="Project's description"
showCount
maxLength={LIMIT120}
onKeyUp={onKeyUp}
/>
</Form.Item>
</Dialog>
</>
)
}
export default Edit