From 40902b362938d183efa5714ac38b678edf058063 Mon Sep 17 00:00:00 2001 From: Pascal Fischer <32096965+pascal-fischer@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:20:11 +0100 Subject: [PATCH] add resources to groups update operation (#434) --- src/components/PeerGroupSelector.tsx | 10 ++++++---- src/contexts/GroupsProvider.tsx | 11 ++++++++++- src/interfaces/Group.ts | 7 ++++++- src/modules/groups/AssignPeerToGroupModal.tsx | 3 +++ src/modules/groups/useGroupHelper.tsx | 3 +++ src/modules/peers/PeerMultiSelect.tsx | 1 + 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/components/PeerGroupSelector.tsx b/src/components/PeerGroupSelector.tsx index f9c3bb43..904ed397 100644 --- a/src/components/PeerGroupSelector.tsx +++ b/src/components/PeerGroupSelector.tsx @@ -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"; @@ -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 }, ]); } diff --git a/src/contexts/GroupsProvider.tsx b/src/contexts/GroupsProvider.tsx index 7346dd93..4aa6fe60 100644 --- a/src/contexts/GroupsProvider.tsx +++ b/src/contexts/GroupsProvider.tsx @@ -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 = { @@ -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 = @@ -112,6 +119,7 @@ export function GroupsProviderContent({ { name: group.name, peers: peers, + resources: resources, }, `/${group.id}`, ); @@ -119,6 +127,7 @@ export function GroupsProviderContent({ return groupRequest.post({ name: group.name, peers: peers, + resources: resources, }); } }; diff --git a/src/interfaces/Group.ts b/src/interfaces/Group.ts index 8b800fc4..27863486 100644 --- a/src/interfaces/Group.ts +++ b/src/interfaces/Group.ts @@ -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 @@ -14,3 +14,8 @@ export interface GroupPeer { id: string; name: string; } + +export interface GroupResource { + id: string; + type: string; +} \ No newline at end of file diff --git a/src/modules/groups/AssignPeerToGroupModal.tsx b/src/modules/groups/AssignPeerToGroupModal.tsx index 9e15305b..f4f30b5e 100644 --- a/src/modules/groups/AssignPeerToGroupModal.tsx +++ b/src/modules/groups/AssignPeerToGroupModal.tsx @@ -124,6 +124,7 @@ export const AssignGroupToPeerModalContent = ({ } as GroupPeer; }), peers_count: selectedPeers.length, + resources: group.resources, keepClientState: true, }); return; @@ -138,6 +139,7 @@ export const AssignGroupToPeerModalContent = ({ { name: group.name, peers: selectedPeers.map((peer) => peer.id), + resources: group.resources, }, "/" + group?.id, ); @@ -146,6 +148,7 @@ export const AssignGroupToPeerModalContent = ({ groupRequest.post({ name: group.name, peers: selectedPeers.map((peer) => peer.id), + resources: group.resources, }); } notify({ diff --git a/src/modules/groups/useGroupHelper.tsx b/src/modules/groups/useGroupHelper.tsx index 1b6cdde6..e617d98e 100644 --- a/src/modules/groups/useGroupHelper.tsx +++ b/src/modules/groups/useGroupHelper.tsx @@ -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; @@ -112,6 +113,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) { { name: selectedGroup.name, peers: peers, + resources: selectedGroup.resources, }, `/${selectedGroup.id}`, ); @@ -122,6 +124,7 @@ export default function useGroupHelper({ initial = [], peer }: Props) { .post({ name: selectedGroup.name, peers: groupPeers || [], + resources: selectedGroup.resources, }) .then((group) => { setSelectedGroups((prev) => { diff --git a/src/modules/peers/PeerMultiSelect.tsx b/src/modules/peers/PeerMultiSelect.tsx index a70113a7..b090c196 100644 --- a/src/modules/peers/PeerMultiSelect.tsx +++ b/src/modules/peers/PeerMultiSelect.tsx @@ -182,6 +182,7 @@ const PeerGroupMassAssignmentContent = ({ { name: group.name, peers: group.peers, + resources: group.resources, }, "/" + group.id, );