diff --git a/web/src/components/storage/DevicesManager.js b/web/src/components/storage/DevicesManager.js index 9848ce5bfd..2f836fe86c 100644 --- a/web/src/components/storage/DevicesManager.js +++ b/web/src/components/storage/DevicesManager.js @@ -145,14 +145,55 @@ export default class DevicesManager { return compact(uniq(sids).map(sid => this.stagingDevice(sid))); } + /** + * Devices deleted. + * + * @note The devices are extracted from the actions. + * + * @returns {StorageDevice[]} + */ + deletedDevices() { + const sids = this.actions + .filter(a => a.delete) + .map(a => a.device); + const devices = sids.map(sid => this.systemDevice(sid)); + return compact(devices); + } + + /** + * Systems deleted. + * + * @returns {string[]} + */ + deletedSystems() { + const systems = this.deletedDevices() + .map(d => d.systems) + .flat(); + return compact(systems); + } + + /** + * @param {number} sid + * @param {StorageDevice[]} source + * @returns {StorageDevice|undefined} + */ #device(sid, source) { return source.find(d => d.sid === sid); } + /** + * @param {StorageDevice} device + * @param {StorageDevice[]} source + * @returns {boolean} + */ #exist(device, source) { return this.#device(device.sid, source) !== undefined; } + /** + * @param {StorageDevice} device + * @returns {boolean} + */ #isUsed(device) { const sids = uniq(compact(this.actions.map(a => a.device))); diff --git a/web/src/components/storage/DevicesManager.test.js b/web/src/components/storage/DevicesManager.test.js index a14f6caaca..53ff712927 100644 --- a/web/src/components/storage/DevicesManager.test.js +++ b/web/src/components/storage/DevicesManager.test.js @@ -367,3 +367,54 @@ describe("usedDevices", () => { }); }); }); + +describe("deletedDevices", () => { + beforeEach(() => { + system = [ + { sid: 60 }, + { sid: 62 }, + { sid: 63 }, + { sid: 64 } + ]; + actions = [ + { device: 60, delete: true }, + // This device does not exist in system. + { device: 61, delete: true }, + { device: 62, delete: false }, + { device: 63, delete: true } + ]; + }); + + it("includes all deleted devices", () => { + const manager = new DevicesManager(system, staging, actions); + const sids = manager.deletedDevices().map(d => d.sid) + .sort(); + expect(sids).toEqual([60, 63]); + }); +}); + +describe("deletedSystems", () => { + beforeEach(() => { + system = [ + { sid: 60, systems: ["Windows XP"] }, + { sid: 62, systems: ["Ubuntu"] }, + { sid: 63, systems: ["openSUSE Leap"] }, + { sid: 64 } + ]; + actions = [ + { device: 60, delete: true }, + // This device does not exist in system. + { device: 61, delete: true }, + { device: 62, delete: false }, + { device: 63, delete: true } + ]; + }); + + it("includes all deleted systems", () => { + const manager = new DevicesManager(system, staging, actions); + const systems = manager.deletedSystems(); + expect(systems.length).toEqual(2); + expect(systems).toContain("Windows XP"); + expect(systems).toContain("openSUSE Leap"); + }); +});