Skip to content

Commit

Permalink
Fix an issue of creating duplicate groups in the access control and n…
Browse files Browse the repository at this point in the history
…etwork routes modal when group does not exist (#328)
  • Loading branch information
heisbrot authored Feb 12, 2024
1 parent dfa41a4 commit 093efc0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/app/(dashboard)/peer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function PeerOverview() {
<Breadcrumbs.Item label={peer.ip} active />
</Breadcrumbs>

<div className={"flex justify-between max-w-6xl"}>
<div className={"flex justify-between max-w-6xl items-start"}>
<div>
<div className={"flex items-center gap-3"}>
<h1 className={"flex items-center gap-3"}>
Expand Down
4 changes: 3 additions & 1 deletion src/modules/access-control/AccessControlModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ export function AccessControlModalContent({
const createOrUpdateGroups = uniqBy([...g1, ...g2], "name").map(
(g) => g.promise,
);
const groups = await Promise.all(createOrUpdateGroups);
const groups = await Promise.all(
createOrUpdateGroups.map((call) => call()),
);

let sources = sourceGroups
.map((g) => {
Expand Down
19 changes: 12 additions & 7 deletions src/modules/groups/useGroupHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useApiCall } from "@utils/api";
import { isEmpty } from "lodash";
import { useMemo, useState } from "react";
import { useSWRConfig } from "swr";
import { useGroups } from "@/contexts/GroupsProvider";
Expand Down Expand Up @@ -46,7 +47,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
return groupsToUpdate.map((group) => {
return {
name: group.name,
promise: updateOrCreateGroup(group),
promise: () => updateOrCreateGroup(group),
};
});
};
Expand All @@ -67,7 +68,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
return removePeerFromGroup(group);
});

return [...updateCalls, ...removeCalls] as Promise<Group>[];
return [...updateCalls.map((c) => c()), ...removeCalls] as Promise<Group>[];
};

const removePeerFromGroup = async (g: Group) => {
Expand All @@ -93,20 +94,24 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
const updateOrCreateGroup = async (selectedGroup: Group) => {
const groupPeers =
selectedGroup.peers &&
selectedGroup.peers.map((p) => {
const groupPeer = p as GroupPeer;
return groupPeer.id;
});
selectedGroup.peers
.map((p) => {
const groupPeer = p as GroupPeer;
return groupPeer.id;
})
.filter((p) => p !== undefined && p !== null);

// Update group if it has an id (only when peer prop is passed)
const hasId = !!selectedGroup.id;
const peers = isEmpty(groupPeers) ? undefined : groupPeers;

if (hasId) {
if (selectedGroup.name == "All" || !peer)
return Promise.resolve(selectedGroup);
return groupRequest.put(
{
name: selectedGroup.name,
peers: groupPeers || [],
peers: peers,
},
`/${selectedGroup.id}`,
);
Expand Down
4 changes: 3 additions & 1 deletion src/modules/routes/RouteModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ export function RouteModalContent({ onSuccess, peer }: ModalProps) {
const createOrUpdateGroups = uniqBy([...g1, ...g2], "name").map(
(g) => g.promise,
);
const createdGroups = await Promise.all(createOrUpdateGroups);
const createdGroups = await Promise.all(
createOrUpdateGroups.map((call) => call()),
);
const peerGroups = routingPeerGroups
.map((g) => {
const find = createdGroups.find((group) => group.name === g.name);
Expand Down
4 changes: 3 additions & 1 deletion src/modules/routes/RouteUpdateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ function RouteUpdateModalContent({ onSuccess, route, cell }: ModalProps) {
const createOrUpdateGroups = uniqBy([...g1, ...g2], "name").map(
(g) => g.promise,
);
const createdGroups = await Promise.all(createOrUpdateGroups);
const createdGroups = await Promise.all(
createOrUpdateGroups.map((call) => call()),
);
const peerGroups = routingPeerGroups
.map((g) => {
const find = createdGroups.find((group) => group.name === g.name);
Expand Down

0 comments on commit 093efc0

Please sign in to comment.