fleet/frontend/pages/admin/UserManagementPage/helpers/userManagementHelpers.ts
Gabe Hernandez a85476c23b
implement member page for team details (#685)
* added reducers and kolide api teams code, hooked up empty state

* request for get all teams and remove unused loading bar

* added create team functionality|gs

* update link cell to be more generic

* create teams detail page and hook it up

* added tabbing and styling to top nav team details

* added edit and delete modal functionality

* add in table and modals for members for teams

* created reusable edit user modal and use it in manage teams page

* creating add member autocomplete

* hook up adding members to teams

* hook up real members from api into table, and empty state for table

* fix proptype warning

* hooked up table querying for member page

* added remove member modal

* added tems to edit useres on member page

* finish remove member from team

* fixed up editing on members page

* fix the role value in member table

* fix prettier errors

* fixes from PR comments round 1

* add missing error handler on add member

* add dynamic team name to member page and user dynamic user and team names to succuess and errors

* add test for userManagementHelper module

* fix lint errors

* fix tests

* add member test to row results on member page
2021-04-29 14:47:33 +01:00

54 lines
1.5 KiB
TypeScript

import { isEqual } from "lodash";
import { IInvite } from "interfaces/invite";
import { IUser, IUserUpdateBody } from "interfaces/user";
import { IFormData } from "../components/UserForm/UserForm";
type ICurrentUserData = Pick<
IUser,
"global_role" | "teams" | "name" | "email" | "sso_enabled"
>;
/**
* Helper function that will compare the current user with data from the editing
* form and return an object with the difference between the two. This can be
* be used for PATCH updates when updating a user.
* @param currentUserData
* @param formData
*/
const generateUpdateData = (
currentUserData: IUser | IInvite,
formData: IFormData
): IUserUpdateBody => {
const updatableFields = [
"global_role",
"teams",
"name",
"email",
"sso_enabled",
];
return Object.keys(formData).reduce<IUserUpdateBody>(
(updatedAttributes, attr) => {
// attribute can be updated and is different from the current value.
if (
updatableFields.includes(attr) &&
!isEqual(
formData[attr as keyof ICurrentUserData],
currentUserData[attr as keyof ICurrentUserData]
)
) {
// Note: ignore TS error as we will never have undefined set to an
// updatedAttributes value if we get to this code.
// @ts-ignore
updatedAttributes[attr as keyof ICurrentUserData] =
formData[attr as keyof ICurrentUserData];
}
return updatedAttributes;
},
{}
);
};
export default {
generateUpdateData,
};