Skip to content

Commit

Permalink
fix(HMS-3784): fix registered domains table refresh
Browse files Browse the repository at this point in the history
When a new domain was registered, the table was not being
refreshed properly, requiring a manual refresh.

This change fix some situation with the application
context that was evoking this behavior.

Co-authored-by: Petr Vobornik <[email protected]>
Signed-off-by: Alejandro Visiedo <[email protected]>
  • Loading branch information
avisiedo and pvoborni committed Apr 25, 2024
1 parent 1220cdb commit 9f8c8b7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const AppContextProvider: React.FC<AppContextProviderProps> = ({ children
* @param domain The domain to be updated into the context.
*/
const _updateDomain = (domain: Domain) => {
const newDomains: Domain[] = {} as Domain[];
const newDomains: Domain[] = [];
for (const idx in domains) {
if (domains[idx].domain_id === domain.domain_id) {
newDomains[idx] = domain;
Expand All @@ -94,7 +94,7 @@ export const AppContextProvider: React.FC<AppContextProviderProps> = ({ children
* @param id the domain identifier.
*/
const _deleteDomain = (id: string) => {
const newDomains: Domain[] = {} as Domain[];
const newDomains: Domain[] = [];
for (const idx in domains) {
if (domains[idx].domain_id !== id) {
newDomains[idx] = domains[idx];
Expand Down
23 changes: 4 additions & 19 deletions src/Components/DomainList/DomainList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActionsColumn, IAction, TableComposable, Tbody, Td, Th, ThProps, Thead, Tr } from '@patternfly/react-table';
import './DomainList.scss';
import { Fragment, useContext, useState } from 'react';
import { Fragment, useContext } from 'react';
import React from 'react';

import { Domain, DomainType, ResourcesApiFactory } from '../../Api/api';
Expand Down Expand Up @@ -106,7 +106,7 @@ export const DomainList = () => {
// Sort direction of the currently sorted column
const [activeSortDirection, setActiveSortDirection] = React.useState<'asc' | 'desc'>('asc');

const [domains, setDomains] = useState<Domain[]>(context?.domains || ([] as Domain[]));
const domains = context?.domains || ([] as Domain[]);
const enabledText = 'Enabled';
const disabledText = 'Disabled';

Expand All @@ -123,24 +123,9 @@ export const DomainList = () => {
columnIndex,
});

// given a domain object, replace it in the `domains` state
// (matching by uuid).
const replaceDomain = (newDomain: Domain): void => {
const newDomains = domains.map((domain) => {
return domain.domain_id === newDomain.domain_id ? newDomain : domain;
});
setDomains(newDomains);
};

// remove domain(s) matching the given uuid from the `domains` state
const removeDomain = (uuid: string): void => {
const newDomains: Domain[] = [];
for (const domain of domains) {
if (domain.domain_id !== uuid) {
newDomains.push(domain);
}
}
setDomains(newDomains);
context?.deleteDomain(uuid);
};

const onEnableDisable = (domain: Domain) => {
Expand All @@ -152,7 +137,7 @@ export const DomainList = () => {
})
.then((response) => {
if (response.status == 200) {
replaceDomain(response.data);
context?.updateDomain(response.data);
} else {
// TODO show-up notification with error message
}
Expand Down
1 change: 1 addition & 0 deletions src/Routes/DefaultPage/DefaultPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const ListContent = () => {
.readDomain(item.domain_id)
.then((res_domain) => {
local_domains[count++] = res_domain.data;
// This is executed when the last item was read
if (res.data.data.length == local_domains.length) {
appContext?.setDomains(local_domains);
const newOffset = Math.floor((offset + perPage - 1) / perPage) * perPage;
Expand Down

0 comments on commit 9f8c8b7

Please sign in to comment.