Skip to content

Commit 177f873

Browse files
committed
Re-add playground configuration encoder
1 parent f5c5735 commit 177f873

File tree

9 files changed

+61
-20
lines changed

9 files changed

+61
-20
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type ShareLinkShortenedUrlResponse = {
2+
shortenedUrl: string;
3+
};

src/commons/controlBar/ControlBarShareButton.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { IconNames } from '@blueprintjs/icons';
33
import { useHotkeys } from '@mantine/hooks';
44
import React, { useRef, useState } from 'react';
55
import * as CopyToClipboard from 'react-copy-to-clipboard';
6-
import { usePlaygroundConfigurationEncoder } from 'src/features/playground/shareLinks/encoder/Encoder';
7-
import ShareLinkState from 'src/features/playground/shareLinks/ShareLinkState';
6+
import JsonEncoderDelegate from 'src/features/playground/shareLinks/encoder/delegates/JsonEncoderDelegate';
7+
import { usePlaygroundConfigurationEncoder } from 'src/features/playground/shareLinks/encoder/EncoderHooks';
88

99
import ControlButton from '../ControlButton';
1010
import { postSharedProgram } from '../sagas/RequestsSaga';
@@ -26,17 +26,10 @@ type StateProps = {
2626
shortURL?: string;
2727
key: string;
2828
isSicp?: boolean;
29-
programConfig: ShareLinkState;
30-
token: Tokens;
3129
};
3230

3331
type ShareLinkRequestHelperParams = RemoveLast<Parameters<typeof request>>;
3432

35-
export type Tokens = {
36-
accessToken: string | undefined;
37-
refreshToken: string | undefined;
38-
};
39-
4033
export const requestToShareProgram = async (
4134
...[path, method, opts]: ShareLinkRequestHelperParams
4235
) => {
@@ -56,7 +49,9 @@ export const ControlBarShareButton: React.FC<ControlBarShareButtonProps> = props
5649

5750
customStringKeyword;
5851

59-
return postSharedProgram(playgroundConfiguration)
52+
const configuration = playgroundConfiguration.encodeWith(new JsonEncoderDelegate());
53+
54+
return postSharedProgram(configuration)
6055
.then(({ shortenedUrl }) => setShortenedUrl(shortenedUrl))
6156
.catch(err => showWarningMessage(err.toString()))
6257
.finally(() => setIsLoading(false));

src/commons/sagas/RequestsSaga.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { call } from 'redux-saga/effects';
22
import { backendParamsToProgressStatus } from 'src/features/grading/GradingUtils';
3-
import ShareLinkState from 'src/features/playground/shareLinks/ShareLinkState';
43
import { OptionType } from 'src/pages/academy/teamFormation/subcomponents/TeamFormationForm';
54

65
import {
@@ -1666,12 +1665,14 @@ export async function deleteDevice(device: Pick<Device, 'id'>, tokens?: Tokens):
16661665
* POST /shared_programs
16671666
*/
16681667
export async function postSharedProgram(
1669-
programConfig: ShareLinkState,
1668+
programConfig: string,
16701669
tokens?: Tokens
16711670
): Promise<ShareLinkShortenedUrlResponse> {
16721671
tokens = fillTokens(tokens);
16731672
const resp = await request(`shared_programs`, 'POST', {
1674-
body: programConfig,
1673+
body: {
1674+
configuration: programConfig
1675+
},
16751676
...tokens
16761677
});
16771678

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import ShareLinkState from '../ShareLinkState';
2+
import EncoderDelegate from './delegates/EncoderDelegate';
3+
4+
class ShareLinkStateEncoder {
5+
state: ShareLinkState;
6+
7+
constructor(state: ShareLinkState) {
8+
this.state = state;
9+
}
10+
11+
encodeWith(encoderDelegate: EncoderDelegate): string {
12+
return encoderDelegate.encode(this.state);
13+
}
14+
}
15+
16+
export default ShareLinkStateEncoder;

src/features/playground/shareLinks/encoder/Encoder.tsx renamed to src/features/playground/shareLinks/encoder/EncoderHooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { useTypedSelector } from 'src/commons/utils/Hooks';
77
import { EditorTabState } from 'src/commons/workspace/WorkspaceTypes';
88

99
import ShareLinkState from '../ShareLinkState';
10+
import ShareLinkStateEncoder from './Encoder';
1011

11-
export const usePlaygroundConfigurationEncoder = () => {
12+
export const usePlaygroundConfigurationEncoder = (): ShareLinkStateEncoder => {
1213
const isFolderModeEnabled = useTypedSelector(
1314
state => state.workspaces.playground.isFolderModeEnabled
1415
);
@@ -36,7 +37,7 @@ export const usePlaygroundConfigurationEncoder = () => {
3637
exec: execTime.toString()
3738
};
3839

39-
return result;
40+
return new ShareLinkStateEncoder(result);
4041
};
4142

4243
const useGetFile = () => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ShareLinkState from '../../ShareLinkState';
2+
3+
interface EncoderDelegate {
4+
encode(state: ShareLinkState): string;
5+
}
6+
7+
export default EncoderDelegate;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import ShareLinkState from '../../ShareLinkState';
2+
import EncoderDelegate from './EncoderDelegate';
3+
4+
class JsonEncoderDelegate implements EncoderDelegate {
5+
encode(state: ShareLinkState) {
6+
return JSON.stringify(state);
7+
}
8+
}
9+
10+
export default JsonEncoderDelegate;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import qs from 'query-string';
2+
3+
import ShareLinkState from '../../ShareLinkState';
4+
import EncoderDelegate from './EncoderDelegate';
5+
6+
class UrlParamsEncoderDelegate implements EncoderDelegate {
7+
encode(state: ShareLinkState): string {
8+
return qs.stringify(state);
9+
}
10+
}
11+
12+
export default UrlParamsEncoderDelegate;

src/pages/playground/Playground.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ import {
9191
import ShareLinkStateDecoder from 'src/features/playground/shareLinks/decoder/Decoder';
9292
import JsonDecoderDelegate from 'src/features/playground/shareLinks/decoder/delegates/JsonDecoderDelegate';
9393
import UrlParamsDecoderDelegate from 'src/features/playground/shareLinks/decoder/delegates/UrlParamsDecoderDelegate';
94-
import { usePlaygroundConfigurationEncoder } from 'src/features/playground/shareLinks/encoder/Encoder';
9594
import ShareLinkState from 'src/features/playground/shareLinks/ShareLinkState';
9695

9796
import {
@@ -445,7 +444,6 @@ const Playground: React.FC<PlaygroundProps> = props => {
445444

446445
const hash = isSicpEditor ? props.initialEditorValueHash : location.hash;
447446
const { uuid } = useParams<{ uuid: string }>();
448-
const config = usePlaygroundConfigurationEncoder();
449447
const tokens = useTypedSelector((state: OverallState) => ({
450448
accessToken: state.session.accessToken,
451449
refreshToken: state.session.refreshToken
@@ -845,14 +843,12 @@ const Playground: React.FC<PlaygroundProps> = props => {
845843
handleShortenURL={s => dispatch(shortenURL(s))}
846844
handleUpdateShortURL={s => dispatch(updateShortURL(s))}
847845
queryString={qs}
848-
programConfig={config}
849-
token={tokens}
850846
shortURL={shortURL}
851847
isSicp={isSicpEditor}
852848
key="share"
853849
/>
854850
);
855-
}, [dispatch, isSicpEditor, props.initialEditorValueHash, queryString, shortURL, config, tokens]);
851+
}, [dispatch, isSicpEditor, props.initialEditorValueHash, queryString, shortURL]);
856852

857853
const toggleFolderModeButton = useMemo(() => {
858854
return (

0 commit comments

Comments
 (0)