Skip to content

Commit

Permalink
[TM-754] Improve the process for filtering out data that is hidden du…
Browse files Browse the repository at this point in the history
…e to a conditional question.
  • Loading branch information
roguenet committed Jun 11, 2024
1 parent 05338cd commit c2f766b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 51 deletions.
2 changes: 0 additions & 2 deletions src/components/elements/ServerSideTable/ServerSideTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface ServerSideTableProps<TData> extends Omit<TableProps<TData>, "on
meta: any;
onTableStateChange?: (state: ServerSideTableState) => void;
onQueryParamChange?: (queryParams: any) => void;
treeSpeciesShow?: boolean;
}

export function ServerSideTable<TData extends RowData>({
Expand Down Expand Up @@ -68,7 +67,6 @@ export function ServerSideTable<TData extends RowData>({
setPage(1);
setPageSize(size);
}}
treeSpeciesShow={props.treeSpeciesShow}
/>
)}
</>
Expand Down
37 changes: 15 additions & 22 deletions src/components/extensive/Pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,22 @@ export interface PaginationProps extends PageSelectorProps {
hasPageSizeSelector?: boolean;
defaultPageSize?: number;
setPageSize?: (count: number) => void;
treeSpeciesShow?: boolean;
}

function Pagination(props: PaginationProps) {
return props.treeSpeciesShow ? (
<div className={classNames("flex items-center justify-between", props.containerClassName)}>
<PageSelector {...props} />
</div>
) : (
<div className={classNames("flex items-center justify-between", props.containerClassName)}>
{props.hasPageSizeSelector ? (
<PerPageSelector
label="Per page"
options={[5, 10, 15, 20, 50]}
defaultValue={props.defaultPageSize}
onChange={props.setPageSize!}
/>
) : (
<div />
)}
<PageSelector {...props} />
</div>
);
}
const Pagination = (props: PaginationProps) => (
<div className={classNames("flex items-center justify-between", props.containerClassName)}>
{props.hasPageSizeSelector ? (
<PerPageSelector
label="Per page"
options={[5, 10, 15, 20, 50]}
defaultValue={props.defaultPageSize}
onChange={props.setPageSize!}
/>
) : (
<div />
)}
<PageSelector {...props} />
</div>
);

export default Pagination;
8 changes: 4 additions & 4 deletions src/components/extensive/Tables/TreeSpeciesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export interface TreeSpeciesTableProps {
const TreeSpeciesTable = ({ modelName, modelUUID, collection, onFetch }: TreeSpeciesTableProps) => {
const t = useT();

const [queryParams, setQueryParams] = useState<any>();
const [queryParams, setQueryParams] = useState<any>({});

if (collection && queryParams) {
if (collection != null) {
queryParams["filter[collection]"] = collection;
}

Expand All @@ -39,13 +39,13 @@ const TreeSpeciesTable = ({ modelName, modelUUID, collection, onFetch }: TreeSpe
: false;

const showTreeSpecies = useProcessRecordData(modelUUID, modelName, "treeSpecies");
const data = showTreeSpecies ? treeSpecies?.data?.map(item => ({ ...item, amount: item.amount ?? 0 })) ?? [] : [];
return (
<div>
<ServerSideTable
meta={treeSpecies?.meta}
data={(showTreeSpecies ? treeSpecies?.data?.map(item => ({ ...item, amount: item.amount || 0 })) : []) ?? []}
data={data}
isLoading={isLoading}
treeSpeciesShow={true}
onQueryParamChange={setQueryParams}
columns={[
{
Expand Down
35 changes: 12 additions & 23 deletions src/hooks/useProcessRecordData.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
import { useMemo, useState } from "react";
import { useMemo } from "react";

import { GetV2FormsENTITYUUIDResponse, useGetV2FormsENTITYUUID } from "@/generated/apiComponents";

export function useProcessRecordData(modelUUID: string, modelName: string, inputType: string) {
const [show, setShow] = useState(false);
const { data: record } = useGetV2FormsENTITYUUID<{ data: GetV2FormsENTITYUUIDResponse }>({
pathParams: {
uuid: modelUUID,
entity: modelName
}
});

const ProcesssRecordData = useMemo(() => {
const viewDataTable = record?.data?.form?.form_sections.map((formSection: any) =>
formSection.form_questions
.map((formQuestion: any) => formQuestion.uuid)
.map((formQuestionUUID: any) => record?.data?.answers?.[formQuestionUUID])
);
return useMemo(() => {
if (record?.data?.form == null) return false;

for (let sectionIndex in record?.data?.form?.form_sections) {
for (let questionIndex in record?.data?.form?.form_sections[sectionIndex].form_questions) {
const question = record?.data?.form?.form_sections[sectionIndex].form_questions[questionIndex];
if (question.children) {
for (let child of question.children) {
if (child.input_type === inputType) {
setShow(viewDataTable?.[sectionIndex]?.[questionIndex]);
}
for (const section of record.data.form.form_sections) {
for (const question of section.form_questions) {
for (const child of question.children ?? []) {
if (child.input_type == inputType) {
// Only hide the data if the answer is an explicit false in order to not break the UI
// for reports that are older than the current version of the form.
return record.data.answers![child.parent_id] !== false;
}
} else {
setShow(true);
}
}
}

return show;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [record, inputType, modelUUID, modelName, show]);

return ProcesssRecordData;
return true;
}, [record, inputType, modelUUID, modelName]);
}

0 comments on commit c2f766b

Please sign in to comment.