Skip to content

Commit

Permalink
add resources to groups update operation (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fischer authored Dec 27, 2024
1 parent fa9bcea commit 40902b3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/components/PeerGroupSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import * as React from "react";
import { Fragment, useEffect, useMemo, useState } from "react";
import { useGroups } from "@/contexts/GroupsProvider";
import { useElementSize } from "@/hooks/useElementSize";
import type { Group, GroupPeer } from "@/interfaces/Group";
import type {Group, GroupPeer, GroupResource} from "@/interfaces/Group";
import { NetworkResource } from "@/interfaces/Network";
import type { Peer } from "@/interfaces/Peer";

Expand Down Expand Up @@ -104,19 +104,21 @@ export function PeerGroupSelector({
const option = dropdownOptions.find((option) => option.name == name);
const groupPeers: GroupPeer[] | undefined =
(group?.peers as GroupPeer[]) || [];
const groupResources: GroupResource[] | undefined =
(group?.resources as GroupResource[]) || [];

if (peer) groupPeers?.push({ id: peer?.id as string, name: peer?.name });

if (!group && !option) {
addDropdownOptions([{ name: name, peers: groupPeers }]);
addDropdownOptions([{ name: name, peers: groupPeers, resources: groupResources }]);
}

if (max == 1 && values.length == 1) {
onChange([{ name: name, id: group?.id, peers: groupPeers }]);
onChange([{ name: name, id: group?.id, peers: groupPeers, resources: groupResources }]);
} else {
onChange((previous) => [
...previous,
{ name: name, id: group?.id, peers: groupPeers },
{ name: name, id: group?.id, peers: groupPeers, resources: groupResources },
]);
}

Expand Down
11 changes: 10 additions & 1 deletion src/contexts/GroupsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import useFetchApi, { useApiCall } from "@utils/api";
import { merge, sortBy, unionBy } from "lodash";
import React, { useEffect, useState } from "react";
import { useLoggedInUser } from "@/contexts/UsersProvider";
import { Group } from "@/interfaces/Group";
import {Group, GroupResource} from "@/interfaces/Group";
import { Peer } from "@/interfaces/Peer";

type Props = {
Expand Down Expand Up @@ -102,6 +102,13 @@ export function GroupsProviderContent({
return peer.id;
}) as string[];

let resources = group?.resources?.map((r) => {
let isString = typeof r === "string";
if (isString) return r;
let resource = r as GroupResource;
return resource.id;
}) as string[];

if (group.name === "All") return Promise.resolve(group);

const groupID =
Expand All @@ -112,13 +119,15 @@ export function GroupsProviderContent({
{
name: group.name,
peers: peers,
resources: resources,
},
`/${group.id}`,
);
} else {
return groupRequest.post({
name: group.name,
peers: peers,
resources: resources,
});
}
};
Expand Down
7 changes: 6 additions & 1 deletion src/interfaces/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface Group {
name: string;
peers?: GroupPeer[] | string[];
peers_count?: number;
resources?: string[];
resources?: GroupResource[] | string[];
resources_count?: number;

// Frontend only
Expand All @@ -14,3 +14,8 @@ export interface GroupPeer {
id: string;
name: string;
}

export interface GroupResource {
id: string;
type: string;
}
3 changes: 3 additions & 0 deletions src/modules/groups/AssignPeerToGroupModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export const AssignGroupToPeerModalContent = ({
} as GroupPeer;
}),
peers_count: selectedPeers.length,
resources: group.resources,
keepClientState: true,
});
return;
Expand All @@ -138,6 +139,7 @@ export const AssignGroupToPeerModalContent = ({
{
name: group.name,
peers: selectedPeers.map((peer) => peer.id),
resources: group.resources,
},
"/" + group?.id,
);
Expand All @@ -146,6 +148,7 @@ export const AssignGroupToPeerModalContent = ({
groupRequest.post({
name: group.name,
peers: selectedPeers.map((peer) => peer.id),
resources: group.resources,
});
}
notify({
Expand Down
3 changes: 3 additions & 0 deletions src/modules/groups/useGroupHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
return groupRequest.put(
{
name: g.name,
resources: g.resources,
peers: newPeerGroups
? newPeerGroups.map((p) => {
const groupPeer = p as GroupPeer;
Expand Down Expand Up @@ -112,6 +113,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
{
name: selectedGroup.name,
peers: peers,
resources: selectedGroup.resources,
},
`/${selectedGroup.id}`,
);
Expand All @@ -122,6 +124,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) {
.post({
name: selectedGroup.name,
peers: groupPeers || [],
resources: selectedGroup.resources,
})
.then((group) => {
setSelectedGroups((prev) => {
Expand Down
1 change: 1 addition & 0 deletions src/modules/peers/PeerMultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const PeerGroupMassAssignmentContent = ({
{
name: group.name,
peers: group.peers,
resources: group.resources,
},
"/" + group.id,
);
Expand Down

0 comments on commit 40902b3

Please sign in to comment.