Skip to content

Commit

Permalink
Merge branch 'master' into feature/group-invite-link
Browse files Browse the repository at this point in the history
  • Loading branch information
Repumba authored May 30, 2023
2 parents 6adbe5d + cc1f3a9 commit 73434b9
Show file tree
Hide file tree
Showing 31 changed files with 957 additions and 789 deletions.
3 changes: 2 additions & 1 deletion mwdb/web/src/__tests__/hooks/useCheckCapabilities.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { renderHook } from "@testing-library/react";
import { useCheckCapabilities } from "@mwdb-web/commons/hooks";
import { AuthContextValues, Capability } from "@mwdb-web/types/types";
import { Capability } from "@mwdb-web/types/types";
import { AuthContext } from "@mwdb-web/commons/auth";
import { AuthProviderProps } from "@mwdb-web/types/props";
import { AuthContextValues } from "@mwdb-web/types/context";

describe("useCheckCapabilities", () => {
const authContextValue = {
Expand Down
40 changes: 27 additions & 13 deletions mwdb/web/src/commons/api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ import {
GetUserProfileResponse,
GetUserResponse,
GetUsersResponse,
OauthGetIdentitiesResponse,
OauthGetLogoutLinkResponse,
OauthGetProvidersResponse,
OauthGetSingleProviderResponse,
OauthRemoveSingleProviderResponse,
OauthUpdateSingleProviderResponse,
PullObjectRemoteResponse,
PushObjectRemoteResponse,
RegisterGroupResponse,
Expand Down Expand Up @@ -87,7 +93,8 @@ import {
UploadFileResponse,
UserRequestPasswordChangeResponse,
} from "@mwdb-web/types/api";
import { Attribute, ObjectType } from "@mwdb-web/types/types";
import { Attribute, Capability, ObjectType } from "@mwdb-web/types/types";
import { APIProviderProps } from "@mwdb-web/types/props";

function getApiForEnvironment() {
// Default API endpoint
Expand Down Expand Up @@ -203,35 +210,42 @@ function oauthRegisterProvider(
});
}

function oauthGetProviders() {
function oauthGetProviders(): OauthGetProvidersResponse {
return axios.get("/oauth");
}

function oauthGetSingleProvider(provider_name: string) {
function oauthGetSingleProvider(
provider_name: string
): OauthGetSingleProviderResponse {
return axios.get(`/oauth/${provider_name}`);
}

function oauthUpdateSingleProvider(name: string, value: string) {
function oauthUpdateSingleProvider(
name: string,
value: string
): OauthUpdateSingleProviderResponse {
return axios.put(`/oauth/${name}`, value);
}

function oauthRemoveSingleProvider(name: string) {
function oauthRemoveSingleProvider(
name: string
): OauthRemoveSingleProviderResponse {
return axios.delete(`/oauth/${name}`);
}

function oauthGetIdentities() {
function oauthGetIdentities(): OauthGetIdentitiesResponse {
return axios.get("/oauth/identities");
}

function oauthGetLogoutLink(provider: string) {
function oauthGetLogoutLink(provider: string): OauthGetLogoutLinkResponse {
return axios.get(`/oauth/${provider}/logout`);
}

function apiKeyAdd(login: string, name: string): ApiKeyAddResponse {
return axios.post(`/user/${login}/api_key`, { name });
}

function apiKeyRemove(key_id: number): ApiKeyRemoveResponse {
function apiKeyRemove(key_id: number | string): ApiKeyRemoveResponse {
return axios.delete(`/api_key/${key_id}`);
}

Expand Down Expand Up @@ -385,7 +399,10 @@ function registerGroup(name: string): RegisterGroupResponse {
return axios.post(`/group/${name}`, { name });
}

function updateGroup(name: string, value: string): UpdateGroupResponse {
function updateGroup(
name: string,
value: { capabilities: Capability[] }
): UpdateGroupResponse {
return axios.put(`/group/${name}`, value);
}

Expand Down Expand Up @@ -875,10 +892,7 @@ export const api = {
enableSharing3rdParty,
};

type APIProviderProps = {
children: React.ReactNode;
};

// TODO: api context is not needed, remove it when all components will rewrite to TypeScript
export const APIContext = React.createContext({});
export function APIProvider(props: APIProviderProps) {
return (
Expand Down
2 changes: 1 addition & 1 deletion mwdb/web/src/commons/auth/context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthContextValues } from "@mwdb-web/types/types";
import React from "react";
import { AuthContextValues } from "@mwdb-web/types/context";

export const AuthContext = React.createContext<AuthContextValues>(
{} as AuthContextValues
Expand Down
3 changes: 2 additions & 1 deletion mwdb/web/src/commons/auth/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { api } from "../api";

import { omit, isEqual, isNil } from "lodash";
import { AuthContext } from "./context";
import { AuthContextValues, Capability, User } from "@mwdb-web/types/types";
import { Capability, User } from "@mwdb-web/types/types";
import { AuthProviderProps } from "@mwdb-web/types/props";
import { AuthContextValues } from "@mwdb-web/types/context";

export const localStorageAuthKey = "user";

Expand Down
2 changes: 1 addition & 1 deletion mwdb/web/src/commons/config/context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigContextValues } from "@mwdb-web/types/types";
import { ConfigContextValues } from "@mwdb-web/types/context";
import React from "react";

export const ConfigContext = React.createContext<ConfigContextValues>(
Expand Down
3 changes: 2 additions & 1 deletion mwdb/web/src/commons/config/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { isEqual } from "lodash";
import { api } from "../api";
import { ConfigContext } from "./context";
import { AuthContext } from "../auth";
import { ConfigContextValues, ServerInfo, User } from "@mwdb-web/types/types";
import { ServerInfo, User } from "@mwdb-web/types/types";
import { ConfigContextValues } from "@mwdb-web/types/context";

const configUpdate = Symbol("configUpdate");
const configError = Symbol("configError");
Expand Down
5 changes: 4 additions & 1 deletion mwdb/web/src/commons/ui/DateString.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
type Props = {
date: string;
date?: string | Date;
};

export default function DateString(props: Props) {
if (!props.date) {
return <></>;
}
const date = props.date;
const d = new Date(date);
return <span>{date != null ? d.toUTCString() : "(never)"}</span>;
Expand Down
2 changes: 1 addition & 1 deletion mwdb/web/src/commons/ui/ObjectTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IconProp } from "@fortawesome/fontawesome-svg-core";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { capitalize } from "../helpers";
import { TabContextValues } from "@mwdb-web/types/types";
import { TabContextValues } from "@mwdb-web/types/context";

export const TabContext = React.createContext<TabContextValues>(
{} as TabContextValues
Expand Down
4 changes: 2 additions & 2 deletions mwdb/web/src/commons/ui/ShowIf.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
type Props = {
condition: boolean;
children: React.ReactNode;
children: JSX.Element;
};

export function ShowIf({ condition, children }: Props) {
return condition ? children : [];
return condition ? children : <></>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from "react";
import { useContext, useEffect, useState } from "react";
import { NavLink, useParams, Outlet } from "react-router-dom";

import { faUserCog } from "@fortawesome/free-solid-svg-icons";
Expand All @@ -10,6 +10,7 @@ import { ConfigContext } from "@mwdb-web/commons/config";
import { View } from "@mwdb-web/commons/ui";
import { useViewAlert } from "@mwdb-web/commons/hooks";
import DeleteCapabilityModal from "../Settings/Views/DeleteCapabilityModal";
import { Capability, User } from "@mwdb-web/types/types";

function ProfileNav() {
const config = useContext(ConfigContext);
Expand All @@ -32,12 +33,10 @@ function ProfileNav() {
<NavLink end to="/profile/api-keys" className="nav-link">
API keys
</NavLink>
{config.config["is_oidc_enabled"] ? (
{config.config["is_oidc_enabled"] && (
<NavLink end to="/profile/oauth" className="nav-link">
OpenID Connect
</NavLink>
) : (
[]
)}
</div>
<hr />
Expand All @@ -48,15 +47,18 @@ function ProfileNav() {
export default function ProfileView() {
const auth = useContext(AuthContext);
const { redirectToAlert, setAlert } = useViewAlert();
const user = useParams().user || auth.user.login;
const [profile, setProfile] = useState({});
const userLogin = useParams().user || auth.user.login;
const [profile, setProfile] = useState<User>({} as User);
const [capabilitiesToDelete, setCapabilitiesToDelete] = useState("");

useEffect(() => {
getProfile();
}, [user]);
}, [userLogin]);

async function changeCapabilities(capability, callback) {
async function changeCapabilities(
capability: Capability,
callback: Function
) {
try {
const capabilities = profile.capabilities.filter(
(item) => item !== capability
Expand All @@ -71,7 +73,7 @@ export default function ProfileView() {

async function getProfile() {
try {
const response = await api.getUserProfile(user);
const response = await api.getUserProfile(userLogin);
setProfile(response.data);
} catch (error) {
redirectToAlert({
Expand All @@ -81,7 +83,7 @@ export default function ProfileView() {
}
}

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

return (
<View ident="profile" fluid>
Expand All @@ -103,7 +105,7 @@ export default function ProfileView() {
changeCapabilities={changeCapabilities}
capabilitiesToDelete={capabilitiesToDelete}
setCapabilitiesToDelete={setCapabilitiesToDelete}
successMessage={`Capabilities for ${user} successfully changed`}
successMessage={`Capabilities for ${userLogin} successfully changed`}
/>
</View>
);
Expand Down
Loading

0 comments on commit 73434b9

Please sign in to comment.