Skip to content

Commit

Permalink
Handle undefined api response cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakravarthy7102 committed Nov 10, 2023
1 parent 3e84cb9 commit 8196a87
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 60 deletions.
2 changes: 1 addition & 1 deletion build/static/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/static/js/bundle.js.map

Large diffs are not rendered by default.

16 changes: 4 additions & 12 deletions src/api/userroles/role/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type GetRolesResponse =
export const useRolesService = () => {
const fetchData = useFetchData();

const getRoles = async (query?: GetRolesQuery): Promise<GetRolesResponse> => {
const getRoles = async (query?: GetRolesQuery): Promise<GetRolesResponse | undefined> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/roles"),
method: "GET",
Expand All @@ -53,18 +53,13 @@ export const useRolesService = () => {
return body;
}

return {
status: "OK",
roles: [],
rolesCount: 0,
totalPages: 0,
};
return undefined;
};

const createRole = async (
role: string,
permissions: string[]
): Promise<{ status: "OK"; createdNewRole: boolean } | { status: "FEATURE_NOT_ENABLED_ERROR" }> => {
): Promise<{ status: "OK"; createdNewRole: boolean } | { status: "FEATURE_NOT_ENABLED_ERROR" } | undefined> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/role"),
method: "PUT",
Expand All @@ -81,10 +76,7 @@ export const useRolesService = () => {
return body;
}

return {
status: "OK",
createdNewRole: false,
};
return undefined;
};

const deleteRole = async (role: string): Promise<void> => {
Expand Down
12 changes: 8 additions & 4 deletions src/api/userroles/role/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const usePermissionsService = () => {
permissions: string[];
}
| { status: "FEATURE_NOT_ENABLED_ERROR" }
| undefined
> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/role/permissions"),
Expand All @@ -44,7 +45,7 @@ export const usePermissionsService = () => {
const addPermissionsToRole = async (
role: string,
permissions: string[]
): Promise<{ status: "OK"; createdNewRole: boolean } | { status: "FEATURE_NOT_ENABLED_ERROR" }> => {
): Promise<{ status: "OK"; createdNewRole: boolean } | { status: "FEATURE_NOT_ENABLED_ERROR" } | undefined> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/role"),
method: "PUT",
Expand All @@ -70,9 +71,12 @@ export const usePermissionsService = () => {
const removePermissionsFromRole = async (
role: string,
permissions: string[]
): Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}> => {
): Promise<
| {
status: "OK" | "UNKNOWN_ROLE_ERROR" | "FEATURE_NOT_ENABLED_ERROR";
}
| undefined
> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/role/permissions/remove"),
method: "PUT",
Expand Down
11 changes: 8 additions & 3 deletions src/api/userroles/user/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ export const useUserRolesService = () => {
const addRoleToUser = async (
userId: string,
role: string
): Promise<{
status: "OK" | "UNKNOWN_ROLE_ERROR";
}> => {
): Promise<
| {
status: "OK" | "UNKNOWN_ROLE_ERROR";
}
| undefined
> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/user/roles"),
method: "PUT",
Expand Down Expand Up @@ -55,6 +58,7 @@ export const useUserRolesService = () => {
| {
status: "FEATURE_NOT_ENABLED_ERROR";
}
| undefined
> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/user/roles"),
Expand Down Expand Up @@ -85,6 +89,7 @@ export const useUserRolesService = () => {
| {
status: "UNKNOWN_ROLE_ERROR";
}
| undefined
> => {
const response = await fetchData({
url: getApiUrl("/api/userroles/user/roles"),
Expand Down
22 changes: 14 additions & 8 deletions src/ui/components/userDetail/userDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ export const UserDetail: React.FC<UserDetailProps> = (props) => {
const { getUserMetaData } = useMetadataService();
const { getSessionsForUser } = useSessionsForUserService();
const { getRolesForUser } = useUserRolesService();
const { showModal } = useContext(PopupContentContext);
const { showModal, showToast } = useContext(PopupContentContext);

const loadUserDetail = useCallback(async () => {
const userDetailsResponse = await getUser(user);
setUserDetail(JSON.parse(JSON.stringify(userDetailsResponse)));
}, []);

const { showToast } = useContext(PopupContentContext);

const updateUser = useCallback(
async (
userId: string,
Expand Down Expand Up @@ -158,12 +156,20 @@ export const UserDetail: React.FC<UserDetailProps> = (props) => {

async function fetchUserRoles() {
const response = await getRolesForUser(user);
if (response.status === "OK") {
setUserRoles(response.roles);
}
if (response !== undefined) {
if (response.status === "OK") {
setUserRoles(response.roles);
}

if (response.status === "FEATURE_NOT_ENABLED_ERROR") {
setIsUserRolesFeatureEnabled(false);
if (response.status === "FEATURE_NOT_ENABLED_ERROR") {
setIsUserRolesFeatureEnabled(false);
}
} else {
showToast({
iconImage: getImageUrl("form-field-error-icon.svg"),
toastType: "error",
children: <>Something went wrong Please try again!</>,
});
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/ui/components/userroles/components/RolesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,26 @@ export function RolesTable({ setIsFeatureEnabled }: { setIsFeatureEnabled: (valu
try {
const response = await getRoles({ limit: PAGINATION_LIMIT.toString(), page: page.toString() });

if (response.status === "OK" && response.totalPages !== undefined) {
if (response.roles.length < 1 && page !== 1) {
setPage(page - 1);
return;
if (response !== undefined) {
if (response.status === "OK" && response.totalPages !== undefined) {
if (response.roles.length < 1 && page !== 1) {
setPage(page - 1);
return;
}
setRoles(response.roles);
setTotalPages(response.totalPages);
setRolesCount(response.rolesCount);
}
setRoles(response.roles);
setTotalPages(response.totalPages);
setRolesCount(response.rolesCount);
}

if (response.status === "FEATURE_NOT_ENABLED_ERROR") {
setIsFeatureEnabled(false);
if (response.status === "FEATURE_NOT_ENABLED_ERROR") {
setIsFeatureEnabled(false);
}
} else {
showToast({
iconImage: getImageUrl("form-field-error-icon.svg"),
toastType: "error",
children: <>Something went wrong Please try again!</>,
});
}
} catch (_) {
showToast({
Expand Down
18 changes: 13 additions & 5 deletions src/ui/components/userroles/components/dialogs/AssignRoles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ export default function AssignRolesDialog({
const fetchRoles = async () => {
setIsLoading(true);
const response = await getRoles();
if (response.status === "OK" && response.totalPages === undefined) {
setRoles(response.roles);
const normalizedRoles = response.roles.filter((r) => assignedRoles.includes(r) === false);
setNormalizedRoles(normalizedRoles);
setFilteredRoles(normalizedRoles);
if (response !== undefined) {
if (response.status === "OK" && response.totalPages === undefined) {
setRoles(response.roles);
const normalizedRoles = response.roles.filter((r) => assignedRoles.includes(r) === false);
setNormalizedRoles(normalizedRoles);
setFilteredRoles(normalizedRoles);
}
} else {
showToast({
iconImage: getImageUrl("form-field-error-icon.svg"),
toastType: "error",
children: <>Something went wrong Please try again!</>,
});
}
setIsLoading(false);
};
Expand Down
40 changes: 24 additions & 16 deletions src/ui/components/userroles/components/dialogs/CreateNewRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,38 @@ export default function CreateNewRoleDialog({
setIsLoading(true);
const response = await createRole(role, permissions);

if (response.status === "FEATURE_NOT_ENABLED_ERROR") {
showToast({
iconImage: getImageUrl("form-field-error-icon.svg"),
toastType: "error",
children: "This Feature is not enabled.",
});
closeDialog();
return;
}

if (response.status === "OK") {
if (response.createdNewRole === false) {
if (response !== undefined) {
if (response.status === "FEATURE_NOT_ENABLED_ERROR") {
showToast({
iconImage: getImageUrl("form-field-error-icon.svg"),
toastType: "error",
children: "Role already exists!",
children: "This Feature is not enabled.",
});
closeDialog();
return;
}

if (response.status === "OK") {
if (response.createdNewRole === false) {
showToast({
iconImage: getImageUrl("form-field-error-icon.svg"),
toastType: "error",
children: "Role already exists!",
});
return;
}

showToast({
iconImage: getImageUrl("checkmark-green.svg"),
toastType: "success",
children: "Role create successfully!",
});
}
} else {
showToast({
iconImage: getImageUrl("checkmark-green.svg"),
toastType: "success",
children: "Role create successfully!",
iconImage: getImageUrl("form-field-error-icon.svg"),
toastType: "error",
children: <>Something went wrong Please try again!</>,
});
}
refetchRoles();
Expand Down

0 comments on commit 8196a87

Please sign in to comment.