Skip to content

Commit

Permalink
Implement 'Save' button
Browse files Browse the repository at this point in the history
The 'Save' button will save the newly
added changes of a specific user.

Signed-off-by: Carla Martinez <carlmart@redhat.com>
  • Loading branch information
carma12 committed Jul 17, 2023
1 parent f29c229 commit 06b9fdf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/UserSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface PropsToUserSettings {
certData: any;
onRefresh?: () => void;
onRevert?: () => void;
onSave?: () => void;
isDataLoading?: boolean;
from: "active-users" | "stage-users" | "preserved-users";
}
Expand Down Expand Up @@ -111,7 +112,12 @@ const UserSettings = (props: PropsToUserSettings) => {
{
key: 2,
element: (
<SecondaryButton isDisabled={!fieldsChanged}>Save</SecondaryButton>
<SecondaryButton
isDisabled={!fieldsChanged}
onClickHandler={props.onSave}
>
Save
</SecondaryButton>
),
},
{
Expand Down
21 changes: 21 additions & 0 deletions src/pages/ActiveUsers/ActiveUsersTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import useUserSettingsData from "src/hooks/useUserSettingsData";
import {
BatchResult,
Command,
FindRPCResponse,
useRefreshUsersMutation,
useSaveUserDataMutation,
} from "src/services/rpc";

const ActiveUsersTabs = () => {
Expand All @@ -55,6 +57,9 @@ const ActiveUsersTabs = () => {
// Define function to execute 'useRefreshUsersMutation' hook (via Mutation)
const [retrieveUserData] = useRefreshUsersMutation();

// Define function to execute 'useSaveUserDataMutation' hook (via mutation)
const [saveData] = useSaveUserDataMutation();

// Update states on receiving 'batchResponse' from 'useUserSettingsData'
useEffect(() => {
if (Object.keys(batchResponse).length > 0) {
Expand Down Expand Up @@ -130,6 +135,21 @@ const ActiveUsersTabs = () => {
// TODO: Set the rest of the values
};

// Save button
const saveUserData = () => {
// Prepare payload
// - TODO: Take the new updated data from fields
// - TEST
const payload = { uid: "alee", params: { title: "Senior", sn: "Lee" } };

// Make API call
saveData(payload).then((response) => {
if ("data" in response) {
console.log(response.data as FindRPCResponse);
}
});
};

// Tab
const [activeTabKey, setActiveTabKey] = useState(0);

Expand Down Expand Up @@ -191,6 +211,7 @@ const ActiveUsersTabs = () => {
onUserChange={setUser}
onRefresh={refreshUserData}
onRevert={revertUserData}
onSave={saveUserData}
isDataLoading={isDataLoading}
from="active-users"
/>
Expand Down
29 changes: 28 additions & 1 deletion src/services/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@reduxjs/toolkit/query/react";
// Utils
import { API_VERSION_BACKUP } from "src/utils/utils";
import { Metadata } from "src/utils/datatypes/globalDataTypes";
import { Metadata, User } from "src/utils/datatypes/globalDataTypes";

export interface UIDType {
dn: string;
Expand Down Expand Up @@ -120,6 +120,8 @@ export const getBatchCommand = (commandData: Command[], apiVersion: string) => {
return payloadBatchParams;
};

type userModData = { uid: string, params: Partial<User> };

// Endpoints that will be called from anywhere in the application.
// Two types:
// - Queries: https://redux-toolkit.js.org/rtk-query/usage/queries
Expand Down Expand Up @@ -245,6 +247,30 @@ export const api = createApi({
transformResponse: (response: BatchResponse): BatchResult[] =>
response.result.results,
}),
saveUserData: build.mutation<FindRPCResponse, userModData>({
query: (data) => {
console.log("--> data.params");
console.log(data.params);
const baseParams = {
all: true,
rights: true,
version: API_VERSION_BACKUP,
}

const fullParams = Object.assign(baseParams, data.params)
console.log("--> fullParams");
console.log(fullParams);


return getCommand({
method: "user_mod",
params: [
[data.uid],
fullParams,
],
});
},
}),
}),
});

Expand All @@ -257,4 +283,5 @@ export const {
useGetObjectMetadataQuery,
useGetUsersFullDataQuery,
useRefreshUsersMutation,
useSaveUserDataMutation,
} = api;

0 comments on commit 06b9fdf

Please sign in to comment.