Skip to content
Merged
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
28 changes: 25 additions & 3 deletions features/forms/components/form-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ function getDefaultAnswer(question: { type: string; min?: number }): any {
return undefined;
}

/**
* Extracts the ticked options of a choice list answer.
*
* @param rawValue - The current answer, in either shape described below.
* @returns The ticked options, or an empty list when none is ticked.
*
* @remarks
* The choice list UI keeps its answer as `{ selected, other }`, while an answer
* read straight from `valores_selecionados` is a plain array. Both shapes reach
* the save path, so both are normalized to the array the column stores.
*/
function getSelectedOptions(rawValue: any): string[] {
if (Array.isArray(rawValue)) return rawValue.map(String);
if (Array.isArray(rawValue?.selected)) return rawValue.selected.map(String);
return [];
}

/**
* Control Record questions auto-filled by the session and hidden during
* execution: the app fills them when the session ends, and they remain editable
Expand Down Expand Up @@ -277,8 +294,14 @@ export const FormComponent = forwardRef(function FormComponent(
for (const r of respostas) {
// An ATA answer lives in its own column: the ticked options are the
// answer, and `valor_preenchido` only carries the derived score.
// The choice list UI reads `{ selected, other }`, so the stored
// array is wrapped back into that shape — otherwise reopening the
// form would show every indicator unticked.
if (Array.isArray(r.valores_selecionados)) {
loadedAnswers[r.pergunta_id] = r.valores_selecionados;
loadedAnswers[r.pergunta_id] = {
selected: r.valores_selecionados.map(String),
other: "",
};
continue;
}
if (r.valor_preenchido == null) continue;
Expand Down Expand Up @@ -374,13 +397,12 @@ export const FormComponent = forwardRef(function FormComponent(
// why it is sent even though `isFilled` is false for it.
const isAtaChoice = isAta && q.type === "choice_list";
if (isAtaChoice) {
const selected = Array.isArray(rawValue) ? rawValue : [];
return {
formulario_id: formularioId,
sessao_id: sessaoId || null,
aluno_id: alunoId || null,
pergunta_id: q.id,
valores_selecionados: selected,
valores_selecionados: getSelectedOptions(rawValue),
status_item: "respondido",
};
}
Expand Down
Loading