1
- import { FSModule } from 'browserfs/dist/node/core/FS' ;
2
- import { Chapter , Variant } from 'js-slang/dist/types' ;
3
- import { compressToEncodedURIComponent } from 'lz-string' ;
4
- import qs from 'query-string' ;
1
+ import { Chapter } from 'js-slang/dist/types' ;
5
2
import { SagaIterator } from 'redux-saga' ;
6
- import { call , delay , put , race , select } from 'redux-saga/effects' ;
3
+ import { call , put , select } from 'redux-saga/effects' ;
7
4
import CseMachine from 'src/features/cseMachine/CseMachine' ;
8
5
import { CseMachine as JavaCseMachine } from 'src/features/cseMachine/java/CseMachine' ;
9
6
10
- import {
11
- changeQueryString ,
12
- shortenURL ,
13
- updateShortURL
14
- } from '../../features/playground/PlaygroundActions' ;
15
- import { GENERATE_LZ_STRING , SHORTEN_URL } from '../../features/playground/PlaygroundTypes' ;
16
7
import { isSourceLanguage , OverallState } from '../application/ApplicationTypes' ;
17
- import { ExternalLibraryName } from '../application/types/ExternalTypes' ;
18
- import { retrieveFilesInWorkspaceAsRecord } from '../fileSystem/utils' ;
19
8
import { visitSideContent } from '../sideContent/SideContentActions' ;
20
9
import { SideContentType , VISIT_SIDE_CONTENT } from '../sideContent/SideContentTypes' ;
21
10
import Constants from '../utils/Constants' ;
22
- import { showSuccessMessage , showWarningMessage } from '../utils/notifications/NotificationsHelper' ;
23
11
import {
24
12
clearReplOutput ,
25
13
setEditorHighlightedLines ,
@@ -29,46 +17,10 @@ import {
29
17
updateCurrentStep ,
30
18
updateStepsTotal
31
19
} from '../workspace/WorkspaceActions' ;
32
- import { EditorTabState , PlaygroundWorkspaceState } from '../workspace/WorkspaceTypes' ;
20
+ import { PlaygroundWorkspaceState } from '../workspace/WorkspaceTypes' ;
33
21
import { safeTakeEvery as takeEvery } from './SafeEffects' ;
34
22
35
23
export default function * PlaygroundSaga ( ) : SagaIterator {
36
- yield takeEvery ( GENERATE_LZ_STRING , updateQueryString ) ;
37
-
38
- yield takeEvery ( SHORTEN_URL , function * ( action : ReturnType < typeof shortenURL > ) : any {
39
- const queryString = yield select ( ( state : OverallState ) => state . playground . queryString ) ;
40
- const keyword = action . payload ;
41
- const errorMsg = 'ERROR' ;
42
-
43
- let resp , timeout ;
44
-
45
- //we catch and move on if there are errors (plus have a timeout in case)
46
- try {
47
- const { result, hasTimedOut } = yield race ( {
48
- result : call ( shortenURLRequest , queryString , keyword ) ,
49
- hasTimedOut : delay ( 10000 )
50
- } ) ;
51
-
52
- resp = result ;
53
- timeout = hasTimedOut ;
54
- } catch ( _ ) { }
55
-
56
- if ( ! resp || timeout ) {
57
- yield put ( updateShortURL ( errorMsg ) ) ;
58
- return yield call ( showWarningMessage , 'Something went wrong trying to create the link.' ) ;
59
- }
60
-
61
- if ( resp . status !== 'success' && ! resp . shorturl ) {
62
- yield put ( updateShortURL ( errorMsg ) ) ;
63
- return yield call ( showWarningMessage , resp . message ) ;
64
- }
65
-
66
- if ( resp . status !== 'success' ) {
67
- yield call ( showSuccessMessage , resp . message ) ;
68
- }
69
- yield put ( updateShortURL ( Constants . urlShortenerBase + resp . url . keyword ) ) ;
70
- } ) ;
71
-
72
24
yield takeEvery (
73
25
VISIT_SIDE_CONTENT ,
74
26
function * ( {
@@ -126,60 +78,30 @@ export default function* PlaygroundSaga(): SagaIterator {
126
78
) ;
127
79
}
128
80
129
- function * updateQueryString ( ) {
130
- const isFolderModeEnabled : boolean = yield select (
131
- ( state : OverallState ) => state . workspaces . playground . isFolderModeEnabled
132
- ) ;
133
- const fileSystem : FSModule = yield select (
134
- ( state : OverallState ) => state . fileSystem . inBrowserFileSystem
135
- ) ;
136
- const files : Record < string , string > = yield call (
137
- retrieveFilesInWorkspaceAsRecord ,
138
- 'playground' ,
139
- fileSystem
140
- ) ;
141
- const editorTabs : EditorTabState [ ] = yield select (
142
- ( state : OverallState ) => state . workspaces . playground . editorTabs
143
- ) ;
144
- const editorTabFilePaths = editorTabs
145
- . map ( ( editorTab : EditorTabState ) => editorTab . filePath )
146
- . filter ( ( filePath ) : filePath is string => filePath !== undefined ) ;
147
- const activeEditorTabIndex : number | null = yield select (
148
- ( state : OverallState ) => state . workspaces . playground . activeEditorTabIndex
149
- ) ;
150
- const chapter : Chapter = yield select (
151
- ( state : OverallState ) => state . workspaces . playground . context . chapter
152
- ) ;
153
- const variant : Variant = yield select (
154
- ( state : OverallState ) => state . workspaces . playground . context . variant
155
- ) ;
156
- const external : ExternalLibraryName = yield select (
157
- ( state : OverallState ) => state . workspaces . playground . externalLibrary
158
- ) ;
159
- const execTime : number = yield select (
160
- ( state : OverallState ) => state . workspaces . playground . execTime
161
- ) ;
162
- const newQueryString = qs . stringify ( {
163
- isFolder : isFolderModeEnabled ,
164
- files : compressToEncodedURIComponent ( qs . stringify ( files ) ) ,
165
- tabs : editorTabFilePaths . map ( compressToEncodedURIComponent ) ,
166
- tabIdx : activeEditorTabIndex ,
167
- chap : chapter ,
168
- variant,
169
- ext : external ,
170
- exec : execTime
171
- } ) ;
172
- yield put ( changeQueryString ( newQueryString ) ) ;
173
- }
174
-
81
+ type UrlShortenerResponse = {
82
+ status : string ;
83
+ code : string ;
84
+ url : {
85
+ keyword : string ;
86
+ url : string ;
87
+ title : string ;
88
+ date : string ;
89
+ ip : string ;
90
+ clicks : string ;
91
+ } ;
92
+ message : string ;
93
+ title : string ;
94
+ shorturl : string ;
95
+ statusCode : number ;
96
+ } ;
175
97
/**
176
98
* Gets short url from microservice
177
99
* @returns {(Response|null) } Response if successful, otherwise null.
178
100
*/
179
- export async function shortenURLRequest (
101
+ export async function externalUrlShortenerRequest (
180
102
queryString : string ,
181
103
keyword : string
182
- ) : Promise < Response | null > {
104
+ ) : Promise < { shortenedUrl : string ; message : string } > {
183
105
const url = `${ window . location . protocol } //${ window . location . host } /playground#${ queryString } ` ;
184
106
185
107
const params = {
@@ -199,9 +121,15 @@ export async function shortenURLRequest(
199
121
200
122
const resp = await fetch ( `${ Constants . urlShortenerBase } yourls-api.php` , fetchOpts ) ;
201
123
if ( ! resp || ! resp . ok ) {
202
- return null ;
124
+ throw new Error ( 'Something went wrong trying to create the link.' ) ;
125
+ }
126
+
127
+ const res : UrlShortenerResponse = await resp . json ( ) ;
128
+ if ( res . status !== 'success' && ! res . shorturl ) {
129
+ throw new Error ( res . message ) ;
203
130
}
204
131
205
- const res = await resp . json ( ) ;
206
- return res ;
132
+ const message = res . status !== 'success' ? res . message : '' ;
133
+ const shortenedUrl = Constants . urlShortenerBase + res . url . keyword ;
134
+ return { shortenedUrl, message } ;
207
135
}
0 commit comments