Skip to content

Commit

Permalink
HMS-2597 implement enable/disable in list view
Browse files Browse the repository at this point in the history
Implement the enable/disable in the list view.  This implements the
backend interaction and UI state update, but does NOT yet implement
the "This will do X.  Are you sure?" interstitial dialogs.  That
will be dealt with later (perhaps deferred until after the e2e demo
milestone).
  • Loading branch information
frasertweedale committed Oct 17, 2023
1 parent acd8c77 commit 3a43655
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 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 } from '../../Api/api';
import { Link } from 'react-router-dom';
import { AppContext, IAppContext } from '../../AppContext';

Expand Down Expand Up @@ -91,6 +91,9 @@ const DomainListFieldType = (props: DomainListFieldTypeProps) => {
};

export const DomainList = () => {
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 @@ -101,7 +104,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, setDomains] = useState<Domain[]>(context.domains);
const enabledText = 'Enabled';
const disabledText = 'Disabled';

Expand All @@ -118,10 +121,40 @@ 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);
};

const onEnableDisable = (domain: Domain) => {
console.log(`clicked on Enable/Disable, on row ${domain.title}`);
if (domain.domain_id) {
resources_api
.updateDomainUser(domain.domain_id, {
auto_enrollment_enabled: !domain.auto_enrollment_enabled,
})
.then((response) => {
if (response.status == 200) {
replaceDomain(response.data);
} else {
// TODO show-up notification with error message
}
})
.catch((error) => {
// TODO show-up notification with error message
console.log('error onClose: ' + error);
});
}
};

const defaultActions = (domain: Domain): IAction[] => [
{
title: 'Enable/Disable',
onClick: () => console.log(`clicked on Enable/Disable, on row ${domain.title}`),
onClick: () => onEnableDisable(domain),
},
{
title: 'Edit',
Expand Down

0 comments on commit 3a43655

Please sign in to comment.