2022-04-19 18:43:36 +00:00
|
|
|
import { useMutation, useQueryClient } from 'react-query';
|
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-04-19 18:43:36 +00:00
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { promiseSequence } from '@/portainer/helpers/promise-utils';
|
2022-11-22 12:16:34 +00:00
|
|
|
import { useIntegratedLicenseInfo } from '@/portainer/license-management/use-license.service';
|
2022-04-19 18:43:36 +00:00
|
|
|
|
|
|
|
export function useAssociateDeviceMutation() {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation(
|
|
|
|
(ids: EnvironmentId[]) =>
|
|
|
|
promiseSequence(ids.map((id) => () => associateDevice(id))),
|
|
|
|
{
|
|
|
|
onSuccess: () => {
|
|
|
|
queryClient.invalidateQueries(['environments']);
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
error: {
|
|
|
|
title: 'Failure',
|
|
|
|
message: 'Failed to associate devices',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function associateDevice(environmentId: EnvironmentId) {
|
|
|
|
try {
|
|
|
|
await axios.post(`/endpoints/${environmentId}/edge/trust`);
|
|
|
|
} catch (e) {
|
|
|
|
throw parseAxiosError(e as Error, 'Failed to associate device');
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 12:16:34 +00:00
|
|
|
|
|
|
|
export function useLicenseOverused() {
|
|
|
|
const integratedInfo = useIntegratedLicenseInfo();
|
|
|
|
if (integratedInfo && integratedInfo.licenseInfo.enforcedAt > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|