1- import * as React from "react" ;
2- import { Build } from "../types" ;
1+ import React from "react" ;
2+ import { Build , TestRun } from "../types" ;
33import { buildsService } from "../services" ;
4+ import { TestStatus } from "../types/testStatus" ;
45
56interface IRequestAction {
67 type : "request" ;
@@ -22,7 +23,23 @@ interface IDeleteAction {
2223 payload : string ;
2324}
2425
25- type IAction = IRequestAction | IGetAction | IDeleteAction | ISelectAction ;
26+ interface IUpdateAction {
27+ type : "update" ;
28+ payload : TestRun ;
29+ }
30+
31+ interface IStatsAction {
32+ type : "stats" ;
33+ payload : string ;
34+ }
35+
36+ type IAction =
37+ | IRequestAction
38+ | IGetAction
39+ | IDeleteAction
40+ | ISelectAction
41+ | IStatsAction
42+ | IUpdateAction ;
2643
2744type Dispatch = ( action : IAction ) => void ;
2845type State = {
@@ -49,6 +66,57 @@ function buildReducer(state: State, action: IAction): State {
4966 ...state ,
5067 selectedBuildId : action . payload ,
5168 } ;
69+ case "stats" :
70+ return {
71+ ...state ,
72+ buildList : state . buildList . map ( ( build ) => {
73+ if ( build . id === action . payload ) {
74+ // reset stats
75+ build . passedCount = 0 ;
76+ build . unresolvedCount = 0 ;
77+ build . failedCount = 0 ;
78+
79+ // calculate stats
80+ build . testRuns . forEach ( ( testRun ) => {
81+ switch ( testRun . status ) {
82+ case TestStatus . approved :
83+ case TestStatus . ok : {
84+ build . passedCount += 1 ;
85+ break ;
86+ }
87+ case TestStatus . unresolved :
88+ case TestStatus . new : {
89+ build . unresolvedCount += 1 ;
90+ break ;
91+ }
92+ case TestStatus . failed : {
93+ build . failedCount += 1 ;
94+ break ;
95+ }
96+ }
97+ } ) ;
98+ }
99+ return build ;
100+ } ) ,
101+ } ;
102+ case "update" :
103+ return {
104+ ...state ,
105+ buildList : state . buildList . map ( ( build ) => {
106+ if ( build . id === action . payload . buildId ) {
107+ return {
108+ ...build ,
109+ testRuns : build . testRuns . map ( ( testRun ) => {
110+ if ( testRun . id === action . payload . id ) {
111+ return action . payload ;
112+ }
113+ return testRun ;
114+ } ) ,
115+ } ;
116+ }
117+ return build ;
118+ } ) ,
119+ } ;
52120 case "get" :
53121 return {
54122 ...state ,
@@ -99,6 +167,7 @@ async function getBuildList(dispatch: Dispatch, id: string) {
99167 . getList ( id )
100168 . then ( ( items ) => {
101169 dispatch ( { type : "get" , payload : items } ) ;
170+ items . forEach ( ( build ) => dispatch ( { type : "stats" , payload : build . id } ) ) ;
102171 } )
103172 . catch ( ( error ) => {
104173 console . log ( error . toString ( ) ) ;
@@ -124,11 +193,17 @@ async function selectBuild(dispatch: Dispatch, id: string) {
124193 dispatch ( { type : "select" , payload : id } ) ;
125194}
126195
196+ async function updateBuild ( dispatch : Dispatch , testRun : TestRun ) {
197+ dispatch ( { type : "update" , payload : testRun } ) ;
198+ dispatch ( { type : "stats" , payload : testRun . buildId } ) ;
199+ }
200+
127201export {
128202 BuildProvider ,
129203 useBuildState ,
130204 useBuildDispatch ,
131205 getBuildList ,
132206 deleteBuild ,
133207 selectBuild ,
208+ updateBuild ,
134209} ;
0 commit comments