Skip to content

Commit

Permalink
Prise en compte des statuts non concerné dans le score personnalisé +…
Browse files Browse the repository at this point in the history
… Correction d'arrondi
  • Loading branch information
mariheck committed Oct 23, 2023
1 parent 971761d commit 4731652
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ export const useTasksScoreRepartition = (actionId: string) => {
const tasksScores = scores[referentielId(actionId)].filter(
act => act.action_id.includes(actionId) && act.action_id !== actionId
);
const subActionScore = scores[referentielId(actionId)].filter(
act => act.action_id === actionId
);

let scoreFait = 0;
let scoreProgramme = 0;
let scorePasFait = 0;
let scoreMax = 0;

tasksScores.forEach(task => {
scoreFait += task.point_fait;
scoreProgramme += task.point_programme;
scorePasFait += task.point_pas_fait;
scoreMax += task.point_potentiel;
});

return {
tasksScores,
avancementDetaille: [scoreFait, scoreProgramme, scorePasFait],
scoreMax,
scoreMax: subActionScore[0].point_potentiel,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ export const useTasksStatus = (tasksIds: string[]) => {
);

let tasksStatus: {
[key: string]: TActionAvancement;
[key: string]: {avancement: TActionAvancement; concerne: boolean};
} = {};

tasksIds.forEach(taskId => {
const task = data?.find(action => action.action_id === taskId);
if (task !== undefined)
tasksStatus = {...tasksStatus, [taskId]: task.avancement};
tasksStatus = {
...tasksStatus,
[taskId]: {avancement: task.avancement, concerne: task.concerne},
};
});

return {tasksStatus, isLoading};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {avancementToLabel} from 'app/labels';
import {SuiviScoreRow} from 'app/pages/collectivite/EtatDesLieux/Referentiel/data/useScoreRealise';
import {useTasksScoreRepartition} from 'app/pages/collectivite/EtatDesLieux/Referentiel/data/useTasksScores';
import SubActionTasksList from 'app/pages/collectivite/EtatDesLieux/Referentiel/SuiviAction/SubActionTasksList';
import {actionAvancementColors} from 'app/theme';
import {ActionDefinitionSummary} from 'core-logic/api/endpoints/ActionDefinitionSummaryReadEndpoint';
import {useActionSummaryChildren} from 'core-logic/hooks/referentiel';
import {useTasksStatus} from 'core-logic/hooks/useActionStatut';
import {Dispatch, SetStateAction, useState} from 'react';
import {ActionScore} from 'types/ClientScore';
import ProgressBarWithTooltip from 'ui/score/ProgressBarWithTooltip';
import {TActionAvancement} from 'types/alias';
import Modal from 'ui/shared/floating-ui/Modal';
import {StatusToSavePayload} from './ActionStatusDropdown';
import {getStatusFromIndex} from './utils';

/**
* Vérifie pour chaque tâche de la sous-action le statut
Expand All @@ -21,12 +16,7 @@ import {getStatusFromIndex} from './utils';
const isCustomScoreGranted = (
tasks: ActionDefinitionSummary[],
tasksStatus: {
[key: string]:
| 'fait'
| 'pas_fait'
| 'programme'
| 'non_renseigne'
| 'detaille';
[key: string]: {avancement: TActionAvancement; concerne: boolean};
},
localStatus: {
[key: string]: StatusToSavePayload;
Expand All @@ -35,7 +25,8 @@ const isCustomScoreGranted = (
const isGranted = tasks.reduce((result, currTask) => {
if (
(!!tasksStatus[currTask.id] &&
tasksStatus[currTask.id] === 'non_renseigne') ||
tasksStatus[currTask.id].avancement === 'non_renseigne' &&
tasksStatus[currTask.id].concerne) ||
!tasksStatus[currTask.id]
) {
// Si la tâche est à 'non renseigné' ou n'a pas de statut attribué
Expand All @@ -61,40 +52,6 @@ const isCustomScoreGranted = (
return isGranted;
};

/**
* Calcul pour une tâche donnée du nouveau score détaillé
* en fonction du statut choisi dans la modale
*/
const getNewTaskScores = (task: ActionScore, payload: StatusToSavePayload) => {
let taskScores = {
point_fait: 0,
point_programme: 0,
point_pas_fait: 0,
};

switch (payload.avancement) {
case 'fait':
case 'programme':
case 'pas_fait':
taskScores[`point_${payload.avancement}`] = task.point_potentiel;
break;
case 'detaille':
if (payload.avancementDetaille) {
taskScores.point_fait =
task.point_potentiel * payload.avancementDetaille[0];
taskScores.point_programme =
task.point_potentiel * payload.avancementDetaille[1];
taskScores.point_pas_fait =
task.point_potentiel * payload.avancementDetaille[2];
}
break;
default:
break;
}

return taskScores;
};

type ScoreAutoModalProps = {
action: ActionDefinitionSummary;
actionScores: {[actionId: string]: SuiviScoreRow};
Expand All @@ -113,24 +70,12 @@ const ScoreAutoModal = ({
onOpenScorePerso,
}: ScoreAutoModalProps): JSX.Element => {
const tasks = useActionSummaryChildren(action);
const scores = useTasksScoreRepartition(action.id);

const {tasksStatus} = useTasksStatus(tasks.map(task => task.id));

const [localStatus, setLocalStatus] = useState<{
[key: string]: StatusToSavePayload;
}>({});
const [localTasksScores, setLocalTasksScores] = useState<{
[key: string]: {
point_fait: number;
point_programme: number;
point_pas_fait: number;
};
}>({});
const [localAvancement, setLocalAvancement] = useState(
scores.avancementDetaille
);

const handleChangeStatus = (payload: StatusToSavePayload) => {
setLocalStatus(prevState => ({
...prevState,
Expand All @@ -141,50 +86,6 @@ const ScoreAutoModal = ({
avancementDetaille: payload.avancementDetaille,
},
}));

// Calcul de la jauge de score auto en fonction
// des modifications faites dans la modale
// en passant en revue toutes les tâches de la sous-action
let scoreFait = 0;
let scoreProgramme = 0;
let scorePasFait = 0;

scores.tasksScores.forEach(task => {
if (
task.action_id !== payload.actionId &&
!localTasksScores[task.action_id]
) {
// La tâche n'est pas celle modifiée dans la payload
// et n'a pas été modifiée dans la modale
// -> score stocké en base
scoreFait += task.point_fait;
scoreProgramme += task.point_programme;
scorePasFait += task.point_pas_fait;
} else if (
task.action_id !== payload.actionId &&
localTasksScores[task.action_id]
) {
// La tâche n'est pas celle modifiée dans la payload
// mais a été modifiée dans la modale
// -> score stocké localement
scoreFait += localTasksScores[task.action_id].point_fait;
scoreProgramme += localTasksScores[task.action_id].point_programme;
scorePasFait += localTasksScores[task.action_id].point_pas_fait;
} else {
// La tâche est celle modifiée dans la payload
// -> calcul du nouveau score de la tâche
const newScores = getNewTaskScores(task, payload);
scoreFait += newScores.point_fait;
scoreProgramme += newScores.point_programme;
scorePasFait += newScores.point_pas_fait;
setLocalTasksScores(prevState => ({
...prevState,
[task.action_id]: {...newScores},
}));
}
});

setLocalAvancement([scoreFait, scoreProgramme, scorePasFait]);
};

const handleSaveScoreAuto = () => {
Expand Down Expand Up @@ -212,29 +113,6 @@ const ScoreAutoModal = ({
<>
<h4>Détailler l'avancement : {action.id.split('_')[1]}</h4>

{scores && scores.scoreMax && localAvancement ? (
<div className="flex items-start mt-2 mb-6">
<p className="mb-0 text-sm mr-4">
Score {action.referentiel === 'cae' ? 'automatique' : ''}
</p>
<ProgressBarWithTooltip
score={
localAvancement?.map((a, idx) => ({
value: a,
label: avancementToLabel[getStatusFromIndex(idx)],
color: actionAvancementColors[getStatusFromIndex(idx)],
})) ?? []
}
total={scores.scoreMax ?? 0}
defaultScore={{
label: avancementToLabel.non_renseigne,
color: actionAvancementColors.non_renseigne,
}}
valueToDisplay={avancementToLabel.fait}
/>
</div>
) : null}

<hr className="p-1" />

<div className="w-full -mt-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const ScoreDetailleModal = ({
{/* Score automatique */}
{isScorePerso &&
scores &&
scores.tasksScores.filter(task => task.concerne).length &&
scores.avancementDetaille &&
scores.scoreMax ? (
<div className="flex items-start mt-2 mb-6">
Expand Down

0 comments on commit 4731652

Please sign in to comment.