@@ -11,11 +11,13 @@ import {
1111 HttpPaymentRequiredError ,
1212 License ,
1313 ReviewStatusEnum ,
14+ DeploymentStatus ,
1415} from "../models"
1516import {
1617 getMe ,
1718 searchDeployments as _searchDeployments ,
1819 searchReviews as _searchReviews ,
20+ getDeployment ,
1921 getLicense
2022} from "../apis"
2123import { getShortRef } from "../libs"
@@ -157,6 +159,27 @@ export const notifyDeploymentEvent = createAsyncThunk<void, Deployment, { state:
157159 }
158160)
159161
162+ export const notifyDeploymentStatusEvent = createAsyncThunk < void , DeploymentStatus , { state : { main : MainState } } > (
163+ "main/notifyDeploymentStatusEvent" ,
164+ async ( deploymentStatus , { rejectWithValue} ) => {
165+ if ( deploymentStatus . edges === undefined ) {
166+ return rejectWithValue ( new Error ( "Edges is not included." ) )
167+ }
168+
169+ const { repo, deployment } = deploymentStatus . edges
170+ if ( repo === undefined || deployment === undefined ) {
171+ return rejectWithValue ( new Error ( "Repo or Deployment is not included in the edges." ) )
172+ }
173+
174+ notify ( `${ repo . namespace } /${ repo . name } #${ deployment . number } ` , {
175+ icon : "/logo192.png" ,
176+ body : `${ deploymentStatus . status } - ${ deploymentStatus . description } ` ,
177+ tag : String ( deployment . id ) ,
178+
179+ } )
180+ }
181+ )
182+
160183/**
161184 * The browser notifies the requester when the review is responded to,
162185 * but it should notify the reviewer when the review is requested.
@@ -183,6 +206,22 @@ export const notifyReviewmentEvent = createAsyncThunk<void, Review, { state: { m
183206)
184207
185208
209+ export const handleDeploymentStatusEvent = createAsyncThunk < Deployment , DeploymentStatus , { state : { main : MainState } } > (
210+ "main/handleDeploymentStatusEvent" ,
211+ async ( deploymentStatus , { rejectWithValue } ) => {
212+ if ( deploymentStatus . edges === undefined ) {
213+ return rejectWithValue ( new Error ( "Edges is not included." ) )
214+ }
215+
216+ const { repo, deployment } = deploymentStatus . edges
217+ if ( repo === undefined || deployment === undefined ) {
218+ return rejectWithValue ( new Error ( "Repo or Deployment is not included in the edges." ) )
219+ }
220+
221+ return await getDeployment ( repo . namespace , repo . name , deployment . number )
222+ }
223+ )
224+
186225export const mainSlice = createSlice ( {
187226 name : "main" ,
188227 initialState,
@@ -240,5 +279,21 @@ export const mainSlice = createSlice({
240279 . addCase ( fetchLicense . fulfilled , ( state , action ) => {
241280 state . license = action . payload
242281 } )
282+
283+ . addCase ( handleDeploymentStatusEvent . fulfilled , ( state , { payload : deployment } ) => {
284+ if ( deployment . status === DeploymentStatusEnum . Created ) {
285+ state . deployments . unshift ( deployment )
286+ return
287+ }
288+
289+ state . deployments = state . deployments . map ( ( item ) => {
290+ return ( item . id === deployment . id ) ? deployment : item
291+ } )
292+
293+ state . deployments = state . deployments . filter ( ( item ) => {
294+ return ! ( item . status === DeploymentStatusEnum . Success
295+ || item . status === DeploymentStatusEnum . Failure )
296+ } )
297+ } )
243298 }
244299} )
0 commit comments