From e75e7b221ce30b71f79c12bfe7f35be2aab1727e Mon Sep 17 00:00:00 2001 From: wzglinieckisoldevelo Date: Thu, 6 Jun 2024 13:59:44 +0200 Subject: [PATCH] ONI-255: Proper filtering of task group by allowed source. --- src/actions.js | 2 +- src/components/TaskHeadPanel.js | 1 + .../groups-management/TaskGroupHeadPanel.js | 8 ++--- src/pickers/TaskGroupPicker.js | 31 ++++++++++++------- src/pickers/TaskSourceAllowedPicker.js | 24 +++++--------- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/actions.js b/src/actions.js index 81616e5..060cbf5 100644 --- a/src/actions.js +++ b/src/actions.js @@ -17,7 +17,7 @@ const TASK_GROUP_PROJECTION = () => [ 'code', 'completionPolicy', 'taskexecutorSet { edges { node { user { id username lastName } } } }', - 'taskAllowedSources' + 'taskAllowedSources', ]; const TASK_FULL_PROJECTION = () => [ diff --git a/src/components/TaskHeadPanel.js b/src/components/TaskHeadPanel.js index b27618a..ff0fc0b 100644 --- a/src/components/TaskHeadPanel.js +++ b/src/components/TaskHeadPanel.js @@ -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)} /> diff --git a/src/components/groups-management/TaskGroupHeadPanel.js b/src/components/groups-management/TaskGroupHeadPanel.js index 8843032..682ac3f 100644 --- a/src/components/groups-management/TaskGroupHeadPanel.js +++ b/src/components/groups-management/TaskGroupHeadPanel.js @@ -50,11 +50,11 @@ class TaskGroupHeadPanel extends FormPanel { const taskGroup = { ...edited }; const { rights } = this.props; const filterAllowedSources = (options) => { - if (!taskGroup?.taskAllowedSources?.length){ - return options + if (!taskGroup?.taskAllowedSources?.length) { + return options; } - const sourcesIds = taskGroup.taskAllowedSources.map((source)=>source.id) - return options.filter((option)=>!sourcesIds.includes(option.id)) + const sourcesIds = taskGroup.taskAllowedSources.map((source) => source.id); + return options.filter((option) => sourcesIds.includes(option.id)); } return ( <> diff --git a/src/pickers/TaskGroupPicker.js b/src/pickers/TaskGroupPicker.js index 5b1f938..58b0d4b 100644 --- a/src/pickers/TaskGroupPicker.js +++ b/src/pickers/TaskGroupPicker.js @@ -1,11 +1,6 @@ -import React, { useState } from 'react'; +import React, { useState } from "react"; -import { - useModulesManager, - useTranslations, - Autocomplete, - useGraphqlQuery, -} from '@openimis/fe-core'; +import { useModulesManager, useTranslations, Autocomplete, useGraphqlQuery } from "@openimis/fe-core"; function TaskGroupPicker(props) { const { @@ -16,6 +11,7 @@ function TaskGroupPicker(props) { withPlaceholder, value, label, + source = null, filterOptions, filterSelectedOptions, placeholder, @@ -23,8 +19,8 @@ function TaskGroupPicker(props) { } = props; const modulesManager = useModulesManager(); - const { formatMessage } = useTranslations('claim', modulesManager); - const [searchString, setSearchString] = useState(''); + const { formatMessage } = useTranslations("claim", modulesManager); + const [searchString, setSearchString] = useState(""); const { isLoading, data, error } = useGraphqlQuery( ` @@ -35,6 +31,7 @@ function TaskGroupPicker(props) { id code completionPolicy + taskAllowedSources } } } @@ -45,17 +42,27 @@ function TaskGroupPicker(props) { }, ); + 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 ( edge.node) ?? []} + options={filteredOptionsWithAllowedSources} isLoading={isLoading} value={value} getOptionLabel={(option) => `${option.code}`} diff --git a/src/pickers/TaskSourceAllowedPicker.js b/src/pickers/TaskSourceAllowedPicker.js index 14186f5..f957728 100644 --- a/src/pickers/TaskSourceAllowedPicker.js +++ b/src/pickers/TaskSourceAllowedPicker.js @@ -1,20 +1,10 @@ -import React from 'react'; -import { - Autocomplete, - useTranslations, - useModulesManager, -} from '@openimis/fe-core'; -import { TASK_CONTRIBUTION_KEY } from '../constants'; +import React from "react"; +import { Autocomplete, useTranslations, useModulesManager } from "@openimis/fe-core"; +import { TASK_CONTRIBUTION_KEY } from "../constants"; -function TaskSourceAllowedPicker({ - onChange, - readOnly, - required, - withLabel, - value, -}) { +function TaskSourceAllowedPicker({ onChange, readOnly, required, withLabel, value }) { const modulesManager = useModulesManager(); - const { formatMessage } = useTranslations('tasksManagement'); + const { formatMessage } = useTranslations("tasksManagement"); const contributions = modulesManager.getContribs(TASK_CONTRIBUTION_KEY); const allowedSources = contributions.flatMap((contribution) => { const source = contribution.taskSource; @@ -25,8 +15,8 @@ function TaskSourceAllowedPicker({