Skip to content

Commit

Permalink
Fix: Bad conditional return in web (#799)
Browse files Browse the repository at this point in the history
* Fix wrong conditional return in components

* Update ProfileGroups.jsx

* check all isEmpty and remove some console errors

---------

Co-authored-by: postrowinski <p.ostrowinski@gmail.com>
  • Loading branch information
KWMORALE and postrowinski committed Apr 20, 2023
1 parent cc97fb2 commit 64315a6
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 29 deletions.
5 changes: 2 additions & 3 deletions mwdb/web/src/components/Profile/ProfileView.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useContext, useEffect, useState } from "react";
import { NavLink, useParams, Outlet } from "react-router-dom";
import { isEmpty } from "lodash";

import { faUserCog } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
Expand Down Expand Up @@ -49,7 +48,7 @@ export default function ProfileView() {
const auth = useContext(AuthContext);
const { redirectToAlert, setAlert } = useViewAlert();
const user = useParams().user || auth.user.login;
const [profile, setProfile] = useState();
const [profile, setProfile] = useState({});
const [capabilitiesToDelete, setCapabilitiesToDelete] = useState("");

useEffect(() => {
Expand Down Expand Up @@ -81,7 +80,7 @@ export default function ProfileView() {
}
}

if (isEmpty(profile) || profile.login !== user) return <></>;
if (profile.login !== user) return <></>;

return (
<View ident="profile" fluid>
Expand Down
4 changes: 1 addition & 3 deletions mwdb/web/src/components/Profile/Views/ProfileGroup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function ProfileGroup() {
const { profile } = useOutletContext();
const { redirectToAlert } = useViewAlert();
const { group: groupName } = useParams();
const [workspaces, setWorkspaces] = useState();
const [workspaces, setWorkspaces] = useState([]);

const group = profile.groups.find((group) => group.name === groupName);

Expand All @@ -45,8 +45,6 @@ export default function ProfileGroup() {
}
}

if (isEmpty(workspaces)) return <></>;

if (isEmpty(group)) {
redirectToAlert({
target: "/profile",
Expand Down
8 changes: 4 additions & 4 deletions mwdb/web/src/components/Profile/Views/ProfileGroupMembers.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext, useEffect, useState } from "react";
import { Navigate, useParams, useOutletContext } from "react-router-dom";
import { isEmpty } from "lodash";
import { isEmpty, isNil } from "lodash";

import { api } from "@mwdb-web/commons/api";
import { AuthContext, Capability } from "@mwdb-web/commons/auth";
Expand Down Expand Up @@ -78,7 +78,7 @@ export default function ProfileGroupMembers() {
const { redirectToAlert } = useViewAlert();
const { profile } = useOutletContext();
const { group: groupName } = useParams();
const [workspaces, setWorkspaces] = useState();
const [workspaces, setWorkspaces] = useState([]);

const group = profile.groups.find((group) => group.name === groupName);

Expand All @@ -98,8 +98,6 @@ export default function ProfileGroupMembers() {
}
}

if (isEmpty(workspaces)) return <></>;

if (isEmpty(group)) {
redirectToAlert({
target: "/profile",
Expand All @@ -113,6 +111,8 @@ export default function ProfileGroupMembers() {
// Merge it with workspace info
const workspace = workspaces.find((group) => group.name === groupName);

if (isNil(workspace)) return <></>;

if (
!(
workspace.admins.includes(auth.user.login) ||
Expand Down
4 changes: 1 addition & 3 deletions mwdb/web/src/components/Profile/Views/ProfileGroups.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GroupBadge, useViewAlert } from "@mwdb-web/commons/ui";
export default function ProfileGroups() {
const { redirectToAlert } = useViewAlert();
const { profile } = useOutletContext();
const [workspaces, setWorkspaces] = useState();
const [workspaces, setWorkspaces] = useState([]);

useEffect(() => {
getWorkspaces();
Expand All @@ -26,8 +26,6 @@ export default function ProfileGroups() {
}
}

if (isEmpty(workspaces)) return <></>;

return (
<div className="container">
<h2>Role groups</h2>
Expand Down
21 changes: 9 additions & 12 deletions mwdb/web/src/components/Settings/Views/AccessControl.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState, useCallback, useEffect } from "react";
import { Link } from "react-router-dom";
import { isEmpty } from "lodash";

import { faSave, faTimes } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
Expand Down Expand Up @@ -29,11 +28,11 @@ function GroupAppliesTo({ group }) {
<small className="text-muted">
Applies to{" "}
{intersperse(
group["users"]
.slice(0, 3)
.map((user) => (
<Link to={`/settings/user/${user}`}>{user}</Link>
)),
group["users"].slice(0, 3).map((user) => (
<Link key={user} to={`/settings/user/${user}`}>
{user}
</Link>
)),
", "
)}
{group["users"].length > 3
Expand All @@ -59,7 +58,7 @@ function CapabilitiesHeader({ group }) {

function CapabilitiesList({ capabilities, onDelete }) {
return capabilities.map((cap) => (
<tr>
<tr key={cap}>
<td className="col-auto">
<Link
to={"#"}
Expand Down Expand Up @@ -158,6 +157,7 @@ function CapabilityChangeCard({ groups, onSubmit }) {
const selected = selectedCaps.includes(cap);
return (
<option
key={cap}
data-content={`
${changed ? "*" : ""}
<span class='badge badge-success'>${cap}</span>
Expand Down Expand Up @@ -200,7 +200,7 @@ function CapabilityChangeCard({ groups, onSubmit }) {
}

export default function AccessControl() {
const [groups, setGroups] = useState(null);
const [groups, setGroups] = useState([]);
const { setAlert } = useViewAlert();

const [isChangeModalOpen, setChangeModalOpen] = useState(false);
Expand Down Expand Up @@ -242,8 +242,6 @@ export default function AccessControl() {
getGroups();
}, []);

if (isEmpty(groups)) return <></>;

return (
<div className="container">
<h2>Access control</h2>
Expand All @@ -264,7 +262,7 @@ export default function AccessControl() {
.filter((group) => group.capabilities.length > 0)
.map((group) => (
<React.Fragment key={group.name}>
<CapabilitiesHeader group={group} />,
<CapabilitiesHeader group={group} />
<CapabilitiesList
capabilities={group.capabilities}
onDelete={(capToRemove) => {
Expand All @@ -278,7 +276,6 @@ export default function AccessControl() {
setChangeModalOpen(true);
}}
/>
,
</React.Fragment>
))}
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion mwdb/web/src/components/Settings/Views/OAuthProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function ProviderItem(props) {
export default function OAuthProvider() {
const viewAlert = useViewAlert();
const { name } = useParams();
const [provider, setProvider] = useState(null);
const [provider, setProvider] = useState({});
const [isDeleteModalOpen, setDeleteModalOpen] = useState(false);
const [isDeleteModalDisabled, setDeleteModalDisabled] = useState(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function FlagBadge({ enabled }) {
export default function SettingsOverview() {
const config = useContext(ConfigContext);
const { setAlert } = useViewAlert();
const [extendedInfo, setExtendedInfo] = useState();
const [extendedInfo, setExtendedInfo] = useState({});

useEffect(() => {
getExtendedInfo();
Expand Down
4 changes: 2 additions & 2 deletions mwdb/web/src/components/Settings/Views/UserSingleGroups.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ export default function UserSingleGroups() {

if (isEmpty(user)) return <></>;

let groupItems = user.groups
const groupItems = user.groups
.filter((group) => group.name !== "public" && group.name !== user.login)
.sort((groupA, groupB) => groupA.name.localeCompare(groupB.name));

let allGroupItems = allGroups.filter(
const allGroupItems = allGroups.filter(
(group) =>
!user.groups.map((g) => g.name).includes(group.name) &&
!group.private &&
Expand Down

0 comments on commit 64315a6

Please sign in to comment.