I have an api endpoint such as this and I need to make a put request to this endpoint.
api/user-group/{id}/invite/{userId}
The problem i'm having is my form contains a multi-list, react select list that allows users to select as many groups as they want and so everytime a user selects a group I'm storing the groups id in local state but my api endpoint only takes in one group id. The solution I think is too loop over the group ids and do separate requests. Can someone guide me on how to achieve this solution or is there a better solution?
My Code:
const AddUserToGroupPage = () => {
const [currentUserId, setCurrentUserId] = useState('');
const [GroupIds, setGroupIds] = useState([]);
const { inviteUserToGroup } = useUserGroupApi();
const InviteExistingUser = async () => {
const body = {};
const res = await inviteUserToGroup({
id: groupIds,
body,
userId: currentUserId,
});
return res;
};
const onGroupsSelected = (selected) => {
if (selected) {
setGroupIds(selected.map((select) => select.value));
}
};
return (
<>
<Select
name="userGroups"
label="User Groups"
placeholder="Select as many groups as you want"
id="userGroups"
defaultOptions
options={groups}
isMulti
onSelectChange={onGroupsSelected}
/>
</>
)
}