Skip to content

Commit

Permalink
WIP HMS-2151 feat: connect actions to backend
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Visiedo <[email protected]>
  • Loading branch information
avisiedo committed Oct 10, 2023
1 parent dec9c5a commit 425024a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { VerifyState } from './Routes/WizardPage/Components/VerifyRegistry/Verif
*/
export interface IAppContext {
/** Represent the current list of domains to be displayed in the listDomains view. */
domains: Domain[];
getDomains: () => Domain[];
/** Callback to set the value of `domains`. */
setDomains: (domains: Domain[]) => void;
/** Encapsulates the context related with the wizard. */
Expand All @@ -35,7 +35,9 @@ export interface IAppContext {
* @public
*/
export const AppContext = createContext<IAppContext>({
domains: [],
getDomains: (): Domain[] => {
return [];
},
setDomains: (domains: Domain[]) => {
throw new Error('Function "setDomains" not implemented: domains=' + domains);
},
Expand Down
12 changes: 7 additions & 5 deletions src/AppEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import { Domain } from './Api';
import { VerifyState } from './Routes/WizardPage/Components/VerifyRegistry/VerifyRegistry';

const AppEntry = () => {
const appContext = useContext(AppContext);
const [domains, setDomains] = useState<Domain[]>([]);
const [wizardToken, setWizardToken] = useState<string>('');
const [wizardDomain, setWizardDomain] = useState<Domain>({} as Domain);
const [wizardRegisterStatus, setWizardRegisterStatus] = useState<VerifyState>('initial');
const cbSetDomains = (domains: Domain[]) => {
appContext.domains = domains;
setDomains(domains);

const cbGetDomains = (): Domain[] => {
return domains;
};
const cbSetDomains = (value: Domain[]) => {
setDomains(value);
};
const cbGetWizardToken = (): string => {
return wizardToken;
Expand All @@ -42,7 +44,7 @@ const AppEntry = () => {
<Router basename={getBaseName(window.location.pathname)}>
<AppContext.Provider
value={{
domains: domains,
getDomains: cbGetDomains,
setDomains: cbSetDomains,
wizard: {
getToken: cbGetWizardToken,
Expand Down
54 changes: 43 additions & 11 deletions src/Components/DomainList/DomainList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './DomainList.scss';
import { Fragment, useContext, useState } from 'react';
import React from 'react';

import { Domain, DomainType } from '../../Api/api';
import { Domain, DomainType, ResourcesApiFactory, UpdateDomainUserRequest } from '../../Api/api';
import { Link } from 'react-router-dom';
import { AppContext, IAppContext } from '../../AppContext';

Expand Down Expand Up @@ -105,7 +105,10 @@ const DomainListFieldStatus = (props: DomainListFieldStatusProps) => {
}
};

export const DomainList = () => {
export const DomainList: React.FC = () => {
const base_url = '/api/idmsvc/v1';
const resources_api = ResourcesApiFactory(undefined, base_url, undefined);

const context = useContext<IAppContext>(AppContext);

// Index of the currently sorted column
Expand All @@ -116,7 +119,7 @@ export const DomainList = () => {
// Sort direction of the currently sorted column
const [activeSortDirection, setActiveSortDirection] = React.useState<'asc' | 'desc'>('asc');

const [domains] = useState<Domain[]>(context.domains);
const [domains] = useState<Domain[]>(context.getDomains());
const enabledText = 'Enabled';
const disabledText = 'Disabled';

Expand All @@ -136,7 +139,35 @@ export const DomainList = () => {
const defaultActions = (domain: Domain): IAction[] => [
{
title: 'Enable/Disable',
onClick: () => console.log(`clicked on Enable/Disable, on row ${domain.title}`),
onClick: () => {
const newValue = domain.auto_enrollment_enabled && domain.auto_enrollment_enabled === true ? false : true;
domain.domain_id &&
resources_api
.updateDomainUser(domain.domain_id, { auto_enrollment_enabled: newValue } as UpdateDomainUserRequest)
.then((response) => {
if (response.status >= 200 && response.status < 400) {
let domains: Domain[] = context.getDomains();

Check failure on line 149 in src/Components/DomainList/DomainList.tsx

View workflow job for this annotation

GitHub Actions / validate

'domains' is never reassigned. Use 'const' instead
const newDomain: Domain = { ...domain, auto_enrollment_enabled: newValue } as Domain;
const rowIndex = domains.findIndex((value: Domain, index: number, obj: Domain[]) => {

Check warning on line 151 in src/Components/DomainList/DomainList.tsx

View workflow job for this annotation

GitHub Actions / validate

'index' is defined but never used

Check warning on line 151 in src/Components/DomainList/DomainList.tsx

View workflow job for this annotation

GitHub Actions / validate

'obj' is defined but never used
if (value.domain_id && value.domain_id === domain.domain_id) {
return true;
}
return false;
});
if (rowIndex < 0) {
throw new Error('domain "' + domain.domain_id + '" not found');
}
domains[rowIndex] = newDomain;
context.setDomains(domains);
} else {
throw new Error('domain "' + domain.domain_name + "' not updated");
}
})
.catch((error) => {
throw error;
});
console.log(`clicked on Disable, on row ${domain.title}`);
},
},
{
title: 'Edit',
Expand Down Expand Up @@ -167,24 +198,25 @@ export const DomainList = () => {
<Tbody>
{domains.map((domain) => {
const rowActions: IAction[] | null = defaultActions(domain);
if (domain.auto_enrollment_enabled === true) {
rowActions[0].title = 'Disable';
} else {
rowActions[0].title = 'Enable';
}
/** Given the current state, the action is the opposite to the current state */
rowActions[0].title = domain.auto_enrollment_enabled === true ? disabledText : enabledText;
/** Given the current state, set the column content */
const auto_enrollment_text = domain.auto_enrollment_enabled ? enabledText : disabledText;
/** Link to access the deail view for a specific domain */
const link_to_details = '/domains/{domain.domain_id}';
return (
<>
<Tr key={domain.domain_id}>
<Td>
<Link to="/domains/{domain.domain_id}">{domain.title}</Link>
<Link to={link_to_details}>{domain.title}</Link>
</Td>
<Td>
<DomainListFieldType domain_type={domain.domain_type} />
</Td>
<Td>
<DomainListFieldStatus domain={domain} />
</Td>
<Td>{domain.auto_enrollment_enabled ? enabledText : disabledText}</Td>
<Td>{auto_enrollment_text}</Td>
<Td isActionCell>
<ActionsColumn items={rowActions} />
</Td>
Expand Down

0 comments on commit 425024a

Please sign in to comment.