implement saving and forking

pull/2680/head
Richard Harris 6 years ago
parent a4f79544b4
commit fd1b4448cb

@ -59,7 +59,10 @@
headers: { Authorization }, headers: { Authorization },
body: JSON.stringify({ body: JSON.stringify({
name, name,
components files: components.map(component => ({
name: `${component.name}.${component.type}`,
source: component.source
}))
}) })
}); });
@ -107,16 +110,16 @@
const files = {}; const files = {};
const { components } = repl.toJSON(); const { components } = repl.toJSON();
components.forEach(module => {
const text = module.source.trim();
if (!text.length) return; // skip empty file
files[`${module.name}.${module.type}`] = text;
});
const r = await fetch(`repl/${gist.uid}.json`, { const r = await fetch(`repl/${gist.uid}.json`, {
method: 'PATCH', method: 'PATCH',
headers: { Authorization }, headers: { Authorization },
body: JSON.stringify({ name, files }) body: JSON.stringify({
name,
files: components.map(component => ({
name: `${component.name}.${component.type}`,
source: component.source
}))
})
}); });
if (r.status < 200 || r.status >= 300) { if (r.status < 200 || r.status >= 300) {
@ -197,29 +200,21 @@ export default app;` });
<Icon name="download" /> <Icon name="download" />
</button> </button>
{#if $user} <button class="icon" disabled="{saving || !$user}" on:click={() => fork(false)} title="fork">
<button class="icon" disabled="{saving || !$user}" on:click={fork} title="fork"> {#if justForked}
{#if justForked} <Icon name="check" />
<Icon name="check" /> {:else}
{:else} <Icon name="git-branch" />
<Icon name="git-branch" /> {/if}
{/if} </button>
</button>
<button class="icon" disabled="{saving || !$user}" on:click={save} title="save">
{#if justSaved}
<Icon name="check" />
{:else}
<Icon name="save" />
{/if}
</button>
{/if}
{#if gist} <button class="icon" disabled="{saving || !$user}" on:click={save} title="save">
<a class="icon no-underline" href={gist.html_url} title="link to gist"> {#if justSaved}
<Icon name="link" /> <Icon name="check" />
</a> {:else}
{/if} <Icon name="save" />
{/if}
</button>
{#if $user} {#if $user}
<UserMenu /> <UserMenu />

@ -113,7 +113,7 @@ export async function patch(req, res) {
let k, cols=[], vals=[]; let k, cols=[], vals=[];
for (k in obj) { for (k in obj) {
cols.push(k); cols.push(k);
vals.push(obj[k]); vals.push(k === 'files' ? JSON.stringify(obj[k]) : obj[k]);
} }
const tmp = vals.map((x, i) => `$${i + 1}`).join(','); const tmp = vals.map((x, i) => `$${i + 1}`).join(',');

@ -80,10 +80,9 @@
}); });
function handle_fork(event) { function handle_fork(event) {
example = null;
console.log('> handle_fork', event); console.log('> handle_fork', event);
gist = event.detail.gist; gist = event.detail.gist;
gist_id = gist.uid; id = gist.uid;
} }
$: svelteUrl = process.browser && version === 'local' ? $: svelteUrl = process.browser && version === 'local' ?

@ -8,21 +8,16 @@ export async function post(req, res) {
if (!user) return; // response already sent if (!user) return; // response already sent
try { try {
const { name, components } = await body(req); const { name, files } = await body(req);
const files = {}; console.log({ name, files });
components.forEach(component => {
const text = component.source.trim();
if (!text.length) return; // skip empty file
files[`${component.name}.${component.type}`] = text;
});
const [row] = await query(` const [row] = await query(`
insert into gists(user_id, name, files) insert into gists(user_id, name, files)
values ($1, $2, $3) returning *`, [user.id, name, files]); values ($1, $2, $3) returning *`, [user.id, name, JSON.stringify(files)]);
send(res, 201, { send(res, 201, {
uid: row.uid, uid: row.uid.replace(/-/g, ''),
name: row.name, name: row.name,
files: row.files, files: row.files,
owner: user.uid, owner: user.uid,

Loading…
Cancel
Save