Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Delete device button into DeviceInfoCard #399

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add Delete device button in Device Info Card ([#397](https://github.com/astarte-platform/astarte-dashboard/issues/397)).

## [1.1.0] - 2023-06-20

Expand Down
5 changes: 5 additions & 0 deletions src/DeviceStatusPage/DeviceInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ interface DeviceInfoCardProps {
onInhibitCredentialsClick: () => void;
onEnableCredentialsClick: () => void;
onWipeCredentialsClick: () => void;
onDeleteDeviceClick: () => void;
}

const DeviceInfoCard = ({
device,
onInhibitCredentialsClick,
onEnableCredentialsClick,
onWipeCredentialsClick,
onDeleteDeviceClick,
}: DeviceInfoCardProps): React.ReactElement => (
<FullHeightCard xs={12} md={6} className="mb-4">
<Card.Header as="h5">Device Info</Card.Header>
Expand Down Expand Up @@ -96,6 +98,9 @@ const DeviceInfoCard = ({
<Button variant="danger" onClick={onWipeCredentialsClick}>
Wipe credential secret
</Button>
<Button variant="danger" className="ml-1" onClick={onDeleteDeviceClick}>
Delete device
</Button>
</div>
</Card.Body>
</FullHeightCard>
Expand Down
71 changes: 68 additions & 3 deletions src/DeviceStatusPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/

import React, { useCallback, useMemo, useState } from 'react';
import { Col, Container, Row, Spinner } from 'react-bootstrap';
import { Link, useParams } from 'react-router-dom';
import { Col, Container, Form, Row, Spinner } from 'react-bootstrap';
import { Link, useNavigate, useParams } from 'react-router-dom';

import type { AstarteDevice } from 'astarte-client';
import BackButton from '../ui/BackButton';
Expand Down Expand Up @@ -95,6 +95,11 @@
kind: 'reregister_device_modal';
};

type DeleteDeviceModalT = {
kind: 'delete_device_modal';
isDeletingDevice: boolean;
};

function isWipeCredentialsModal(modal: PageModal): modal is WipeCredentialsModalT {
return modal.kind === 'wipe_credentials_modal';
}
Expand Down Expand Up @@ -131,6 +136,10 @@
return modal.kind === 'reregister_device_modal';
}

function isDeleteDeviceModal(modal: PageModal): modal is DeleteDeviceModalT {
return modal.kind === 'delete_device_modal';
}

type PageModal =
| WipeCredentialsModalT
| AddToGroupModalT
Expand All @@ -140,15 +149,19 @@
| NewAttributeModalT
| EditAttributeModalT
| DeleteAttributeModalT
| ReregisterDeviceModalT;
| ReregisterDeviceModalT
| DeleteDeviceModalT;

export default (): React.ReactElement => {
const { deviceId = '' } = useParams();
const astarte = useAstarte();
const navigate = useNavigate();
const deviceFetcher = useFetch(() => astarte.client.getDeviceInfo(deviceId));
const groupsFetcher = useFetch(() => astarte.client.getGroupList());
const [devicePageAlers, devicePageAlersController] = useAlerts();
const [activeModal, setActiveModal] = useState<PageModal | null>(null);
const [confirmString, setConfirmString] = useState('');
const canDelete = confirmString === deviceId;

const unjoinedGroups = useMemo(() => {
if (deviceFetcher.status === 'ok' && groupsFetcher.status === 'ok') {
Expand All @@ -156,7 +169,7 @@
return groupsFetcher.value.filter((groupName) => !joinedGroups.has(groupName));
}
return [];
}, [deviceFetcher.status, groupsFetcher.status]);

Check warning on line 172 in src/DeviceStatusPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run code quality and funcionality tests

React Hook useMemo has missing dependencies: 'deviceFetcher.value.groups' and 'groupsFetcher.value'. Either include them or remove the dependency array

const dismissModal = useCallback(() => {
setActiveModal(null);
Expand All @@ -175,7 +188,7 @@
);
});
},
[astarte.client, deviceId, devicePageAlersController],

Check warning on line 191 in src/DeviceStatusPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run code quality and funcionality tests

React Hook useCallback has a missing dependency: 'deviceFetcher'. Either include it or remove the dependency array
);

const wipeDeviceCredentials = useCallback(() => {
Expand All @@ -190,6 +203,19 @@
});
}, [astarte.client, deviceId, dismissModal, devicePageAlersController]);

const handleDeleteDevice = useCallback(() => {
astarte.client
.deleteDevice(deviceId)
.then(() => {
setActiveModal({ kind: 'delete_device_modal', isDeletingDevice: true });
navigate('/devices');
})
.catch(() => {
devicePageAlersController.showError(`Couldn't delete the device`);
dismissModal();
});
}, [astarte.client, deviceId, dismissModal, devicePageAlersController, navigate]);

const addDeviceToGroup = useCallback(
(groupName) => {
astarte.client
Expand All @@ -206,7 +232,7 @@
dismissModal();
});
},
[astarte.client, deviceId, devicePageAlersController, dismissModal],

Check warning on line 235 in src/DeviceStatusPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run code quality and funcionality tests

React Hook useCallback has a missing dependency: 'deviceFetcher'. Either include it or remove the dependency array
);

const handleAliasUpdate = useCallback(
Expand All @@ -222,7 +248,7 @@
dismissModal();
});
},
[astarte.client, deviceId, devicePageAlersController, dismissModal],

Check warning on line 251 in src/DeviceStatusPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run code quality and funcionality tests

React Hook useCallback has a missing dependency: 'deviceFetcher'. Either include it or remove the dependency array
);

const handleAliasDeletion = useCallback(
Expand All @@ -238,7 +264,7 @@
dismissModal();
});
},
[astarte.client, deviceId, devicePageAlersController, dismissModal],

Check warning on line 267 in src/DeviceStatusPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run code quality and funcionality tests

React Hook useCallback has a missing dependency: 'deviceFetcher'. Either include it or remove the dependency array
);

const handleAttributeUpdate = useCallback(
Expand All @@ -254,7 +280,7 @@
dismissModal();
});
},
[astarte.client, deviceId, devicePageAlersController, dismissModal],

Check warning on line 283 in src/DeviceStatusPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run code quality and funcionality tests

React Hook useCallback has a missing dependency: 'deviceFetcher'. Either include it or remove the dependency array
);

const handleAttributeDeletion = useCallback(
Expand All @@ -270,7 +296,7 @@
dismissModal();
});
},
[astarte.client, deviceId, devicePageAlersController, dismissModal],

Check warning on line 299 in src/DeviceStatusPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run code quality and funcionality tests

React Hook useCallback has a missing dependency: 'deviceFetcher'. Either include it or remove the dependency array
);

return (
Expand Down Expand Up @@ -313,6 +339,12 @@
isWipingCredentials: false,
})
}
onDeleteDeviceClick={() =>
setActiveModal({
kind: 'delete_device_modal',
isDeletingDevice: false,
})
}
/>
<AliasesCard
device={device}
Expand Down Expand Up @@ -401,6 +433,39 @@
</p>
</ConfirmModal>
)}
{activeModal && isDeleteDeviceModal(activeModal) && (
<ConfirmModal
title="Delete device"
confirmLabel="Delete device"
confirmVariant="danger"
onCancel={dismissModal}
onConfirm={() => {
setActiveModal({ ...activeModal, isDeletingDevice: true });
handleDeleteDevice();
}}
isConfirming={activeModal.isDeletingDevice}
disabled={!canDelete}
>
<p>
You are going to permanently delete&nbsp;
<b>{deviceId}</b> and the corresponding data. Deleted devices cannot be restored but a
new one can be registered instead.
</p>
<p>
Please type <b>{deviceId}</b> to proceed.
</p>
<Form.Group controlId="deleteDevice">
<Form.Control
type="text"
placeholder="Device Id"
value={confirmString}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setConfirmString(e.target.value)
}
/>
</Form.Group>
</ConfirmModal>
)}
{activeModal && isAddToGroupModal(activeModal) && (
<AddToGroupModal
onCancel={dismissModal}
Expand Down
12 changes: 10 additions & 2 deletions src/astarte-client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,20 @@ class AstarteClient {
interfaces: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/interfaces`,
interfaceMajors: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/interfaces/${'interfaceName'}`,
interface: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/interfaces/${'interfaceName'}/${'interfaceMajor'}`,
interfaceData: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/interfaces/${'interfaceName'}/${'interfaceMajor'}`,
interfaceData:
astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/interfaces/${'interfaceName'}/${'interfaceMajor'}`,
trigger: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/triggers/${'triggerName'}`,
triggers: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/triggers`,
policies: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/policies`,
policy: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/policies/${'policyName'}`,
device: astarteAPIurl`${config.realmManagementApiUrl}v1/${'realm'}/devices/${'deviceId'}`,
appengineHealth: astarteAPIurl`${config.appEngineApiUrl}health`,
devicesStats: astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/stats/devices`,
devices: astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/devices`,
deviceInfo: astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/devices/${'deviceId'}`,
deviceData: astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/devices/${'deviceId'}/interfaces/${'interfaceName'}${'path'}?keep_milliseconds=true&since=${'since'}&since_after=${'sinceAfter'}&to=${'to'}&limit=${'limit'}`,
deviceData:
astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/devices/${'deviceId'}/interfaces/${'interfaceName'}${'path'}?
keep_milliseconds=true&since=${'since'}&since_after=${'sinceAfter'}&to=${'to'}&limit=${'limit'}`,
groups: astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/groups`,
groupDevices: astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/groups/${'groupName'}/devices`,
deviceInGroup: astarteAPIurl`${config.appEngineApiUrl}v1/${'realm'}/groups/${'groupName'}/devices/${'deviceId'}`,
Expand Down Expand Up @@ -613,6 +617,10 @@ class AstarteClient {
await this.$delete(this.apiConfig.deviceAgent({ deviceId, ...this.config }));
}

async deleteDevice(deviceId: AstarteDevice['id']): Promise<void> {
await this.$delete(this.apiConfig.device({ ...this.config, deviceId }));
}

async getFlowInstances(): Promise<Array<AstarteFlow['name']>> {
const response = await this.$get(this.apiConfig.flows(this.config));
return response.data;
Expand Down
Loading