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 f2c19b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion 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 Down
31 changes: 27 additions & 4 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,27 @@ 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 ? true : false;

domain.domain_id &&
resources_api.updateDomainUser(domain.domain_id, { auto_enrollment_enabled: newValue } as UpdateDomainUserRequest).then((response) => {
if (response.status >= 200 && response.status < 400) {
const domains: Domain[] = context.getDomains();
const newDomain: Domain = {
...domains.find((value: Domain, index: number) => {
if (value.domain_id === domain.domain_id) {
domains.splice(index, 1);
return value;
}
}),
auto_enrollment_enabled: newValue,
} as Domain;
context.setDomains([...domains, newDomain]);
}
});
console.log(`clicked on Disable, on row ${domain.title}`);
},
},
{
title: 'Edit',
Expand Down

0 comments on commit f2c19b0

Please sign in to comment.