Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create role UI #4266

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/real-plums-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@wso2is/myaccount": patch
"@wso2is/theme": patch
"@wso2is/console": patch
"@wso2is/i18n": patch
"@wso2is/identity-apps-core": patch
---

Create role interface started
135 changes: 91 additions & 44 deletions apps/console/src/features/roles/api/roles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
* Copyright (c) 2020, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,28 +16,32 @@
* under the License.
*/

import { AsgardeoSPAClient } from "@asgardeo/auth-react";
import { AsgardeoSPAClient, HttpClientInstance } from "@asgardeo/auth-react";
import { RoleConstants } from "@wso2is/core/constants";
import { IdentityAppsApiException } from "@wso2is/core/exceptions";
import { HttpMethods, RoleListInterface } from "@wso2is/core/models";
import { AxiosError, AxiosResponse } from "axios";
import { store } from "../../core";
import useRequest, {
RequestConfigInterface,
RequestErrorInterface,
RequestResultInterface
} from "../../core/hooks/use-request";
import { CreateRoleInterface, PatchRoleDataInterface, SearchRoleInterface } from "../models";

/**
* Initialize an axios Http client.
*/
const httpClient = AsgardeoSPAClient.getInstance()
.httpRequest.bind(AsgardeoSPAClient.getInstance())
.bind(AsgardeoSPAClient.getInstance());
const httpClient: HttpClientInstance = AsgardeoSPAClient.getInstance()
.httpRequest.bind(AsgardeoSPAClient.getInstance());

/**
* Retrieve Role details for a give role id.
*
* @param roleId role id to retrieve role details
* @param roleId - role id to retrieve role details
*/
export const getRoleById = (roleId: string): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
Expand All @@ -47,21 +51,21 @@ export const getRoleById = (roleId: string): Promise<any> => {
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};

/**
* Update Data of the matched ID or the role
*
* @param roleId role id to update role details
* @param roleData Data that needs to be updated.
* @param roleId - role id to update role details
* @param roleData - Data that needs to be updated.
*/
export const updateRoleDetails = (roleId: string, roleData: PatchRoleDataInterface): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
data: roleData,
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
Expand All @@ -72,9 +76,9 @@ export const updateRoleDetails = (roleId: string, roleData: PatchRoleDataInterfa
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};
Expand All @@ -85,7 +89,7 @@ export const updateRoleDetails = (roleId: string, roleData: PatchRoleDataInterfa
* @param searchData - search query data
*/
export const searchRoleList = (searchData: SearchRoleInterface): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
data: searchData,
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
Expand All @@ -96,9 +100,9 @@ export const searchRoleList = (searchData: SearchRoleInterface): Promise<any> =>
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};
Expand All @@ -107,10 +111,10 @@ export const searchRoleList = (searchData: SearchRoleInterface): Promise<any> =>
* Delete a selected role with a given role ID.
*
* @param roleId - Id of the role which needs to be deleted.
* @returns {Promise<any>} a promise containing the status of the delete.
* @returns `Promise<any>` a promise containing the status of the delete.
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
*/
export const deleteRoleById = (roleId: string): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
Expand All @@ -120,9 +124,9 @@ export const deleteRoleById = (roleId: string): Promise<any> => {
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};
Expand All @@ -133,7 +137,7 @@ export const deleteRoleById = (roleId: string): Promise<any> => {
* @param data - data object used to create the role
*/
export const createRole = (data: CreateRoleInterface): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
data,
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
Expand All @@ -144,9 +148,9 @@ export const createRole = (data: CreateRoleInterface): Promise<any> => {
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};
Expand All @@ -158,7 +162,7 @@ export const createRole = (data: CreateRoleInterface): Promise<any> => {
* @param data - Permission data of the role
*/
export const updateRolePermissions = (roleId: string, data: unknown): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
data,
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
Expand All @@ -169,20 +173,20 @@ export const updateRolePermissions = (roleId: string, data: unknown): Promise<an
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};

/**
* Retrieve a list of all the permissions from the system.
*
* @returns {Promise<any>} a promise containing the permission list
* @returns `Promise<any>` a promise containing the permission list
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
*/
export const getPermissionList = (): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
Expand All @@ -192,20 +196,20 @@ export const getPermissionList = (): Promise<any> => {
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};

/**
* Retrieve the list of permissions available for a given Role Id.
*
* @param roleId Role Id to retrieve relevant permissions
* @param roleId - Role Id to retrieve relevant permissions
*/
export const getPermissionsForRole = (roleId: string): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
Expand All @@ -215,21 +219,21 @@ export const getPermissionsForRole = (roleId: string): Promise<any> => {
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};

/**
* Update Data of the matched ID or the role
*
* @param roleId role id to update role details
* @param roleData Data that needs to be updated.
* @param roleId - role id to update role details
* @param roleData - Data that needs to be updated.
*/
export const updateRole = (roleId: string, roleData: PatchRoleDataInterface): Promise<any> => {
const requestConfig = {
const requestConfig: RequestConfigInterface = {
data: roleData,
headers: {
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
Expand All @@ -240,9 +244,9 @@ export const updateRole = (roleId: string, roleData: PatchRoleDataInterface): Pr
};

return httpClient(requestConfig)
.then((response) => {
.then((response: AxiosResponse) => {
return Promise.resolve(response);
}).catch((error) => {
}).catch((error: AxiosError) => {
return Promise.reject(error);
});
};
Expand All @@ -251,13 +255,14 @@ export const updateRole = (roleId: string, roleData: PatchRoleDataInterface): Pr
* Retrieve the list of groups that are currently in the system.
* TODO: Return `response.data` rather than `response` and stop returning any.
*
* @param {string} domain - User store domain.
* @return {Promise<RoleListInterface | any>}
* @throws {IdentityAppsApiException}
* @param domain - User store domain.
* @returns `Promise<RoleListInterface | any>`
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
* @throws `IdentityAppsApiException`
* @deprecated - Use `useRolesList` instead.
*/
export const getRolesList = (domain: string): Promise<RoleListInterface | any> => {

const requestConfig = {
const requestConfig: RequestConfigInterface = {
headers: {
"Content-Type": "application/json"
},
Expand Down Expand Up @@ -292,3 +297,45 @@ export const getRolesList = (domain: string): Promise<RoleListInterface | any> =
error.config);
});
};

/**
* Hook to get the retrieve the list of groups that are currently in the system.
*
* @param domain - User store domain.
* @param filter - Search filter.
* @returns `RequestResultInterface<Data=RoleListInterface, Error>`
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
*/
export const useRolesList = <Data = RoleListInterface, Error = RequestErrorInterface>(
domain: string,
filter?: string
): RequestResultInterface<Data, Error> => {

const requestConfig: RequestConfigInterface = {
headers: {
"Content-Type": "application/json"
Achintha444 marked this conversation as resolved.
Show resolved Hide resolved
},
method: HttpMethods.GET,
params: {
domain,
filter
},
url: store.getState().config.endpoints.roles
};

const {
data,
error,
isValidating,
mutate,
response
} = useRequest<Data, Error>(requestConfig);

return {
data,
error,
isLoading: !error && !data,
isValidating,
mutate,
response
};
};
Loading
Loading