fleet/frontend/pages/admin/IntegrationsPage/cards/Vpp/Vpp.tsx
Gabriel Hernandez 845b524dcc
add/remove/disable vpp token in Fleet UI (#20127)
relates to #19866

> NOTE: API integration work still needs to be done, which will happen
in another PR.

This adds the ability to add, remove, or disable a VPP token in the
Fleet UI. This includes:

**Vpp integration page with VPP card:**


![image](https://github.com/fleetdm/fleet/assets/1153709/99b1ca9b-8872-447f-a085-b5385a2b7f7e)


![image](https://github.com/fleetdm/fleet/assets/1153709/1cdb80a2-1afe-4739-994c-fe7430449f13)


![image](https://github.com/fleetdm/fleet/assets/1153709/79ec7927-f905-48c4-b1b9-42d4d6b41028)

**VPP setup page with steps to set up VPP:**


![image](https://github.com/fleetdm/fleet/assets/1153709/dec203e4-01d3-4e1d-b493-be3772b72813)

**VPP setup page with VPP info:**


![image](https://github.com/fleetdm/fleet/assets/1153709/afccba29-e97b-4937-8235-4706e39d9333)

**Disable VPP modal:**


![image](https://github.com/fleetdm/fleet/assets/1153709/da4a2db3-7546-4f3b-8ec0-d77ad7bff19f)

**renew Vpp modal:**


![image](https://github.com/fleetdm/fleet/assets/1153709/8224f466-6aae-43bd-a120-3de5f0c90064)

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Manual QA for all new/changed functionality
2024-07-10 17:05:09 +01:00

135 lines
3.3 KiB
TypeScript

import React, { useContext } from "react";
import { InjectedRouter } from "react-router";
import { useQuery } from "react-query";
import { AxiosError } from "axios";
import PATHS from "router/paths";
import { AppContext } from "context/app";
import mdmAppleAPI, { IGetVppInfoResponse } from "services/entities/mdm_apple";
import { DEFAULT_USE_QUERY_OPTIONS } from "utilities/constants";
import Card from "components/Card";
import SectionHeader from "components/SectionHeader";
import Button from "components/buttons/Button";
import Icon from "components/Icon";
import Spinner from "components/Spinner";
import DataError from "components/DataError";
const baseClass = "vpp";
interface IVppCardProps {
isAppleMdmOn: boolean;
isVppOn: boolean;
router: InjectedRouter;
}
const VppCard = ({ isAppleMdmOn, isVppOn, router }: IVppCardProps) => {
const nagivateToMdm = () => {
router.push(PATHS.ADMIN_INTEGRATIONS_MDM);
};
const navigateToVppSetup = () => {
router.push(PATHS.ADMIN_INTEGRATIONS_VPP_SETUP);
};
const appleMdmDiabledContent = (
<div className={`${baseClass}__mdm-disabled-content`}>
<div>
<h3>Volume Purchasing Program (VPP)</h3>
<p>
To enable Volume Purchasing Program (VPP) for macOS devices, first
turn on macOS MDM.
</p>
</div>
<Button onClick={nagivateToMdm} variant="text-link">
Turn on macOS MDM
</Button>
</div>
);
const isOnContent = (
<div className={`${baseClass}__vpp-on-content`}>
<p>
<span>
<Icon name="success" />
Volume Purchasing Program (VPP) enabled.
</span>
</p>
<Button onClick={navigateToVppSetup} variant="text-icon">
<Icon name="pencil" />
Edit
</Button>
</div>
);
const isOffContent = (
<div className={`${baseClass}__vpp-off-content`}>
<div>
<h3>Volume Purchasing Program (VPP)</h3>
<p>
Install apps from Apple&apos;s App Store purchased through Apple
Business Manager.
</p>
</div>
<Button onClick={navigateToVppSetup} variant="brand">
Enable
</Button>
</div>
);
const renderCardContent = () => {
if (!isAppleMdmOn) {
return appleMdmDiabledContent;
}
return isVppOn ? isOnContent : isOffContent;
};
return (
<Card className={`${baseClass}__card`} color="gray">
{renderCardContent()}
</Card>
);
};
interface IVppProps {
router: InjectedRouter;
}
const Vpp = ({ router }: IVppProps) => {
const { config } = useContext(AppContext);
const { data: vppData, error: vppError, isLoading, isError } = useQuery<
IGetVppInfoResponse,
AxiosError
>("vppInfo", () => mdmAppleAPI.getVppInfo(), {
...DEFAULT_USE_QUERY_OPTIONS,
retry: false,
});
const renderContent = () => {
if (isLoading) {
return <Spinner />;
}
if (isError && vppError?.status !== 404) {
return <DataError />;
}
return (
<VppCard
isAppleMdmOn={!!config?.mdm.enabled_and_configured}
isVppOn={!!vppData && vppError?.status !== 404}
router={router}
/>
);
};
return (
<div className={baseClass}>
<SectionHeader title="Volume Purchasing Program (VPP)" />
<>{renderContent()}</>
</div>
);
};
export default Vpp;