-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathSidebar.tsx
More file actions
105 lines (97 loc) · 3.6 KB
/
Sidebar.tsx
File metadata and controls
105 lines (97 loc) · 3.6 KB
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
import React, { useState, useContext } from 'react';
import { Form, Input, Layout, Menu, Button, Divider, Modal, Tooltip } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import styles from '../styles/sidebar.module.scss';
import { globalContext, GlobalContext } from '../context/GlobalContextProvider';
import { SectionProps } from './Section';
const { TextArea } = Input;
const { Sider } = Layout;
export const Sidebar = () => {
const [addSectionVisible, setAddSectionVisibility] = useState(false);
const [form] = Form.useForm();
const context = useContext(globalContext) as GlobalContext;
const addSection = (formValues: { name: string }) => {
context.addSection({
...formValues,
fields: [],
});
setAddSectionVisibility(false);
};
const generateMenu = (activeSectionIndex: number, sections: SectionProps[]) => {
return (
<Menu
theme="dark"
mode="inline"
defaultSelectedKeys={[activeSectionIndex.toString()]}
onSelect={({ item, key }) => context.changeActiveSection(parseInt(key.toString()))}
>
{sections.map((section, sectionIndex) => {
return <Menu.Item key={sectionIndex}>{section.name}</Menu.Item>;
})}
</Menu>
);
};
const generateAddSectionForm = () => {
return (
<Form form={form} layout="vertical">
<Form.Item name="name" label="Name" rules={[{ required: true }]}>
<TextArea rows={1} autoSize={true} />
</Form.Item>
</Form>
);
};
const generateAddSectionModal = () => {
return (
<Modal
title="New Section Options"
visible={addSectionVisible}
okText="Add section"
onOk={() => {
form.validateFields()
.then((values: { name: string }) => {
form.resetFields();
addSection(values);
})
.catch(info => {
console.log('Validate Failed:', info);
});
}}
onCancel={() => setAddSectionVisibility(false)}
>
{generateAddSectionForm()}
</Modal>
);
};
return (
<Sider collapsible breakpoint="md" collapsedWidth={0}>
<div className={styles.logo}>
<h2>GITHUB</h2>
<h1>PROFILINATOR</h1>
</div>
{generateMenu(context.activeSectionIndex, context.sections)}
<Divider />
<div className={styles.buttonContainer}>
<Tooltip placement="top" title={<span>Add a Section</span>}>
<Button
type="dashed"
ghost
block
className={styles.addSectionButton}
onClick={() => setAddSectionVisibility(true)}
>
<PlusOutlined />
Section
</Button>
</Tooltip>
</div>
{generateAddSectionModal()}
<div className={styles.moreFromCreator}>
<p>More tools:</p>
<a href="https://www.rewardmatrix.in/" target="_blank" rel="noreferrer">
- RewardMatrix
</a>
</div>
</Sider>
);
};
export default Sidebar;