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

JDOC-794 Share button saves OpenAPI instead of JSight #256

Open
wants to merge 3 commits into
base: rc/6.1
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/components/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface EditorProps {
setError?: React.Dispatch<React.SetStateAction<ErrorSimpleType | null>>;
readOnly?: boolean;
currentTheme?: string;
jsightEditorId?: string;
}

function initializeEditor(
Expand Down Expand Up @@ -67,18 +68,20 @@ export const Editor = React.memo(
reload,
reloadedEditor,
readOnly = false,
jsightEditorId = 'jsightEditor',
}: EditorProps) => {
const {key, version, history} = useContext(SharingContext);
const ref = useRef<HTMLDivElement | null>(null);
const jsightEditor = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
const decorationsRef = useRef<string[]>([]);
const [isEditorLoaded, setIsEditorLoaded] = useState<boolean>(false);
const [isContentRendered, setIsContentRendered] = useState<boolean>(false);
const dontUpdateSharingBtn = useRef<boolean>(false);
const languagesList = ['jsight', 'jschema', 'markdown'];
const currentLanguage = 'jsight';

// @ts-ignore
window['jsightEditor'] = jsightEditor;
window[jsightEditorId] = jsightEditor;

const languages: monaco.languages.ILanguageExtensionPoint[] = languagesList.map((id) => ({
id,
Expand Down Expand Up @@ -210,6 +213,8 @@ export const Editor = React.memo(
useEffect(() => {
if (isEditorLoaded) {
if (key) {
// if (isContentRendered && readOnly) return;

dontUpdateSharingBtn.current = true;
(async () => {
try {
Expand All @@ -223,6 +228,7 @@ export const Editor = React.memo(
history.push(`/r/${result.code}/${result.version}`);
}
highlightError();
setIsContentRendered(true);
} catch (error) {
if (error.Code) {
setError &&
Expand Down
6 changes: 5 additions & 1 deletion src/components/MainContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,18 @@ export const MainContent = React.memo((props: MainContentProps) => {
(currentDocSidebar === 'htmldoc' && isJdocLoading),
});

/* TODO: This is workaround. Refactor to avoid openapi compatibility check */
const isValidOpenApiContent = openApiContent.substr(0, 7) === 'openapi';

return (
<div className="main-content-wrapper">
<div className={mainContentClasses}>
{currentDocSidebar === 'openapi' && viewMode !== 'doc' && (
{currentDocSidebar === 'openapi' && viewMode !== 'doc' && isValidOpenApiContent && (
<div className="openapi-wrapper">
<Editor
content={openApiContent}
readOnly={true}
jsightEditorId="openApiViewer"
reload={reloadOpenApi}
reloadedEditor={handleReloadOpenapi}
currentTheme="default"
Expand Down
17 changes: 12 additions & 5 deletions src/components/ShareButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ export const ShareButton: React.FC<ShareButtonProps> = ({openSharingModal, disab
const {key, version} = useContext(SharingContext);
const isAbleUpdate = useMemo(() => key && version, [key, version]);

const handleNewState = async (updateOrCreateState: () => void) => {
const handleCreateState = async () => {
try {
await updateOrCreateState();
await createState();
openSharingModal();
} catch (err) {}
};

const handleUpdateState = async () => {
try {
await updateExistState();
openSharingModal();
} catch (err) {}
};
Expand All @@ -37,7 +44,7 @@ export const ShareButton: React.FC<ShareButtonProps> = ({openSharingModal, disab
</div>
</DropdownToggle>
<DropdownMenu offsetX={-160}>
<div className="menu-item" onClick={() => handleNewState(updateExistState)}>
<div className="menu-item" onClick={handleUpdateState}>
<div>
<i className="icon-upload" />
</div>
Expand All @@ -47,7 +54,7 @@ export const ShareButton: React.FC<ShareButtonProps> = ({openSharingModal, disab
</div>
</div>
<hr />
<div className="menu-item" onClick={() => handleNewState(createState)}>
<div className="menu-item" onClick={handleCreateState}>
<div>
<i className="icon-plus" />
</div>
Expand All @@ -64,7 +71,7 @@ export const ShareButton: React.FC<ShareButtonProps> = ({openSharingModal, disab
disabled={disableSharing || !process.env.REACT_APP_CLOUD_URL}
icon="link"
className="save-by-link-btn"
onClick={() => handleNewState(createState)}
onClick={handleCreateState}
>
Share
</Button>
Expand Down
16 changes: 11 additions & 5 deletions src/hooks/useSharing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createNewState, updateState} from 'api/codeSharing';
import {createNewState, updateState, CodeSharingParamsType} from 'api/codeSharing';
import {useParams} from 'react-router-dom';
import {toast} from 'react-toastify';
import {ToastOptions} from 'react-toastify/dist/types';
Expand Down Expand Up @@ -30,13 +30,18 @@ export function useSharing() {
? (window as any).jsightEditor
: null;

const createState = () => {
const getUrl = (response: CodeSharingParamsType) =>
`/r/${response.code}/${response.version}${path ? `#${path}` : ''}`;

const createState = async () => {
const content = jsightEditor.current?.getValue();

if (content !== undefined) {
return createNewState(content)
.then((response) => {
history.push(`/r/${response.code}/${response.version}${path ? `#${path}` : ''}`);
const url = getUrl(response);
console.log(url);
window.history.pushState({}, document.title, url);
})
.catch(() => {
toast.warning(SharingErrorNotification, errorOptions);
Expand All @@ -47,13 +52,14 @@ export function useSharing() {
}
};

const updateExistState = () => {
const updateExistState = async () => {
const content = jsightEditor.current?.getValue();

if (content !== undefined) {
return updateState(key, content)
.then((response) => {
history.push(`/r/${response.code}/${response.version}${path ? `#${path}` : ''}`);
const url = getUrl(response);
window.history.pushState({}, document.title, url);
})
.catch(() => {
toast.warning(SharingErrorNotification, errorOptions);
Expand Down