Skip to content

Commit

Permalink
feat: Update the Store
Browse files Browse the repository at this point in the history
  • Loading branch information
PintoGideon committed Sep 30, 2024
1 parent 30daeed commit bc76357
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 153 deletions.
46 changes: 36 additions & 10 deletions src/components/PipelinesCopy/PipelineUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Client from "@fnndsc/chrisapi";
import { Button } from "@patternfly/react-core";
import { Alert } from "../Antd";
import axios from "axios";
import { isEmpty } from "lodash";
import { useRef, useState } from "react";
import { Cookies } from "react-cookie";
import ChrisAPIClient from "../../api/chrisapiclient";
import { Alert } from "../Antd";
import {
fetchPluginMetasFromStore,
handleInstallPlugin,
uploadPipelineSourceFile,
extractPluginInfo,
} from "./utils";
import { Cookies } from "react-cookie";

} from "../PipelinesCopy/utils";
import { uploadPipelineSourceFile } from "./utils";
import { extractPluginInfo } from "./utils";
import { useAppSelector } from "../../store/hooks";
interface Notification {
type: "warning" | "success" | "info" | "error" | undefined;
description: string;
Expand All @@ -23,6 +23,7 @@ const PipelineUpload = ({
}: {
fetchPipelinesAgain: () => void;
}) => {
const isStaff = useAppSelector((state) => state.user.isStaff);
const cookies = new Cookies();
const fileInput = useRef<HTMLInputElement>(null);
const [notification, setNotification] = useState<Notification>({
Expand Down Expand Up @@ -57,12 +58,37 @@ const PipelineUpload = ({
version,
);

// This feature is only available to logged-in users with a valid token
// If you have a token, install the plugin by making a request to the admin URL
// Read values from cookies
const admin_username = cookies.get("admin_username");
const admin_password = cookies.get("admin_password");
const compute_resource = cookies.get("compute_resource") || "host"; // Default to 'host' if not set

// This feature is available to logged-in users or with admin credentials
const client = ChrisAPIClient.getClient();
const nonAdminCredentials = `Token ${client.auth.token}`;

let authorization: string;

if (isStaff) {
// Use the token if the user is logged in
authorization = `Token ${client.auth.token}`;
} else if (admin_username && admin_password) {
// Use admin credentials if available
const adminCredentials = btoa(
`${admin_username.trim()}:${admin_password.trim()}`,
);
authorization = `Basic ${adminCredentials}`;
} else {
throw new Error(
"Please log in or provide admin credentials to install the plugin.",
);
}

try {
await handleInstallPlugin(nonAdminCredentials, selectedPlugin);
await handleInstallPlugin(
authorization,
selectedPlugin,
compute_resource,
);
} catch (e) {
// biome-ignore lint/complexity/noUselessCatch: <explanation>
throw e;
Expand Down
2 changes: 1 addition & 1 deletion src/components/PipelinesCopy/context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
ComputeResource,
Pipeline,
PipelinePipingDefaultParameterList,
Expand Down
2 changes: 1 addition & 1 deletion src/components/PipelinesCopy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const PipelinesCopy = () => {
/>
</div>

{isStaff && <PipelineUpload fetchPipelinesAgain={fetchPipelinesAgain} />}
{<PipelineUpload fetchPipelinesAgain={fetchPipelinesAgain} />}

{isError && (
<Alert type="error" description={<span>{error.message}</span>} />
Expand Down
13 changes: 7 additions & 6 deletions src/components/PipelinesCopy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const uploadPipelineSourceFile = async (client: Client, file: File) => {
export const handleInstallPlugin = async (
adminCred: string,
pluginToInstall: Plugin,
computeResource: string,
) => {
const adminURL = import.meta.env.VITE_CHRIS_UI_URL.replace(
"/api/v1/",
Expand All @@ -99,7 +100,7 @@ export const handleInstallPlugin = async (
throw new Error("Please provide a link to your chris-admin url");

const pluginData = {
compute_names: "host",
compute_names: computeResource,
name: pluginToInstall.data.name,
version: pluginToInstall.data.version,
plugin_store_url: pluginToInstall.url,
Expand All @@ -115,22 +116,22 @@ export const handleInstallPlugin = async (
const data = await response.data;
return data;
} catch (e) {
console.log("Error", e);
if (axios.isAxiosError(e) && e.response?.data) {
const message = e.response.data;

// Log the entire error for more detailed context
console.error("Full error response:", message);

if (message.detail) {
// Handle case { detail: "Invalid Username/Password" }
throw new Error(message.detail);
}
// Check if it's an object with errors
if (typeof message === "object") {
const firstErrorKey = Object.keys(message)[0]; // Get the first error key
const firstErrorMessage = message[firstErrorKey][0]; // Get the first error message
throw new Error(`${firstErrorMessage}`);
}
} else {
throw new Error("An unexpected error occurred");
}
throw new Error("An unexpected error occurred");
}
};

Expand Down
Loading

0 comments on commit bc76357

Please sign in to comment.