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 13, 2023
1 parent 7c1aa29 commit 5fb9ff1
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 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 @@ -106,6 +106,9 @@ const DomainListFieldStatus = (props: DomainListFieldStatusProps) => {
};

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

Expand All @@ -133,10 +136,42 @@ 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

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

View workflow job for this annotation

GitHub Actions / validate

Replace `⏎········?·newDomain⏎·······` with `·?·newDomain`
? 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 5fb9ff1

Please sign in to comment.