implement saving and forking

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

@ -59,7 +59,10 @@
headers: { Authorization },
body: JSON.stringify({
name,
components
files: components.map(component => ({
name: `${component.name}.${component.type}`,
source: component.source
}))
})
});
@ -107,16 +110,16 @@
const files = {};
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`, {
method: 'PATCH',
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) {
@ -197,29 +200,21 @@ export default app;` });
<Icon name="download" />
</button>
{#if $user}
<button class="icon" disabled="{saving || !$user}" on:click={fork} title="fork">
{#if justForked}
<Icon name="check" />
{:else}
<Icon name="git-branch" />
{/if}
</button>
<button class="icon" disabled="{saving || !$user}" on:click={save} title="save">
{#if justSaved}
<Icon name="check" />
{:else}
<Icon name="save" />
{/if}
</button>
{/if}
<button class="icon" disabled="{saving || !$user}" on:click={() => fork(false)} title="fork">
{#if justForked}
<Icon name="check" />
{:else}
<Icon name="git-branch" />
{/if}
</button>
{#if gist}
<a class="icon no-underline" href={gist.html_url} title="link to gist">
<Icon name="link" />
</a>
{/if}
<button class="icon" disabled="{saving || !$user}" on:click={save} title="save">
{#if justSaved}
<Icon name="check" />
{:else}
<Icon name="save" />
{/if}
</button>
{#if $user}
<UserMenu />

@ -113,7 +113,7 @@ export async function patch(req, res) {
let k, cols=[], vals=[];
for (k in obj) {
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(',');

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

@ -8,21 +8,16 @@ export async function post(req, res) {
if (!user) return; // response already sent
try {
const { name, components } = await body(req);
const { name, files } = await body(req);
const files = {};
components.forEach(component => {
const text = component.source.trim();
if (!text.length) return; // skip empty file
files[`${component.name}.${component.type}`] = text;
});
console.log({ name, files });
const [row] = await query(`
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, {
uid: row.uid,
uid: row.uid.replace(/-/g, ''),
name: row.name,
files: row.files,
owner: user.uid,

Loading…
Cancel
Save