-
Notifications
You must be signed in to change notification settings - Fork 1
/
Groups.tsx
138 lines (128 loc) · 4.21 KB
/
Groups.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
import { Button, Select } from "antd";
import PrivateGroup from "../../Components/Groups/PrivateGroup";
import PublicGroup from "../../Components/Groups/PublicGroup";
import styles from "./Groups.module.scss";
import { useQuery } from "react-query";
import { getGames } from "../../Services/games";
import Search from "antd/es/input/Search";
import { useState } from "react";
import { getGroups } from "../../Services/groups";
import {
PlusOutlined,
SortAscendingOutlined,
SortDescendingOutlined,
} from "@ant-design/icons";
import { getTags } from "../../Services/tags";
import { useNavigate } from "react-router-dom";
import { useAuth } from "../../Components/Hooks/useAuth";
function Groups() {
const navigate = useNavigate();
const { isLoggedIn } = useAuth();
const membershipOptions = [
{ value: "PRIVATE", label: "Private" },
{ value: "PUBLIC", label: "Public" },
{ value: null, label: "All" },
];
const { data: games } = useQuery(["games"], () => getGames());
const gameOptions = games?.map((game: any) => {
return { value: game.gameName, label: game.gameName };
});
const sortOptions = [{ label: "Creation Date", value: "CREATION_DATE" }];
const { data: tagOptions } = useQuery(["tagOptions", "groups"], async () => {
const data = await getTags({ tagType: "GROUP" });
return data.map((item: { name: any; id: any }) => ({
label: item.name,
value: item.id,
}));
});
const [title, setTitle] = useState("");
const [tags, setTags] = useState([]);
const [membershipPolicy, setMembershipPolicy] = useState();
const [gameName, setGameName] = useState("");
const [sortBy, setSortBy] = useState("");
const [sortDir, setSortDir] = useState<"ASCENDING" | "DESCENDING">(
"DESCENDING"
);
const { data: groups } = useQuery(
["groups", title, gameName, tags, membershipPolicy, sortBy, sortDir],
() =>
getGroups({ tags, title, gameName, membershipPolicy, sortBy, sortDir })
);
const toggleSortDir = () => {
setSortDir((currentSortDir) =>
currentSortDir === "ASCENDING" ? "DESCENDING" : "ASCENDING"
);
};
return (
<div className={styles.groupsPage}>
<div className={styles.groupsContainer}>
<div className={styles.findGroups}>
<Search
placeholder="Search groups by name"
enterButton
className={styles.search}
onSearch={setTitle}
onChange={(e) => {
e.target.value === "" ? setTitle("") : "";
}}
style={{ width: "50%" }}
/>
<Select
placeholder="Filter by membership rule"
defaultValue={null}
options={membershipOptions}
onChange={setMembershipPolicy}
style={{ width: "15%" }}
/>
<Button onClick={toggleSortDir}>
{sortDir === "DESCENDING" ? (
<SortDescendingOutlined />
) : (
<SortAscendingOutlined />
)}
</Button>
<Select
options={sortOptions}
defaultValue={sortBy}
value={"CREATION_DATE"}
onChange={setSortBy}
style={{ flex: 1 }}
/>
<Select
placeholder="Filter by game"
onChange={setGameName}
style={{ width: "30%" }}
options={gameOptions}
/>
<Select
mode="multiple"
placeholder="Filter by tag"
onChange={setTags}
style={{ width: "30%" }}
options={tagOptions}
/>
</div>
<div style={{ alignSelf: "flex-end" }}>
{isLoggedIn && (
<Button
icon={<PlusOutlined />}
onClick={() => navigate("/group/create")}
style={{ backgroundColor: "#ff824d" }}
>
Create Group
</Button>
)}
</div>
{groups &&
groups.map((group: any) =>
group.membershipPolicy === "PRIVATE" ? (
<PrivateGroup key={group.id} group={group}></PrivateGroup>
) : (
<PublicGroup key={group.id} group={group}></PublicGroup>
)
)}
</div>
</div>
);
}
export default Groups;