@@ -6,11 +6,24 @@ import {
66 SubmitCodeResponse ,
77} from "../model/types" ;
88
9+ interface SubmissionsQueryArg {
10+ duelId : string ;
11+ taskKey ?: string | null ;
12+ }
13+
14+ const normalizeSubmissionsArg = ( arg : string | SubmissionsQueryArg ) => {
15+ if ( typeof arg === "string" ) {
16+ return { duelId : arg , taskKey : null } ;
17+ }
18+
19+ return arg ;
20+ } ;
21+
922export const submitCodeApiSlice = apiSlice . injectEndpoints ( {
1023 endpoints : ( builder ) => ( {
1124 submitCode : builder . mutation <
1225 SubmitCodeResponse ,
13- { duelId : string ; data : SubmitCodeRequestData }
26+ { duelId : string ; data : SubmitCodeRequestData ; taskKey ?: string | null }
1427 > ( {
1528 query : ( { duelId, data } ) => ( {
1629 url : `/duels/${ duelId } /submissions` ,
@@ -20,14 +33,14 @@ export const submitCodeApiSlice = apiSlice.injectEndpoints({
2033 invalidatesTags : ( _result , _error , { duelId } ) => [
2134 { type : "Submission" , id : `LIST-${ duelId } ` } ,
2235 ] ,
23- async onQueryStarted ( { duelId, data } , { dispatch, queryFulfilled } ) {
36+ async onQueryStarted ( { duelId, data, taskKey } , { dispatch, queryFulfilled } ) {
2437 try {
2538 const { data : result } = await queryFulfilled ;
2639
2740 dispatch (
2841 submitCodeApiSlice . util . updateQueryData (
2942 "getSubmissions" ,
30- duelId ,
43+ { duelId, taskKey : taskKey ?? null } ,
3144 ( draft ) => {
3245 const exists = draft . some (
3346 ( s ) => String ( s . submission_id ) === String ( result . submission_id ) ,
@@ -51,20 +64,28 @@ export const submitCodeApiSlice = apiSlice.injectEndpoints({
5164 } ,
5265 } ) ,
5366
54- getSubmissions : builder . query < SubmissionItem [ ] , string > ( {
55- query : ( duelId : string ) => ( {
56- url : `/duels/${ duelId } /submissions` ,
57- } ) ,
58- providesTags : ( result , _error , duelId ) =>
59- result
67+ getSubmissions : builder . query < SubmissionItem [ ] , string | SubmissionsQueryArg > ( {
68+ query : ( arg ) => {
69+ const { duelId, taskKey } = normalizeSubmissionsArg ( arg ) ;
70+ return {
71+ url : `/duels/${ duelId } /submissions` ,
72+ params : {
73+ ...( taskKey ? { taskKey } : { } ) ,
74+ } ,
75+ } ;
76+ } ,
77+ providesTags : ( result , _error , arg ) => {
78+ const { duelId } = normalizeSubmissionsArg ( arg ) ;
79+ return result
6080 ? [
6181 ...result . map ( ( { submission_id } ) => ( {
6282 type : "Submission" as const ,
6383 id : `${ duelId } -${ submission_id } ` ,
6484 } ) ) ,
6585 { type : "Submission" , id : `LIST-${ duelId } ` } ,
6686 ]
67- : [ { type : "Submission" , id : `LIST-${ duelId } ` } ] ,
87+ : [ { type : "Submission" , id : `LIST-${ duelId } ` } ] ;
88+ } ,
6889 merge : ( currentCache , newItems ) => {
6990 if ( ! currentCache ) return newItems ;
7091
@@ -80,7 +101,14 @@ export const submitCodeApiSlice = apiSlice.injectEndpoints({
80101
81102 return Array . from ( resultMap . values ( ) ) ;
82103 } ,
83- forceRefetch : ( { currentArg, previousArg } ) => currentArg !== previousArg ,
104+ forceRefetch : ( { currentArg, previousArg } ) => {
105+ if ( ! currentArg || ! previousArg ) {
106+ return true ;
107+ }
108+ const current = normalizeSubmissionsArg ( currentArg ) ;
109+ const previous = normalizeSubmissionsArg ( previousArg ) ;
110+ return current . duelId !== previous . duelId || current . taskKey !== previous . taskKey ;
111+ } ,
84112 } ) ,
85113
86114 getSubmissionDetail : builder . query <
@@ -99,7 +127,7 @@ export const submitCodeApiSlice = apiSlice.injectEndpoints({
99127 dispatch (
100128 submitCodeApiSlice . util . updateQueryData (
101129 "getSubmissions" ,
102- duelId ,
130+ { duelId, taskKey : null } ,
103131 ( draft ) => {
104132 const submissionIndex = draft . findIndex (
105133 ( s ) => String ( s . submission_id ) === String ( submissionId ) ,
0 commit comments