Skip to content
Closed
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
40 changes: 38 additions & 2 deletions components/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,49 @@ function EditPage({ source: initialDocument }) {
const [doc, setDoc] = React.useState(initialDocument);
const { content, config, errors } = useMarkdocCode(doc);

const [docChanged, setDocChanged] = React.useState(false);

const [prNumber, setPrNumber] = React.useState(null);

const createPR = async () => {
const filename = window.CURRENT_FILENAME;

const files = {
[filename]: doc
};

console.log(files);

const body = JSON.stringify({ files });

const response = await fetch('/api/create-pr', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should just call this:

Suggested change
const response = await fetch('/api/create-pr', {
const response = await fetch('/api/pr', {

and POST to it.

method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body
});

const prNumber = await response.json();
setPrNumber(prNumber);

return prNumber;
};

return (
<>
{Markdoc.renderers.react(content.children, React, {
components: config.components
})}
<EditPagePanel>
<Editor code={doc} onChange={setDoc} errors={errors} />
<EditPagePanel docChanged={docChanged} createPR={createPR}>
<Editor
code={doc}
onChange={(...args) => {
setDocChanged(true);
setDoc(...args);
}}
errors={errors}
/>
</EditPagePanel>
</>
);
Expand Down
46 changes: 45 additions & 1 deletion components/EditPagePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,49 @@ import React from 'react';
const PATTERN = Buffer.from('NDI0Mg==', 'base64').toString();
const WIDTH = 55;

export function EditPagePanel({ children }) {
const MARKDOC_CLIENT_ID = '3a8716adbaa6294da1ec';
function CreatePRButton({ createPR }) {
const [loading, setLoading] = React.useState(false);

return (
<div>
<button
className="create-pr-btn"
onClick={async () => {
// TODO: oauth is so annoying. We have to auth this and then get the code back, pass that to the server, but
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this comment more straight to the point?

// it makes the UX annoying
window.open(
`https://github.com/login/oauth/authorize?client_id=${MARKDOC_CLIENT_ID}&redirect_uri=${window.location.href}`
);
setLoading(true);
const prNumber = await createPR();
window.open(
`https://github.com/markdoc/docs/pull/${prNumber}`,
'_blank'
);
setLoading(false);
}}
>
{loading ? 'Creating...' : 'Create PR'}
</button>
<style jsx>
{`
.create-pr-btn {
position: absolute;
bottom: 0.8rem;
right: 1rem;
top: inherit;
background: #ffd849;
font-size: 12px;
padding: 5px 12px;
}
`}
</style>
</div>
);
}

export function EditPagePanel({ docChanged, children, createPR }) {
const [showEditor, setShowEditor] = React.useState(false);
const [mouseOver, setMouseOver] = React.useState(false);
const [keystrokes, setCount] = React.useState(0);
Expand Down Expand Up @@ -71,6 +113,7 @@ export function EditPagePanel({ children }) {
<button onClick={() => setShowEditor(false)}>
<kbd>CMD + J / Esc</kbd>
</button>
{docChanged && <CreatePRButton createPR={createPR} />}
</section>
<style jsx>
{`
Expand All @@ -88,6 +131,7 @@ export function EditPagePanel({ children }) {
width: ${100 - WIDTH}%;
height: 100%;
}

section {
width: ${WIDTH}%;
height: 100%;
Expand Down
Loading