11import styled from "@emotion/styled" ;
2+ import {
3+ Table ,
4+ TableBody ,
5+ TableCell ,
6+ TableContainer ,
7+ TableHead ,
8+ TableRow ,
9+ Paper ,
10+ } from '@mui/material' ;
211import * as React from "react" ;
312import { useState , useEffect } from "react" ;
413import { Chip , Grid } from "@mui/material" ;
@@ -16,12 +25,87 @@ import { Routes } from "../../routes";
1625import { TeamDTO } from "../../api/types/dto" ;
1726import { api } from "../../hooks/use-api" ;
1827import { PageHeader } from "../base/page-header" ;
28+ import { useLoginContext } from "../../contexts/login-context" ;
29+ import { UserRole } from "../../api/types/enums" ;
1930
2031const HeaderContainer = styled ( NonGrowingFlexContainer ) `
2132 justify-content: space-between;
2233 flex-direction: row;
2334` ;
2435
36+ const arraySum = ( array ) => {
37+ return array . reduce ( ( partialSum , a ) => partialSum + a , 0 ) ;
38+ }
39+
40+ /**
41+ * A table displaying the average rating per criterion, and the total sum
42+ * for each project.
43+ */
44+ const RatingResults = ( ) => {
45+ const [ ratingResults , setRatingResults ] = useState ( [ ] ) ;
46+ const [ criteria , setCriteria ] = React . useState ( [ ] ) ;
47+
48+ useEffect (
49+ ( ) => {
50+ api . getRatingResults ( ) . then ( ( stuff ) => {
51+ setRatingResults ( stuff )
52+ } ) ;
53+
54+ api . getAllCriteria ( ) . then ( ( criteria ) => {
55+ setCriteria ( criteria ) ;
56+ } ) ;
57+ } ,
58+ [ ]
59+ ) ;
60+
61+ return (
62+ < div style = { { marginTop : "2em" } } >
63+ < h2 > Results</ h2 >
64+ < p > (Only visible to admins)</ p >
65+ {
66+ < TableContainer component = { Paper } >
67+ < Table >
68+ < TableHead >
69+ < TableRow >
70+ < TableCell > Project</ TableCell >
71+ { criteria . map ( criterion => (
72+ < TableCell key = { criterion . id } align = "center" >
73+ { criterion . title }
74+ </ TableCell >
75+ ) ) }
76+ < TableCell key = "CriterionSum" align = "center" >
77+ Sum
78+ </ TableCell >
79+ </ TableRow >
80+ </ TableHead >
81+ < TableBody >
82+ { ratingResults . map ( resultForProject => (
83+ < TableRow key = { resultForProject . project . id } >
84+ < TableCell >
85+ { resultForProject . project . title } #{ resultForProject . project . id }
86+ </ TableCell >
87+ { criteria . map ( criterion => (
88+ < TableCell key = { criterion . id } align = "center" >
89+ {
90+ resultForProject
91+ . averagesPerCriterion
92+ . find ( ( a ) => a . criterion . id == criterion . id ) ?. average
93+ }
94+ </ TableCell >
95+ ) ) }
96+ < TableCell key = "CriterionSum" align = "center" >
97+ { arraySum ( resultForProject . averagesPerCriterion . map ( ( { average } ) => average ) ) }
98+ </ TableCell >
99+ </ TableRow >
100+ ) ) }
101+ </ TableBody >
102+ </ Table >
103+ </ TableContainer >
104+ }
105+ </ div >
106+ )
107+ }
108+
25109/**
26110 * - Show all projects visible to the user (owned + rating allowed)
27111 * - Let users rate them
@@ -32,6 +116,8 @@ const HeaderContainer = styled(NonGrowingFlexContainer)`
32116export const Projects = ( ) => {
33117 const [ allProjects , setAllProjects ] = useState ( [ ] ) ;
34118 const [ settings , setSettings ] = useState ( { } ) ;
119+ const loginState = useLoginContext ( ) ;
120+ const { user } = loginState ;
35121
36122 // Do this only on mount
37123 useEffect (
@@ -111,6 +197,9 @@ export const Projects = () => {
111197 </ Grid >
112198 ) ) }
113199 </ Grid >
200+ { user . role === UserRole . Root && (
201+ < RatingResults />
202+ ) }
114203 </ Page >
115204 ) ;
116205} ;
0 commit comments