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

ONI-255: Basic allowed task source picker and filtering. #52

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const TASK_GROUP_PROJECTION = () => [
'code',
'completionPolicy',
'taskexecutorSet { edges { node { user { id username lastName } } } }',
'taskAllowedSources',
];

const TASK_FULL_PROJECTION = () => [
Expand Down Expand Up @@ -64,6 +65,7 @@ export const formatTaskGroupGQL = (taskGroup) => {
${taskGroup?.id ? `id: "${taskGroup.id}"` : ''}
${taskGroup?.taskexecutorSet ? `userIds: ${executorsString}` : 'userIds: []'}
${taskGroup?.taskSources ? `taskSources: ${taskSourcesString}` : 'taskSources: []'}
${taskGroup?.taskAllowedSources ? `taskAllowedSources: ${taskSourcesString}` : 'taskAllowedSources: []'}
`;
};

Expand Down Expand Up @@ -122,6 +124,7 @@ export function fetchTaskGroup(modulesManager, variables) {
completionPolicy
jsonExt
taskexecutorSet { edges { node { user { id username lastName } } } },
taskAllowedSources
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/components/TaskHeadPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class TaskHeadPanel extends FormPanel {
readOnly={!rights.includes(TASK_UPDATE)
|| [TASK_STATUS.COMPLETED, TASK_STATUS.FAILED].includes(task.status)}
withNull
source={task?.source}
value={task?.taskGroup}
onChange={(taskGroup) => this.updateAttribute('taskGroup', taskGroup)}
/>
Expand Down
18 changes: 18 additions & 0 deletions src/components/groups-management/TaskGroupHeadPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { withTheme, withStyles } from '@material-ui/core/styles';
import TaskExecutorsPicker from '../../pickers/TaskExecutorsPicker';
import GroupPolicyPicker from '../../pickers/GroupPolicyPicker';
import TaskSourcePicker from '../../pickers/TaskSourcePicker';
import TaskSourceAllowedPicker from '../../pickers/TaskSourceAllowedPicker';
import { TASK_GROUP_UPDATE, TASK_GROUP_CREATE } from '../../constants';

const styles = (theme) => ({
tableTitle: theme.table.title,
Expand Down Expand Up @@ -46,6 +48,14 @@ class TaskGroupHeadPanel extends FormPanel {
edited, classes, readOnly,
} = this.props;
const taskGroup = { ...edited };
const { rights } = this.props;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The properties have already been destructured from this.props. Add rights to the destructured properties.

const filterAllowedSources = (options) => {
if (!taskGroup?.taskAllowedSources?.length) {
return options;
}
const sourcesIds = taskGroup.taskAllowedSources.map((source) => source.id);
return options.filter((option) => sourcesIds.includes(option.id));
};
return (
<>
{renderHeadPanelTitle(classes)}
Expand Down Expand Up @@ -86,6 +96,14 @@ class TaskGroupHeadPanel extends FormPanel {
readOnly={readOnly}
value={taskGroup?.taskSources}
onChange={(sources) => this.updateAttribute('taskSources', sources)}
filterOptions={filterAllowedSources}
/>
</Grid>
<Grid item xs={6} className={classes.item}>
<TaskSourceAllowedPicker
readOnly={!(rights.includes(TASK_GROUP_CREATE) || rights.includes(TASK_GROUP_UPDATE))}
value={taskGroup?.taskAllowedSources}
onChange={(allowedSources) => this.updateAttribute('taskAllowedSources', allowedSources)}
/>
</Grid>
</Grid>
Expand Down
21 changes: 15 additions & 6 deletions src/pickers/TaskGroupPicker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React, { useState } from 'react';

import {
useModulesManager,
useTranslations,
Autocomplete,
useGraphqlQuery,
import {

Check failure on line 3 in src/pickers/TaskGroupPicker.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
useModulesManager, useTranslations, Autocomplete, useGraphqlQuery

Check failure on line 4 in src/pickers/TaskGroupPicker.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed

Check failure on line 4 in src/pickers/TaskGroupPicker.js

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
} from '@openimis/fe-core';

function TaskGroupPicker(props) {
Expand All @@ -16,6 +13,7 @@
withPlaceholder,
value,
label,
source = null,
filterOptions,
filterSelectedOptions,
placeholder,
Expand All @@ -35,6 +33,7 @@
id
code
completionPolicy
taskAllowedSources
}
}
}
Expand All @@ -45,6 +44,16 @@
},
);

const options = data?.taskGroup?.edges.map((edge) => edge.node) ?? [];

const filteredOptionsWithAllowedSources = options.filter((option) => {
const parsedResponse = JSON.parse(option.taskAllowedSources);
const allowedSources = typeof parsedResponse === 'object' ? [parsedResponse] : parsedResponse;
const usersAllowedSources = allowedSources.flatMap((source) => source.task_allowed_sources);

return usersAllowedSources.includes(source);
});

return (
<Autocomplete
multiple={multiple}
Expand All @@ -55,7 +64,7 @@
withLabel={withLabel}
withPlaceholder={withPlaceholder}
readOnly={readOnly}
options={data?.taskGroup?.edges.map((edge) => edge.node) ?? []}
options={filteredOptionsWithAllowedSources}
isLoading={isLoading}
value={value}
getOptionLabel={(option) => `${option.code}`}
Expand Down
33 changes: 33 additions & 0 deletions src/pickers/TaskSourceAllowedPicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";

Check failure on line 1 in src/pickers/TaskSourceAllowedPicker.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
import { Autocomplete, useTranslations, useModulesManager } from "@openimis/fe-core";

Check failure on line 2 in src/pickers/TaskSourceAllowedPicker.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
import { TASK_CONTRIBUTION_KEY } from "../constants";

Check failure on line 3 in src/pickers/TaskSourceAllowedPicker.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

function TaskSourceAllowedPicker({ onChange, readOnly, required, withLabel, value }) {

Check failure on line 5 in src/pickers/TaskSourceAllowedPicker.js

View workflow job for this annotation

GitHub Actions / lint

Expected a line break after this opening brace

Check failure on line 5 in src/pickers/TaskSourceAllowedPicker.js

View workflow job for this annotation

GitHub Actions / lint

Expected a line break before this closing brace
const modulesManager = useModulesManager();
const { formatMessage } = useTranslations("tasksManagement");

Check failure on line 7 in src/pickers/TaskSourceAllowedPicker.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
const contributions = modulesManager.getContribs(TASK_CONTRIBUTION_KEY);
const allowedSources = contributions.flatMap((contribution) => {
const source = contribution.taskSource;
return source ? source.map((item) => ({ id: item, name: item })) : [];
});

return (
<Autocomplete
multiple
required={required}
label={formatMessage("TaskSourceAllowedPicker.label")}

Check failure on line 18 in src/pickers/TaskSourceAllowedPicker.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
placeholder={formatMessage("TaskSourceAllowedPicker.placeholder")}
readOnly={readOnly}
withLabel={withLabel}
withPlaceholder={!value?.length}
options={allowedSources}
value={value}
getOptionLabel={(source) => `${source.name}`}
onChange={onChange}
filterSelectedOptions
onInputChange={() => {}}
/>
);
}

export default TaskSourceAllowedPicker;
3 changes: 2 additions & 1 deletion src/pickers/TaskSourcePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function TaskSourcePicker({
required,
withLabel,
value,
filterOptions
}) {
const modulesManager = useModulesManager();
const { formatMessage } = useTranslations('tasksManagement');
Expand All @@ -34,7 +35,7 @@ function TaskSourcePicker({
value={value}
getOptionLabel={(source) => `${source.name}`}
onChange={onChange}
filterSelectedOptions
filterOptions={filterOptions}
onInputChange={() => {}}
/>
);
Expand Down
3 changes: 3 additions & 0 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ function reducer(
taskSources: taskGroup?.jsonExt
? JSON.parse(taskGroup.jsonExt).task_sources.map((source) => ({ id: source, name: source }))
: [],
taskAllowedSources: taskGroup?.taskAllowedSources
? JSON.parse(taskGroup?.taskAllowedSources).task_allowed_sources.map((source) => ({ id: source, name: source }))
: [],
}))?.[0],
fetchingTaskGroup: false,
errorTaskGroup: formatGraphQLError(action.payload),
Expand Down
2 changes: 2 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"tasksManagement.menu.taskExecutionerGroups": "Task Executioner Groups",
"tasksManagement.TaskSourcePicker.label": "Task Sources",
"tasksManagement.TaskSourcePicker.placeholder": "Search for task sources...",
"tasksManagement.TaskSourceAllowedPicker.label": "Allowed Task Sources",
"tasksManagement.TaskSourceAllowedPicker.placeholder": "Search for allowed task sources...",
"tasksManagement.entries.tasksManagementAllView": "All Tasks",
"tasksManagement.task.source.mutationLabel": "Resolving task",
"tasksManagement.task.source.IndividualService": "Individual Service",
Expand Down
Loading