/
Person.tsx
163 lines (154 loc) · 3.87 KB
/
Person.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
import { useState, useEffect, useRef } from "react";
import { Table, Button, Modal, Popconfirm, message } from "antd";
import type { ColumnsType } from "antd/es/table";
import PersonEdit from "./PersonEdit";
import { apiCall } from "./util";
import { useGlobalContext } from "./GlobalContext";
interface DataType {
key?: React.Key;
id?: number;
name: string;
}
export default function Person() {
const { refreshDescriptor, refreshPerson } = useGlobalContext();
const [editVisible, setEditVisible] = useState(false);
const [columns, setColumns] = useState<ColumnsType<DataType>>();
const [data, setData] = useState<DataType[]>([]);
const editFormRef = useRef<any>();
const [currentRecord, setCurrentRecord] = useState<DataType>();
const [errorMessage, errorMessageHolder] = message.useMessage();
const doDelete = async (id: number) => {
try {
await apiCall("person_delete", { id });
refreshPerson();
} catch (e) {
setEditVisible(false);
console.error(e);
errorMessage.open({
type: "error",
content: "Can't delete person",
});
}
};
const doEdit = (record: any) => {
setCurrentRecord(record);
setEditVisible(true);
};
const load = async () => {
try {
const result = await apiCall("person_select");
const items = JSON.parse(JSON.parse(result as unknown as string));
setData(
items.map((item: any) => ({
key: item.id,
...item,
}))
);
} catch (e) {
console.error(e);
errorMessage.open({
type: "error",
content: "Can't delete person",
});
}
};
useEffect(() => {
load();
const columns: ColumnsType<DataType> = [
{
title: "Name",
dataIndex: "name",
key: "name",
fixed: "left",
// sorter: (a, b) => a.l_name.length - b.skill_name.length,
},
{
title: "...",
dataIndex: "operation",
width: 130,
fixed: "right",
render: (_, record: DataType) => (
<>
<a
href="#"
onClick={() => {
doEdit(record);
}}
>
Edit
</a>{" "}
|
<Popconfirm
title="Delete staff"
onConfirm={() => doDelete(record.id as number)}
>
<a>Delete</a>
</Popconfirm>
</>
),
},
];
setColumns(columns);
}, [refreshDescriptor.person]);
const doEditOk = () => {
editFormRef.current.submit();
};
const handleEditOk = async (formData: DataType) => {
try {
if (currentRecord?.id) {
await apiCall("person_update", {
name: formData.name,
id: currentRecord.id,
});
} else {
await apiCall("person_insert", {
name: formData.name,
});
}
setEditVisible(false);
refreshPerson();
} catch (e) {
setEditVisible(false);
console.error(e);
errorMessage.open({
type: "error",
content: "Can't update person",
});
}
};
const handleEditCancel = () => {
setEditVisible(false);
};
const addNewRow = () => {
setCurrentRecord({ name: "" });
setEditVisible(true);
};
return !columns ? (
<></>
) : (
<div>
{errorMessageHolder}
<Button onClick={addNewRow} style={{ margin: 10 }}>
New Person
</Button>
<Table
columns={columns}
dataSource={data}
pagination={false}
scroll={{ y: "calc(100vh - 200px)" }}
/>
<Modal
title="Person"
open={editVisible}
onOk={doEditOk}
onCancel={handleEditCancel}
>
<PersonEdit
ref={editFormRef}
currentRecord={currentRecord}
handleEditOk={handleEditOk}
></PersonEdit>
</Modal>
</div>
);
}