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' ;
52import { SagaIterator } from 'redux-saga' ;
6- import { call , delay , put , race , select } from 'redux-saga/effects' ;
3+ import { call , put , select } from 'redux-saga/effects' ;
74import CseMachine from 'src/features/cseMachine/CseMachine' ;
85import { CseMachine as JavaCseMachine } from 'src/features/cseMachine/java/CseMachine' ;
96
10- import {
11- changeQueryString ,
12- shortenURL ,
13- updateShortURL
14- } from '../../features/playground/PlaygroundActions' ;
15- import { GENERATE_LZ_STRING , SHORTEN_URL } from '../../features/playground/PlaygroundTypes' ;
167import { isSourceLanguage , OverallState } from '../application/ApplicationTypes' ;
17- import { ExternalLibraryName } from '../application/types/ExternalTypes' ;
18- import { retrieveFilesInWorkspaceAsRecord } from '../fileSystem/utils' ;
198import { visitSideContent } from '../sideContent/SideContentActions' ;
209import { SideContentType , VISIT_SIDE_CONTENT } from '../sideContent/SideContentTypes' ;
2110import Constants from '../utils/Constants' ;
22- import { showSuccessMessage , showWarningMessage } from '../utils/notifications/NotificationsHelper' ;
2311import {
2412 clearReplOutput ,
2513 setEditorHighlightedLines ,
@@ -29,46 +17,10 @@ import {
2917 updateCurrentStep ,
3018 updateStepsTotal
3119} from '../workspace/WorkspaceActions' ;
32- import { EditorTabState , PlaygroundWorkspaceState } from '../workspace/WorkspaceTypes' ;
20+ import { PlaygroundWorkspaceState } from '../workspace/WorkspaceTypes' ;
3321import { safeTakeEvery as takeEvery } from './SafeEffects' ;
3422
3523export 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-
7224 yield takeEvery (
7325 VISIT_SIDE_CONTENT ,
7426 function * ( {
@@ -126,60 +78,30 @@ export default function* PlaygroundSaga(): SagaIterator {
12678 ) ;
12779}
12880
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+ } ;
17597/**
17698 * Gets short url from microservice
17799 * @returns {(Response|null) } Response if successful, otherwise null.
178100 */
179- export async function shortenURLRequest (
101+ export async function externalUrlShortenerRequest (
180102 queryString : string ,
181103 keyword : string
182- ) : Promise < Response | null > {
104+ ) : Promise < { shortenedUrl : string ; message : string } > {
183105 const url = `${ window . location . protocol } //${ window . location . host } /playground#${ queryString } ` ;
184106
185107 const params = {
@@ -199,9 +121,15 @@ export async function shortenURLRequest(
199121
200122 const resp = await fetch ( `${ Constants . urlShortenerBase } yourls-api.php` , fetchOpts ) ;
201123 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 ) ;
203130 }
204131
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 } ;
207135}
0 commit comments