Skip to content

Commit

Permalink
Merge pull request #2015 from jadmsaadaot/TRACK-task#1953
Browse files Browse the repository at this point in the history
Add First Nations column to Work list in Partners Insight
  • Loading branch information
jadmsaadaot authored Mar 21, 2024
2 parents 9a34109 + 87dfd94 commit 36e7010
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 66 deletions.
3 changes: 3 additions & 0 deletions epictrack-api/src/api/models/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class Work(BaseModelVersioned):
eac_decision_by = relationship("Staff", foreign_keys=[eac_decision_by_id], lazy='select')
decision_by = relationship("Staff", foreign_keys=[decision_by_id], lazy='select')
decision_maker_position = relationship("Staff", foreign_keys=[decision_by_id], lazy='select')
indigenous_works = relationship(
"IndigenousWork", backref="parent_work", lazy="select", cascade="all, delete-orphan"
)

def as_dict(self, recursive=True):
"""Return JSON Representation."""
Expand Down
6 changes: 5 additions & 1 deletion epictrack-api/src/api/resources/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ def get():
"""Return all active works."""
args = request.args
is_active = args.get("is_active", False, bool)
include_indigenous_nations = args.get('include_indigenous_nations', False, bool)
works = WorkService.find_all_works(is_active)
return jsonify(res.WorkResponseSchema(many=True).dump(works)), HTTPStatus.OK
exclude = [] if include_indigenous_nations else ['indigenous_works']
works_schema = res.WorkResponseSchema(many=True, exclude=exclude)

return jsonify(works_schema.dump(works)), HTTPStatus.OK

@staticmethod
@cors.crossdomain(origin="*")
Expand Down
18 changes: 18 additions & 0 deletions epictrack-api/src/api/schemas/response/work_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ def get_visibility(self, obj: WorkPhase) -> str:
return obj.visibility if isinstance(obj.visibility, str) else obj.visibility.value


class IndigenousWorkResponseSchema(
AutoSchemaBase
): # pylint: disable=too-many-ancestors,too-few-public-methods
"""Indigenous Work model schema class"""

class Meta:
"""Meta information"""

fields = ['id', 'name']

name = fields.Method("get_name")

def get_name(self, obj: WorkPhase) -> str:
"""Return the name of the indigenous nation"""
return obj.indigenous_nation.name if obj.indigenous_nation else ""


class WorkResponseSchema(
AutoSchemaBase
): # pylint: disable=too-many-ancestors,too-few-public-methods
Expand Down Expand Up @@ -67,6 +84,7 @@ class Meta(AutoSchemaBase.Meta):
eac_decision_by = fields.Nested(StaffSchema, exclude=("position",), dump_only=True)
decision_by = fields.Nested(StaffSchema, exclude=("position",), dump_only=True)
work_state = fields.Method("get_work_state")
indigenous_works = fields.List(fields.Nested(IndigenousWorkResponseSchema, dump_only=True))

def get_work_state(self, obj: Work) -> str:
"""Return the work state"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import React, { useEffect } from "react";
import React, { useEffect, useMemo } from "react";
import { MRT_ColumnDef } from "material-react-table";
import { showNotification } from "components/shared/notificationProvider";
import { Work } from "models/work";
import { rowsPerPageOptions } from "components/shared/MasterTrackTable/utils";
import { searchFilter } from "components/shared/MasterTrackTable/filters";
import TableFilter from "components/shared/filterSelect/TableFilter";
import MasterTrackTable from "components/shared/MasterTrackTable";
import { useGetWorksQuery } from "services/rtkQuery/insights";
import { useGetWorksWithNationsQuery } from "services/rtkQuery/insights";

const WorkList = () => {
const [ministries, setMinistries] = React.useState<string[]>([]);

const [pagination, setPagination] = React.useState({
pageIndex: 0,
pageSize: 10,
});

const { data, error, isLoading } = useGetWorksQuery();
const { data, error, isLoading } = useGetWorksWithNationsQuery();

const works = data || [];

Expand All @@ -27,36 +25,28 @@ const WorkList = () => {
}));
}, [works]);

const codeTypes: { [x: string]: any } = {
ministry: setMinistries,
};

React.useEffect(() => {
Object.keys(codeTypes).forEach((key: string) => {
let accessor = "name";
if (key == "ministry") {
accessor = "abbreviation";
}
const codes = works
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.map((w) => (w[key] ? w[key][accessor] : null))
.filter(
(ele, index, arr) => arr.findIndex((t) => t === ele) === index && ele
);
codeTypes[key](codes);
});
}, [works]);

useEffect(() => {
if (error) {
showNotification("Error fetching works", { type: "error" });
}
}, [error]);

const federalInvolvements = Array.from(
new Set(works.map((w) => w.federal_involvement.name))
);
const federalInvolvements = useMemo(() => {
return Array.from(new Set(works.map((w) => w.federal_involvement.name)));
}, [works]);

const ministries = useMemo(() => {
return Array.from(new Set(works.map((w) => w.ministry.name)));
}, [works]);

const indigenousNations = useMemo(() => {
const nations = works
.map((work) => work.indigenous_works)
.flat()
.map((nation) => nation?.name ?? "")
.filter((nation) => nation);
return Array.from(new Set(nations));
}, [works]);

const columns = React.useMemo<MRT_ColumnDef<Work>[]>(
() => [
Expand Down Expand Up @@ -84,17 +74,19 @@ const WorkList = () => {
/>
);
},
filterFn: (row, id, filterValue) => {
filterFn: (row, id, filterValues) => {
if (
!filterValue.length ||
filterValue.length > ministries.length // select all is selected
!filterValues.length ||
filterValues.length > ministries.length // select all is selected
) {
return true;
}

const value: string = row.getValue(id) || "";

return filterValue.includes(value);
return filterValues.some((filerValue: string) =>
value.includes(filerValue)
);
},
},
{
Expand Down Expand Up @@ -126,6 +118,45 @@ const WorkList = () => {
return filterValue.includes(value);
},
},
{
accessorKey: "indigenous_nations",
header: "First nations",
size: 200,
filterVariant: "multi-select",
filterSelectOptions: indigenousNations,
accessorFn: (row) => {
return (
row.indigenous_works
?.map((indigenous_work) => indigenous_work.name)
?.join(", ") ?? ""
);
},
Filter: ({ header, column }) => {
return (
<TableFilter
isMulti
header={header}
column={column}
variant="inline"
name="rolesFilter"
/>
);
},
filterFn: (row, id, filterValues) => {
if (
!filterValues.length ||
filterValues.length > indigenousNations.length // select all is selected
) {
return true;
}

const value: string = row.getValue(id) || "";

return filterValues.some((filterValue: string) =>
value.includes(filterValue)
);
},
},
],
[ministries, works]
);
Expand Down
4 changes: 4 additions & 0 deletions epictrack-web/src/models/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export interface Work extends MasterBase {
substitution_act: ListType;
eac_decision_by: Staff;
decision_by: Staff;
indigenous_works?: {
id: number;
name: string;
}[];
}

export interface WorkPhase extends MasterBase {
Expand Down
Loading

0 comments on commit 36e7010

Please sign in to comment.