|
| 1 | +import React, { useContext, useState } from "react"; |
| 2 | +import { AxiosResponse } from "axios"; |
| 3 | + |
| 4 | +import { IApiError } from "interfaces/errors"; |
| 5 | +import { NotificationContext } from "context/notification"; |
| 6 | +import mdmAPI from "services/entities/mdm"; |
| 7 | + |
| 8 | +import CustomLink from "components/CustomLink"; |
| 9 | +import FileUploader from "components/FileUploader"; |
| 10 | + |
| 11 | +import { UPLOAD_ERROR_MESSAGES, getErrorMessage } from "./helpers"; |
| 12 | + |
| 13 | +const baseClass = "setup-assistant-package-uploader"; |
| 14 | + |
| 15 | +interface ISetupAssistantPackageUploaderProps { |
| 16 | + currentTeamId: number; |
| 17 | + onUpload: () => void; |
| 18 | +} |
| 19 | + |
| 20 | +const SetupAssistantPackageUploader = ({ |
| 21 | + currentTeamId, |
| 22 | + onUpload, |
| 23 | +}: ISetupAssistantPackageUploaderProps) => { |
| 24 | + const { renderFlash } = useContext(NotificationContext); |
| 25 | + const [showLoading, setShowLoading] = useState(false); |
| 26 | + |
| 27 | + const onUploadFile = async (files: FileList | null) => { |
| 28 | + setShowLoading(true); |
| 29 | + |
| 30 | + if (!files || files.length === 0) { |
| 31 | + setShowLoading(false); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const file = files[0]; |
| 36 | + |
| 37 | + // quick exit if the file type is incorrect |
| 38 | + if (!file.name.includes(".pkg")) { |
| 39 | + renderFlash("error", UPLOAD_ERROR_MESSAGES.wrongType.message); |
| 40 | + setShowLoading(false); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + await mdmAPI.uploadBootstrapPackage(file, currentTeamId); |
| 46 | + renderFlash("success", "Successfully uploaded!"); |
| 47 | + onUpload(); |
| 48 | + } catch (e) { |
| 49 | + const error = e as AxiosResponse<IApiError>; |
| 50 | + const errMessage = getErrorMessage(error); |
| 51 | + renderFlash("error", errMessage); |
| 52 | + } finally { |
| 53 | + setShowLoading(false); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className={baseClass}> |
| 59 | + <p> |
| 60 | + Add an automatic enrollment profile to customize the macOS Setup |
| 61 | + Assistant. |
| 62 | + <CustomLink |
| 63 | + url=" https://fleetdm.com/learn-more-about/setup-assistant" |
| 64 | + text="Learn how" |
| 65 | + newTab |
| 66 | + /> |
| 67 | + </p> |
| 68 | + <FileUploader |
| 69 | + message="Automatic enrollment profile (.json)" |
| 70 | + graphicName="file-configuration-profile" |
| 71 | + accept=".json" |
| 72 | + buttonMessage="Add profile" |
| 73 | + onFileUpload={onUploadFile} |
| 74 | + isLoading={showLoading} |
| 75 | + /> |
| 76 | + </div> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +export default SetupAssistantPackageUploader; |
0 commit comments