1- import axios , { AxiosResponse } from 'axios' ;
21import { useState } from 'react' ;
32import { useMutation , useQuery } from 'react-query' ;
4- import useToken from './token' ;
3+ import authenticatedRequest from '../components/utils/request' ;
4+ import { useToken } from './token' ;
55
66function useProcessInterval ( {
77 onSuccess,
@@ -22,35 +22,27 @@ function useProcessInterval({
2222 code : string ;
2323 benchmarkId : string ;
2424 } ) {
25- const response = await fetch (
26- `${ process . env . REACT_APP_API_ENDPOINT } /submissions` ,
27- {
28- method : 'POST' ,
29- headers : {
30- 'Content-Type' : 'application/json' ,
31- Authorization : `Bearer ${ token } ` ,
32- } ,
33- body : JSON . stringify ( {
34- language : 'cpython3' ,
35- code : code ,
36- benchmarkId : benchmarkId ,
37- } ) ,
25+ const { data } = await authenticatedRequest ( {
26+ url : `/submissions` ,
27+ method : 'POST' ,
28+ data : {
29+ language : 'cpython3' ,
30+ code : code ,
31+ benchmarkId : benchmarkId ,
3832 } ,
39- ) ;
40- return await response . json ( ) ;
33+ } ) ;
34+ return data ;
4135 }
4236
4337 const { mutate } = useMutation ( createJob , {
4438 onMutate : ( ) => {
4539 setIsPollingEnabled ( true ) ;
4640 } ,
4741 onError : ( error ) => {
48- console . error ( error ) ;
4942 setIsPollingEnabled ( false ) ;
5043 onError ( ) ;
5144 } ,
5245 onSuccess : ( data ) => {
53- console . log ( data ) ;
5446 setProcessId ( data . id ) ;
5547 } ,
5648 } ) ;
@@ -59,19 +51,18 @@ function useProcessInterval({
5951 const { isLoading, data } = useQuery (
6052 [ 'processProgress' , token , processId ] ,
6153 async ( ) => {
62- const res : AxiosResponse < {
63- status : string ;
64- stdout : string ;
65- stderr : string ;
66- } > = await axios . get (
67- `${ process . env . REACT_APP_API_ENDPOINT } /submissions/${ processId } ` ,
68- {
69- headers : {
70- Authorization : `Bearer ${ token } ` ,
71- } ,
72- } ,
73- ) ;
74- return res . data ;
54+ const {
55+ data,
56+ } : {
57+ data : {
58+ status : string ;
59+ stdout : string ;
60+ stderr : string ;
61+ } ;
62+ } = await authenticatedRequest ( {
63+ url : `submissions/${ processId } ` ,
64+ } ) ;
65+ return data ;
7566 } ,
7667 {
7768 onSuccess : ( data ) => {
@@ -101,23 +92,16 @@ export function useLastSubmissionForUser(
10192 benchmarkId : string ,
10293 language : string ,
10394) {
104- const { token } = useToken ( ) ;
105-
10695 return useQuery < { code : string } , Error > (
10796 `last-submission-${ benchmarkId } -${ language } ` ,
10897 async ( ) => {
10998 if ( benchmarkId && language ) {
110- const { data } = await axios . get (
111- `${ process . env . REACT_APP_API_ENDPOINT } /benchmarks/${ benchmarkId } /submissions/last` ,
112- {
113- headers : {
114- Authorization : `Bearer ${ token } ` ,
115- } ,
116- params : {
117- language,
118- } ,
99+ const { data } = await authenticatedRequest ( {
100+ url : `benchmarks/${ benchmarkId } /submissions/last` ,
101+ params : {
102+ language,
119103 } ,
120- ) ;
104+ } ) ;
121105 return data ;
122106 }
123107 } ,
0 commit comments