Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add loading state to RunButton in CodeBlock component #237

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions src/components/Common/CodeBlock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { Highlight, Prism, themes } from 'prism-react-renderer';
import Editor from 'react-simple-code-editor';
import { alpha, Box, Button } from '@mui/material';
import { alpha, Box, Button, CircularProgress } from '@mui/material';
import { styled, useTheme } from '@mui/material/styles';
import { PlayArrowOutlined } from '@mui/icons-material';
import { CopyButton } from './CopyButton';
Expand All @@ -25,25 +25,32 @@ const StyledEditor = styled((props) => <Editor padding={0} textareaClassName={'c
* @return {JSX.Element}
* @constructor
*/
export const RunButton = ({ code, onRun }) => {
export const RunButton = ({ code, onRun, loading }) => {
return (
<Button variant="outlined" endIcon={<PlayArrowOutlined />} onClick={() => onRun(code)} data-testid="code-block-run">
Run
<Button
variant="outlined"
endIcon={loading ? <CircularProgress size={24} color="inherit" /> : <PlayArrowOutlined />}
onClick={() => onRun(code)}
disabled={loading}
data-testid="code-block-run"
>
{loading ? 'Running...' : 'Run'}
</Button>
);
};

RunButton.propTypes = {
code: PropTypes.string.isRequired,
onRun: PropTypes.func.isRequired,
loading: PropTypes.bool,
};

/**
* Code block with syntax highlighting
* @return {JSX.Element}
* @constructor
*/
export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, editable = true }) => {
export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, editable = true, loading }) => {
const [code, setCode] = useState(codeStr);
const theme = useTheme();
const prismTheme = theme.palette.mode === 'light' ? themes.nightOwlLight : themes.vsDark;
Expand Down Expand Up @@ -102,7 +109,7 @@ export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, edit
>
{withRunButton && onRun && (
<Box sx={{ flexGrow: '1' }}>
<RunButton code={code} onRun={onRun} />
<RunButton code={code} onRun={onRun} loading={loading} />
</Box>
)}
{title && <Box>{title}</Box>}
Expand Down Expand Up @@ -131,4 +138,5 @@ CodeBlock.propTypes = {
onRun: PropTypes.func,
title: PropTypes.string,
editable: PropTypes.bool, // by default code block is editable
loading: PropTypes.bool,
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ export const MdxCodeBlock = ({ children }) => {
const language = className.replace(/language-/, '');
const withRunButton = children.props.withRunButton && bigIntJSON.parse(children.props.withRunButton);
const { setResult } = useTutorial();
const [loading, setLoading] = React.useState(false);

const handleRun = (code) => {
setLoading(true);
setResult('{}');
requestFromCode(code, false)
.then((res) => {
setResult(() => bigIntJSON.stringify(res));
setLoading(false);
})
.catch((err) => {
setResult(() => bigIntJSON.stringify(err));
setLoading(false);
});
};

return <CodeBlock codeStr={code} language={language} withRunButton={withRunButton} onRun={handleRun} />;
return (
<CodeBlock codeStr={code} language={language} withRunButton={withRunButton} onRun={handleRun} loading={loading} />
);
};

MdxCodeBlock.propTypes = {
Expand Down
Loading