Skip to content

Commit 093efc0

Browse files
authored
Fix an issue of creating duplicate groups in the access control and network routes modal when group does not exist (#328)
1 parent dfa41a4 commit 093efc0

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

src/app/(dashboard)/peer/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function PeerOverview() {
132132
<Breadcrumbs.Item label={peer.ip} active />
133133
</Breadcrumbs>
134134

135-
<div className={"flex justify-between max-w-6xl"}>
135+
<div className={"flex justify-between max-w-6xl items-start"}>
136136
<div>
137137
<div className={"flex items-center gap-3"}>
138138
<h1 className={"flex items-center gap-3"}>

src/modules/access-control/AccessControlModal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ export function AccessControlModalContent({
162162
const createOrUpdateGroups = uniqBy([...g1, ...g2], "name").map(
163163
(g) => g.promise,
164164
);
165-
const groups = await Promise.all(createOrUpdateGroups);
165+
const groups = await Promise.all(
166+
createOrUpdateGroups.map((call) => call()),
167+
);
166168

167169
let sources = sourceGroups
168170
.map((g) => {

src/modules/groups/useGroupHelper.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useApiCall } from "@utils/api";
2+
import { isEmpty } from "lodash";
23
import { useMemo, useState } from "react";
34
import { useSWRConfig } from "swr";
45
import { useGroups } from "@/contexts/GroupsProvider";
@@ -46,7 +47,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
4647
return groupsToUpdate.map((group) => {
4748
return {
4849
name: group.name,
49-
promise: updateOrCreateGroup(group),
50+
promise: () => updateOrCreateGroup(group),
5051
};
5152
});
5253
};
@@ -67,7 +68,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
6768
return removePeerFromGroup(group);
6869
});
6970

70-
return [...updateCalls, ...removeCalls] as Promise<Group>[];
71+
return [...updateCalls.map((c) => c()), ...removeCalls] as Promise<Group>[];
7172
};
7273

7374
const removePeerFromGroup = async (g: Group) => {
@@ -93,20 +94,24 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
9394
const updateOrCreateGroup = async (selectedGroup: Group) => {
9495
const groupPeers =
9596
selectedGroup.peers &&
96-
selectedGroup.peers.map((p) => {
97-
const groupPeer = p as GroupPeer;
98-
return groupPeer.id;
99-
});
97+
selectedGroup.peers
98+
.map((p) => {
99+
const groupPeer = p as GroupPeer;
100+
return groupPeer.id;
101+
})
102+
.filter((p) => p !== undefined && p !== null);
100103

101104
// Update group if it has an id (only when peer prop is passed)
102105
const hasId = !!selectedGroup.id;
106+
const peers = isEmpty(groupPeers) ? undefined : groupPeers;
107+
103108
if (hasId) {
104109
if (selectedGroup.name == "All" || !peer)
105110
return Promise.resolve(selectedGroup);
106111
return groupRequest.put(
107112
{
108113
name: selectedGroup.name,
109-
peers: groupPeers || [],
114+
peers: peers,
110115
},
111116
`/${selectedGroup.id}`,
112117
);

src/modules/routes/RouteModal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ export function RouteModalContent({ onSuccess, peer }: ModalProps) {
113113
const createOrUpdateGroups = uniqBy([...g1, ...g2], "name").map(
114114
(g) => g.promise,
115115
);
116-
const createdGroups = await Promise.all(createOrUpdateGroups);
116+
const createdGroups = await Promise.all(
117+
createOrUpdateGroups.map((call) => call()),
118+
);
117119
const peerGroups = routingPeerGroups
118120
.map((g) => {
119121
const find = createdGroups.find((group) => group.name === g.name);

src/modules/routes/RouteUpdateModal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ function RouteUpdateModalContent({ onSuccess, route, cell }: ModalProps) {
150150
const createOrUpdateGroups = uniqBy([...g1, ...g2], "name").map(
151151
(g) => g.promise,
152152
);
153-
const createdGroups = await Promise.all(createOrUpdateGroups);
153+
const createdGroups = await Promise.all(
154+
createOrUpdateGroups.map((call) => call()),
155+
);
154156
const peerGroups = routingPeerGroups
155157
.map((g) => {
156158
const find = createdGroups.find((group) => group.name === g.name);

0 commit comments

Comments
 (0)