insert history entry when forking

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

@ -10,7 +10,7 @@
<script> <script>
import Repl from '@sveltejs/svelte-repl'; import Repl from '@sveltejs/svelte-repl';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { goto } from '@sapper/app';
import { process_example } from '../../../utils/examples'; import { process_example } from '../../../utils/examples';
import InputOutputToggle from '../../../components/Repl/InputOutputToggle.svelte'; import InputOutputToggle from '../../../components/Repl/InputOutputToggle.svelte';
import AppControls from './_components/AppControls/index.svelte'; import AppControls from './_components/AppControls/index.svelte';
@ -26,7 +26,7 @@
let width = process.browser ? window.innerWidth : 1000; let width = process.browser ? window.innerWidth : 1000;
let checked = false; let checked = false;
$: if (typeof history !== 'undefined') { function update_query_string(version) {
const params = []; const params = [];
if (version !== 'latest') params.push(`version=${version}`); if (version !== 'latest') params.push(`version=${version}`);
@ -38,17 +38,15 @@
history.replaceState({}, 'x', url); history.replaceState({}, 'x', url);
} }
onMount(() => { $: if (typeof history !== 'undefined') update_query_string(version);
if (version !== 'local') {
fetch(`https://unpkg.com/svelte@${version || '3'}/package.json`) function fetch_gist(id) {
.then(r => r.json()) if (gist && gist.uid === id) {
.then(pkg => { // if the id changed because we just forked, don't refetch
version = pkg.version; return;
});
} }
// TODO handle `relaxed` logic // TODO handle `relaxed` logic
fetch(`repl/${id}.json`).then(r => { fetch(`repl/${id}.json`).then(r => {
if (r.ok) { if (r.ok) {
r.json().then(data => { r.json().then(data => {
@ -77,12 +75,24 @@
console.warn('TODO: 404 Gist') console.warn('TODO: 404 Gist')
} }
}); });
}
$: if (process.browser) fetch_gist(id);
onMount(() => {
if (version !== 'local') {
fetch(`https://unpkg.com/svelte@${version || '3'}/package.json`)
.then(r => r.json())
.then(pkg => {
version = pkg.version;
});
}
}); });
function handle_fork(event) { function handle_fork(event) {
console.log('> handle_fork', event); console.log('> handle_fork', event);
gist = event.detail.gist; gist = event.detail.gist;
id = gist.uid; goto(`/repl/${gist.uid}?version=${version}`);
} }
$: svelteUrl = process.browser && version === 'local' ? $: svelteUrl = process.browser && version === 'local' ?

@ -10,8 +10,6 @@ export async function post(req, res) {
try { try {
const { name, files } = await body(req); const { name, files } = await body(req);
console.log({ name, files });
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, JSON.stringify(files)]); values ($1, $2, $3) returning *`, [user.id, name, JSON.stringify(files)]);

Loading…
Cancel
Save