1
+ import axios from 'axios' ;
2
+ import { useLocation } from 'react-router-dom' ;
3
+ import { initialize , renew_accessToken } from '../modules/member' ;
4
+ import * as Sentry from "@sentry/react" ;
5
+ import { postSurveyThumbnail } from "../services/SurveyService" ;
6
+
7
+ const SURVEY_API_BASE_URL = "http://210.109.60.160" ;
8
+ const SURVEY_SHARE_URL = "http://210.109.60.160" ;
9
+
10
+ const config = {
11
+ withCredentials :true ,
12
+ accesstoken :localStorage . getItem ( 'accessToken' ) ,
13
+ refreshtoken :localStorage . getItem ( 'refreshToken' )
14
+ }
15
+
16
+ class SurveyService {
17
+
18
+ async saveSurvey ( loc , memberid , data ) {
19
+ axios . defaults . headers . post = null
20
+ const thumbNum = Math . floor ( Math . random ( ) * 5 + 1 ) ;
21
+ console . log ( "thumbNum: " , thumbNum ) ;
22
+ const response = await axios . post ( `${ SURVEY_API_BASE_URL } ${ loc } /${ thumbNum } /${ memberid } ` , data , { headers :config } ) . catch ( function ( e ) {
23
+ Sentry . captureException ( e ) ;
24
+ } )
25
+ if ( response . headers [ "auth-token" ] ) {
26
+ const surveyId = response . data . result ;
27
+ // postSurveyThumbnail(surveyId);
28
+ const auth_token = response . headers [ "auth-token" ] . split ( ';' , 2 ) [ 0 ] . split ( '=' , 2 ) [ 1 ]
29
+ const access_Token = auth_token . split ( ":" , 4 ) [ 1 ]
30
+ const refresh_Token = auth_token . split ( ":" , 4 ) [ 3 ]
31
+ renew_accessToken ( access_Token , refresh_Token ) ;
32
+ }
33
+ if ( response . status === 401 ) {
34
+ initialize ( )
35
+ }
36
+ return response
37
+ }
38
+ shareSurvey ( surveyId ) {
39
+ return `${ SURVEY_SHARE_URL } /respondent/${ surveyId ? surveyId : "" } `
40
+ }
41
+ async loadSurvey ( loc , memberid ) {
42
+ var value = loc
43
+ if ( loc === "/survey" ) {
44
+ var value = value + "/" + memberid
45
+ }
46
+ else if ( loc . substring ( 0 , 7 ) === "/manage" ) {
47
+ var value = loc . substring ( 7 , loc . length ) + "/" + memberid
48
+ }
49
+ value = "/load" + value
50
+ const response = await axios . get ( SURVEY_API_BASE_URL + `${ value } ` , { headers :config } ) . catch ( function ( e ) {
51
+ Sentry . captureException ( e ) ;
52
+ } )
53
+ if ( response . headers [ "auth-token" ] ) {
54
+ const auth_token = response . headers [ "auth-token" ] . split ( ';' , 2 ) [ 0 ] . split ( '=' , 2 ) [ 1 ]
55
+ const access_Token = auth_token . split ( ":" , 4 ) [ 1 ]
56
+ const refresh_Token = auth_token . split ( ":" , 4 ) [ 3 ]
57
+ renew_accessToken ( access_Token , refresh_Token ) ;
58
+ }
59
+ if ( response . status === 401 ) {
60
+ initialize ( )
61
+ }
62
+ return response
63
+ }
64
+ async sendSurvey ( loc , data ) {
65
+ const response = await axios . post ( SURVEY_API_BASE_URL + `${ loc } ` , data , { headers :config } ) . catch ( function ( e ) {
66
+ Sentry . captureException ( e ) ;
67
+ } )
68
+ if ( response . headers [ "auth-token" ] ) {
69
+ const auth_token = response . headers [ "auth-token" ] . split ( ';' , 2 ) [ 0 ] . split ( '=' , 2 ) [ 1 ]
70
+ const access_Token = auth_token . split ( ":" , 4 ) [ 1 ]
71
+ const refresh_Token = auth_token . split ( ":" , 4 ) [ 3 ]
72
+ renew_accessToken ( access_Token , refresh_Token ) ;
73
+ }
74
+ if ( response . status === 401 ) {
75
+ initialize ( )
76
+ }
77
+ return response
78
+ }
79
+ }
80
+
81
+ export default new SurveyService ( )
0 commit comments