Skip to content

Commit

Permalink
[web] Move issues management to client
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Oct 26, 2023
1 parent 847d353 commit 8302b9b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 177 deletions.
50 changes: 47 additions & 3 deletions web/src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { UsersClient } from "./users";
import phase from "./phase";
import { QuestionsClient } from "./questions";
import { NetworkClient } from "./network";
import { IssuesClient } from "./issues";
import cockpit from "../lib/cockpit";

const BUS_ADDRESS_FILE = "/run/agama/bus.address";
Expand All @@ -46,13 +45,26 @@ const MANAGER_SERVICE = "org.opensuse.Agama.Manager1";
* @property {StorageClient} storage - storage client
* @property {UsersClient} users - users client
* @property {QuestionsClient} questions - questions client
* @property {IssuesClient} issues - issues client
* @property {() => Promise<Issues>} issues - issues from all contexts
* @property {(handler: IssuesHandler) => (() => void)} onIssuesChange - registers a handler to run
* when issues from any context change. It returns a function to deregister the handler.
* @property {() => Promise<boolean>} isConnected - determines whether the client is connected
* @property {(handler: () => void) => (() => void)} onDisconnect - registers a handler to run
* when the connection is lost. It returns a function to deregister the
* handler.
*/

/**
* @typedef {import ("~/client/mixins").Issue} Issue
*
* @typedef {object} Issues
* @property {Issue[]} [product] - Issues from product.
* @property {Issue[]} [storage] - Issues from storage.
* @property {Issue[]} [software] - Issues from software.
*
* @typedef {(issues: Issues) => void} IssuesHandler
*/

/**
* Creates the Agama client
*
Expand All @@ -67,7 +79,38 @@ const createClient = (address = "unix:path=/run/agama/bus") => {
const storage = new StorageClient(address);
const users = new UsersClient(address);
const questions = new QuestionsClient(address);
const issues = new IssuesClient({ storage });

/**
* Gets all issues, grouping them by context.
*
* TODO: issues are requested by several components (e.g., overview sections, notifications
* provider, issues page, storage page, etc). There should be an issues provider.
*
* @returns {Promise<Issues>}
*/
const issues = async () => {
return {
product: await software.product.getIssues(),
storage: await storage.getIssues(),
software: await software.getIssues()
};
};

/**
* Registers a callback to be executed when issues change.
*
* @param {IssuesHandler} handler - Callback function.
* @return {() => void} - Function to deregister the callback.
*/
const onIssuesChange = (handler) => {
const unsubscribeCallbacks = [];

unsubscribeCallbacks.push(software.product.onIssuesChange(i => handler({ product: i })));
unsubscribeCallbacks.push(storage.onIssuesChange(i => handler({ storage: i })));
unsubscribeCallbacks.push(software.onIssuesChange(i => handler({ software: i })));

return () => { unsubscribeCallbacks.forEach(cb => cb()) };
};

const isConnected = async () => {
try {
Expand All @@ -88,6 +131,7 @@ const createClient = (address = "unix:path=/run/agama/bus") => {
users,
questions,
issues,
onIssuesChange,
isConnected,
onDisconnect: (handler) => monitor.onDisconnect(handler)
};
Expand Down
78 changes: 0 additions & 78 deletions web/src/client/issues.js

This file was deleted.

94 changes: 0 additions & 94 deletions web/src/client/issues.test.js

This file was deleted.

5 changes: 3 additions & 2 deletions web/src/context/notification.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ function NotificationProvider({ children }) {
const load = useCallback(async () => {
if (!client) return;

const hasIssues = await cancellablePromise(client.issues.any());
const issues = await cancellablePromise(client.issues());
const hasIssues = Object.values(issues).flat().length > 0;
update({ issues: hasIssues });
}, [client, cancellablePromise, update]);

useEffect(() => {
if (!client) return;

load();
return client.issues.onIssuesChange(load);
return client.onIssuesChange(load);
}, [client, load]);

const value = [state, update];
Expand Down

0 comments on commit 8302b9b

Please sign in to comment.