Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored blocking content #275

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions JeMPI_Apps/JeMPI_UI/src/pages/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CustomTabPanel, a11yProps } from './deterministic/BasicTabs'
import { Configuration } from 'types/Configuration'
import { generateId } from 'utils/helpers'
import Probabilistic from './probabilistic/Probabilistic'
import { useConfig } from 'hooks/useConfig';
import { useConfig } from 'hooks/useConfig'
import { useSnackbar } from 'notistack'

const Settings = () => {
Expand All @@ -24,19 +24,19 @@ const Settings = () => {
? generateId(JSON.parse(storedData))
: ({} as Configuration)
})
const [isSaving, setIsSaving] = useState<boolean>(false);
const [isSaving, setIsSaving] = useState<boolean>(false)

const { apiClient } = useConfig();
const { apiClient } = useConfig()

const handleChange = (event: SyntheticEvent, newValue: number) => {
setValue(newValue);
};
setValue(newValue)
}
const { enqueueSnackbar } = useSnackbar()

const handleSave = async () => {
setIsSaving(true);
const response = await apiClient.saveConfiguration();
setIsSaving(false);
setIsSaving(true)
const response = await apiClient.saveConfiguration()
setIsSaving(false)
if (response && response.response === 'ok') {
enqueueSnackbar(`Successfully saved configuration`, {
variant: 'success'
Expand All @@ -46,9 +46,9 @@ const Settings = () => {
enqueueSnackbar(`Error saving configuration`, {
variant: 'error'
})
console.log('handleSave error', response.data);
console.log('handleSave error', response.data)
}
};
}

useEffect(() => {
const handleStorageChange = (event: StorageEvent) => {
Expand Down Expand Up @@ -124,28 +124,25 @@ const Settings = () => {
<Typography variant="h5" sx={{ py: 3 }}>
Setup properties that are unique to the golden record
</Typography>
<UniqueToGR/>
<UniqueToGR />
</CustomTabPanel>
<CustomTabPanel value={value} index={2}>
<Typography variant="h5" sx={{ py: 3 }}>
Setup properties that are unique to the interaction
</Typography>
<UniqueToInteraction/>
<UniqueToInteraction />
</CustomTabPanel>
<CustomTabPanel value={value} index={3}>
<Typography variant="h5" sx={{ py: 3 }}>
Setup properties for Golden record lists
</Typography>
<GoldenRecordLists/>
<GoldenRecordLists />
</CustomTabPanel>
<CustomTabPanel value={value} index={4}>
<Deterministic />
</CustomTabPanel>
<CustomTabPanel value={value} index={5}>
<Blocking
demographicData={configurationData.demographicFields}
rules={configurationData?.rules || {}}
/>
<Blocking />
</CustomTabPanel>
<CustomTabPanel value={value} index={6}>
<Typography variant="h5" sx={{ py: 3 }}>
Expand All @@ -159,10 +156,10 @@ const Settings = () => {
<Button variant="outlined" color="secondary">Set to Reference</Button> */}
</Box>
<Box>
<Button
variant="contained"
color="primary"
onClick={handleSave}
<Button
variant="contained"
color="primary"
onClick={handleSave}
disabled={isSaving}
>
{isSaving ? 'Saving...' : 'Save'}
Expand Down
11 changes: 6 additions & 5 deletions JeMPI_Apps/JeMPI_UI/src/pages/settings/blocking/Blocking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useEffect, useState } from 'react'
import { Field, Rule } from 'types/Configuration'
import { a11yProps, CustomTabPanel } from '../deterministic/BasicTabs'
import BlockingContent from './BlockingContent'
import { useConfiguration } from 'hooks/useUIConfiguration'

interface BlockingProps {
demographicData: Field[]
Expand All @@ -20,14 +21,16 @@ interface BlockingProps {
}
}

const Blocking = ({ demographicData = [], rules = {} }: BlockingProps) => {
const Blocking = () => {
const [value, setValue] = useState(0)
const handleChange = (event: React.SyntheticEvent, newValue: number) => {
setValue(newValue)
}
const { configuration, setConfiguration } = useConfiguration()

const matchNotificationRules = rules.matchNotification?.probabilistic ?? []
const linkingRules = rules.link?.probabilistic ?? []
const matchNotificationRules =
configuration?.rules.matchNotification?.probabilistic ?? []
const linkingRules = configuration?.rules.link?.probabilistic ?? []

return (
<Card sx={{ minWidth: 275 }}>
Expand All @@ -50,14 +53,12 @@ const Blocking = ({ demographicData = [], rules = {} }: BlockingProps) => {
</Tabs>
<CustomTabPanel value={value} index={0}>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={linkingRules.length === 0}
linkingRules={{ link: { probabilistic: linkingRules } }}
/>
</CustomTabPanel>
<CustomTabPanel value={value} index={1}>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={matchNotificationRules.length === 0}
linkingRules={{
matchNotification: { probabilistic: matchNotificationRules }
Expand Down
116 changes: 51 additions & 65 deletions JeMPI_Apps/JeMPI_UI/src/pages/settings/blocking/BlockingContent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useReducer, useCallback, useEffect } from 'react'
import React, { useReducer, useEffect } from 'react'
import { AddOutlined, DeleteOutline } from '@mui/icons-material'
import {
Button,
Expand All @@ -11,13 +11,12 @@ import {
SelectChangeEvent
} from '@mui/material'
import SourceView, { RowData } from '../deterministic/SourceView'
import { Configuration, Field, Rule } from 'types/Configuration'
import { Configuration, Rule } from 'types/Configuration'
import { Operator, options } from '../deterministic/DeterministicContent'
import { transformFieldName } from 'utils/helpers'
import { useConfiguration } from 'hooks/useUIConfiguration'

interface BlockingContentProps {
demographicData: Field[]
hasUndefinedRule: boolean
linkingRules: {
link?: { probabilistic?: Rule[] }
Expand Down Expand Up @@ -83,7 +82,6 @@ const reducer = (state: State, action: Action): State => {
}

const BlockingContent = ({
demographicData = [],
hasUndefinedRule,
linkingRules
}: BlockingContentProps) => {
Expand All @@ -106,73 +104,61 @@ const BlockingContent = ({
}
}, [configuration])

const handleUpdateConfiguration = (newRules: Rule[], ruleType: 'matchNotification' | 'link') => {
setConfiguration(prevConfig => {
if (!prevConfig) return prevConfig;

const updatedConfig: Configuration = {
...prevConfig,
rules: {
...prevConfig.rules,
[ruleType]: {
...prevConfig.rules[ruleType],
probabilistic: newRules
}
const handleUpdateConfiguration = (
newRules: Rule[],
ruleType: 'matchNotification' | 'link'
) => {
if (!configuration) return configuration

const updatedConfig: Configuration = {
...configuration,
rules: {
...configuration.rules,
[ruleType]: {
...configuration.rules[ruleType],
probabilistic: newRules
}
};

localStorage.setItem('configuration', JSON.stringify(updatedConfig));
return updatedConfig;
});

setProbabilisticRows(transformRulesToRowData({ probabilistic: newRules }));
};

}
}

const handleAddRule = useCallback(
(ruleType: 'matchNotification' | 'link') => {
const vars = state.fields.filter(
(field, index) => field !== '' && state.fields.indexOf(field) === index
)
const text = state.fields
localStorage.setItem('configuration', JSON.stringify(updatedConfig))
setConfiguration(updatedConfig)
return updatedConfig
}

const handleAddRule = (ruleType: 'matchNotification' | 'link') => {
const vars = state.fields.filter(
(field, index) => field !== '' && state.fields.indexOf(field) === index
)
const text = state.fields
.map((field, index) => {
const operator =
index > 0 ? ` ${state.operators[index - 1].toLowerCase()} ` : '';
const comparator = state.comparators[index];
index > 0 ? ` ${state.operators[index - 1].toLowerCase()} ` : ''
const comparator = state.comparators[index]
const comparatorFunction =
comparator === 0 ? `eq(${field})` : `match(${field}, ${comparator})`;
return `${operator}${comparatorFunction}`;
comparator === 0 ? `eq(${field})` : `match(${field}, ${comparator})`
return `${operator}${comparatorFunction}`
})
.join('');


const newRule: Rule = {
vars,
text
}
.join('')

let updatedRules = [...state.rules]
if (state.editIndex !== null) {
updatedRules[state.editIndex] = newRule
dispatch({ type: 'SET_EDIT_INDEX', payload: null })
} else {
updatedRules = [...state.rules, newRule]
}
const newRule: Rule = {
vars,
text
}

handleUpdateConfiguration(updatedRules, ruleType)
dispatch({ type: 'SET_RULES', payload: updatedRules })
dispatch({ type: 'SET_HAS_CHANGES', payload: false })
dispatch({ type: 'SET_VIEW_TYPE', payload: 0 })
},
[
state.fields,
state.operators,
state.comparators,
state.rules,
state.editIndex,
handleUpdateConfiguration
]
)
let updatedRules = [...state.rules]
if (state.editIndex !== null) {
updatedRules[state.editIndex] = newRule
dispatch({ type: 'SET_EDIT_INDEX', payload: null })
} else {
updatedRules = [...state.rules, newRule]
}

handleUpdateConfiguration(updatedRules, ruleType)
dispatch({ type: 'SET_RULES', payload: updatedRules })
dispatch({ type: 'SET_HAS_CHANGES', payload: false })
dispatch({ type: 'SET_VIEW_TYPE', payload: 0 })
}

const handleFieldChange = (
index: number,
Expand Down Expand Up @@ -385,8 +371,8 @@ const BlockingContent = ({
label="Select Field"
onChange={event => handleFieldChange(index, event)}
>
{Array.isArray(demographicData) &&
demographicData.map(demographicField => (
{Array.isArray(configuration?.demographicFields) &&
configuration?.demographicFields.map(demographicField => (
<MenuItem
key={demographicField.fieldName}
value={demographicField.fieldName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const queryClient = new QueryClient({
queries: {}
}
})

const demographicData: any = mockData.configuration.demographicFields
const linkingRules: any = mockData.configuration.rules

describe('BlockingContent Component', () => {
Expand All @@ -22,7 +20,6 @@ describe('BlockingContent Component', () => {
<BrowserRouter>
<ConfigProvider>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={false}
linkingRules={linkingRules}
handleAddRule={jest.fn()}
Expand All @@ -41,7 +38,6 @@ describe('BlockingContent Component', () => {
<BrowserRouter>
<ConfigProvider>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={false}
linkingRules={linkingRules}
handleAddRule={jest.fn()}
Expand All @@ -67,7 +63,6 @@ describe('BlockingContent Component', () => {
<BrowserRouter>
<ConfigProvider>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={false}
linkingRules={linkingRules}
handleAddRule={jest.fn()}
Expand All @@ -92,7 +87,6 @@ describe('BlockingContent Component', () => {
<BrowserRouter>
<ConfigProvider>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={false}
linkingRules={linkingRules}
handleAddRule={handleAddRuleMock}
Expand All @@ -117,7 +111,6 @@ describe('BlockingContent Component', () => {
<BrowserRouter>
<ConfigProvider>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={false}
linkingRules={linkingRules}
handleAddRule={jest.fn}
Expand All @@ -141,7 +134,6 @@ describe('BlockingContent Component', () => {
<BrowserRouter>
<ConfigProvider>
<BlockingContent
demographicData={demographicData}
hasUndefinedRule={false}
linkingRules={linkingRules}
handleAddRule={jest.fn}
Expand Down