generated from EyeSeeTea/dhis2-app-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from EyeSeeTea/feature/antibiotics-treatment-t…
…able feat: antibiotics treatment episode as table
- Loading branch information
Showing
8 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { Typography } from "@material-ui/core"; | ||
import { Question } from "../../../domain/entities/Questionnaire/QuestionnaireQuestion"; | ||
import { QuestionnaireSection } from "../../../domain/entities/Questionnaire/QuestionnaireSection"; | ||
import { QuestionWidget } from "../survey-questions/QuestionWidget"; | ||
import { PaddedDiv, StyledInput, StyledTitle, StyledWrapper } from "./SurveySection"; | ||
import { | ||
// @ts-ignore | ||
DataTable, | ||
// @ts-ignore | ||
TableHead, | ||
// @ts-ignore | ||
DataTableRow, | ||
// @ts-ignore | ||
DataTableColumnHeader, | ||
// @ts-ignore | ||
DataTableCell, | ||
// @ts-ignore | ||
TableBody, | ||
} from "@dhis2/ui"; | ||
import _c from "../../../domain/entities/generic/Collection"; | ||
import styled from "styled-components"; | ||
import { useTableSection } from "./hook/useTableSection"; | ||
|
||
interface TableSectionProps { | ||
section: QuestionnaireSection; | ||
viewOnly?: boolean; | ||
updateQuestion: (question: Question) => void; | ||
} | ||
|
||
export const TableSection: React.FC<TableSectionProps> = ({ | ||
section, | ||
viewOnly, | ||
updateQuestion, | ||
}) => { | ||
const { questionGroups, headerRow } = useTableSection(section); | ||
return ( | ||
<div key={section.title}> | ||
<DataTable> | ||
<TableHead> | ||
<DataTableRow> | ||
<DataTableColumnHeader colSpan="6"> | ||
<StyledTitle>{section.title}</StyledTitle> | ||
</DataTableColumnHeader> | ||
</DataTableRow> | ||
<DataTableRow> | ||
{headerRow?.map((header, index) => ( | ||
<DataTableColumnHeader key={index}>{header}</DataTableColumnHeader> | ||
))} | ||
</DataTableRow> | ||
</TableHead> | ||
|
||
<TableBody> | ||
{questionGroups?.map(questionGroup => ( | ||
<> | ||
<DataTableRow key={`columns-${questionGroup.groupId}`}> | ||
{questionGroup.columnQuestions.map( | ||
question => | ||
question.isVisible && ( | ||
<DataTableCell key={question.code}> | ||
<StyledDataTableCell key={question.code}> | ||
<QuestionWidget | ||
onChange={updateQuestion} | ||
question={question} | ||
disabled={ | ||
question.disabled || viewOnly | ||
? true | ||
: false | ||
} | ||
/> | ||
{question.errors.map((err, index) => ( | ||
<div key={index}> | ||
<Typography | ||
variant="body2" | ||
color="error" | ||
> | ||
{err} | ||
</Typography> | ||
</div> | ||
))} | ||
</StyledDataTableCell> | ||
</DataTableCell> | ||
) | ||
)} | ||
</DataTableRow> | ||
<DataTableRow key={`detailQuestion-${questionGroup.groupId}`}> | ||
{questionGroup.detailQuestion && | ||
questionGroup.detailQuestion.isVisible && ( | ||
<> | ||
<DataTableCell> | ||
<span>{questionGroup.detailQuestion.text}</span> | ||
</DataTableCell> | ||
|
||
<DataTableCell> | ||
<StyledWrapper> | ||
<StyledInput> | ||
<QuestionWidget | ||
onChange={updateQuestion} | ||
question={questionGroup.detailQuestion} | ||
disabled={ | ||
questionGroup.detailQuestion | ||
.disabled || viewOnly | ||
? true | ||
: false | ||
} | ||
/> | ||
{questionGroup.detailQuestion.errors.map( | ||
(err, index) => ( | ||
<PaddedDiv key={index}> | ||
<Typography | ||
variant="body2" | ||
color="error" | ||
> | ||
{err} | ||
</Typography> | ||
</PaddedDiv> | ||
) | ||
)} | ||
</StyledInput> | ||
</StyledWrapper> | ||
</DataTableCell> | ||
</> | ||
)} | ||
</DataTableRow> | ||
</> | ||
))} | ||
</TableBody> | ||
</DataTable> | ||
</div> | ||
); | ||
}; | ||
|
||
const StyledDataTableCell = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
padding: 0.5rem; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Question } from "../../../../domain/entities/Questionnaire/QuestionnaireQuestion"; | ||
import { | ||
AMR_SURVEYS_MORTALITY_DET_SPECIFY_INN, | ||
QuestionnaireSection, | ||
} from "../../../../domain/entities/Questionnaire/QuestionnaireSection"; | ||
import _c from "../../../../domain/entities/generic/Collection"; | ||
|
||
type QuestionGroup = { | ||
groupId: string; | ||
columnQuestions: Question[]; | ||
detailQuestion?: Question; | ||
}[]; | ||
|
||
export const useTableSection = (section: QuestionnaireSection) => { | ||
const [questionGroups, setQuestionGroups] = useState<QuestionGroup>(); | ||
const [headerRow, setHeaderRow] = useState<string[]>(); | ||
|
||
useEffect(() => { | ||
const groupedRows: QuestionGroup = _c(section.questions) | ||
.groupBy(q => { | ||
return q.text.charAt(0); //Group by section Id which is the first character of the question text | ||
}) | ||
.mapValues(([groupId, questions]) => { | ||
return { | ||
groupId: groupId, | ||
columnQuestions: questions.filter( | ||
question => !question.code.startsWith(AMR_SURVEYS_MORTALITY_DET_SPECIFY_INN) | ||
), // all questions other than the detail question | ||
detailQuestion: questions.find(question => | ||
question.code.startsWith(AMR_SURVEYS_MORTALITY_DET_SPECIFY_INN) | ||
), | ||
}; | ||
}) | ||
.values(); | ||
|
||
const headers = | ||
groupedRows[0]?.columnQuestions | ||
?.filter(q => q.isVisible) | ||
.map(q => q.text.replace("1", "").replace(".", "")) ?? []; | ||
setHeaderRow(headers); | ||
setQuestionGroups(groupedRows); | ||
}, [setQuestionGroups, setHeaderRow, section]); | ||
|
||
return { questionGroups, headerRow }; | ||
}; |