mirror of
https://github.com/fleetdm/fleet
synced 2026-04-24 15:07:29 +00:00
This adds a new ListItem component and FileUploader component and updates the Custom settings and scripts page to use this new ListItem component. This List component centralises where the markup and styles live. We still need to update the bootstrap list item and eula upload list item but will do that in the future. - [x] Manual QA for all new/changed functionality
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import React, { useContext, useState } from "react";
|
|
import { AxiosResponse } from "axios";
|
|
|
|
import { IApiError } from "interfaces/errors";
|
|
import { NotificationContext } from "context/notification";
|
|
import scriptAPI from "services/entities/scripts";
|
|
|
|
import FileUploader from "components/FileUploader";
|
|
|
|
import { getErrorMessage } from "./helpers";
|
|
|
|
const baseClass = "script-uploader";
|
|
|
|
interface IScriptPackageUploaderProps {
|
|
currentTeamId: number;
|
|
onUpload: () => void;
|
|
}
|
|
|
|
const ScriptPackageUploader = ({
|
|
currentTeamId,
|
|
onUpload,
|
|
}: IScriptPackageUploaderProps) => {
|
|
const { renderFlash } = useContext(NotificationContext);
|
|
const [showLoading, setShowLoading] = useState(false);
|
|
|
|
const onUploadFile = async (files: FileList | null) => {
|
|
if (!files || files.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const file = files[0];
|
|
|
|
setShowLoading(true);
|
|
try {
|
|
await scriptAPI.uploadScript(file, currentTeamId);
|
|
renderFlash("success", "Successfully uploaded!");
|
|
onUpload();
|
|
} catch (e) {
|
|
const error = e as AxiosResponse<IApiError>;
|
|
renderFlash("error", `Couldn't upload. ${getErrorMessage(error)}`);
|
|
} finally {
|
|
setShowLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<FileUploader
|
|
className={baseClass}
|
|
graphicName="file-sh"
|
|
message="Script (.sh)"
|
|
additionalInfo="Script will run with “#!/bin/sh”."
|
|
accept=".sh"
|
|
onFileUpload={onUploadFile}
|
|
isLoading={showLoading}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default ScriptPackageUploader;
|