Adjusted save logic

This commit is contained in:
Luke Hagar
2024-05-29 11:45:47 -07:00
parent 488ace6354
commit 60aae717ce
5 changed files with 42 additions and 20 deletions

View File

@@ -0,0 +1,12 @@
<script lang="ts">
import { setSpec, type APISpec } from '$lib/db';
export let spec: APISpec;
function onLoad(e: Event): void {
console.log('load button clicked');
setSpec(spec);
}
</script>
<button class="btn variant-ghost-warning" on:click={onLoad}> Load </button>

View File

@@ -0,0 +1,19 @@
<script lang="ts">
import { openApiStore } from '$lib';
import { db, selectedSpec } from '$lib/db';
function onSave(e: Event): void {
console.log('Save button clicked');
if (!$selectedSpec) {
$selectedSpec = {
name: 'New OpenAPI Spec',
spec: $openApiStore
};
}
console.log($selectedSpec);
$selectedSpec.id = undefined;
db.apiSpecs.put($selectedSpec);
}
</script>
<button class="btn variant-ghost-success" on:click={onSave}> Save New </button>

View File

@@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { openApiStore } from '$lib'; import { openApiStore } from '$lib';
import { db, selectedSpec } from '$lib/db'; import { db, selectedSpec, setSpec } from '$lib/db';
import type { OpenAPIV3_1 } from '$lib/openAPITypes'; import type { OpenAPIV3_1 } from '$lib/openAPITypes';
import { import {
FileButton, FileButton,
@@ -29,11 +29,7 @@
} else { } else {
content = parse(result); content = parse(result);
} }
openApiStore.set(content); setSpec({ name: file.name, spec: content });
selectedSpec.set({
name: file.name,
spec: content
});
} catch (error) { } catch (error) {
console.error(`Error parsing ${isJson ? 'json' : 'yaml'} file`, error); console.error(`Error parsing ${isJson ? 'json' : 'yaml'} file`, error);
} }

View File

@@ -5,19 +5,10 @@ import { writable, type Writable } from "svelte/store";
export const selectedSpec: Writable<APISpec | undefined> = writable(undefined) export const selectedSpec: Writable<APISpec | undefined> = writable(undefined)
openApiStore.subscribe((value) => { export const setSpec = (spec: APISpec) => {
if (selectedSpec) { selectedSpec.set(spec);
selectedSpec.update((v) => { openApiStore.set(spec.spec);
if (v) {
return {
...v,
spec: value
} }
}
return v;
})
}
});
export interface APISpec { export interface APISpec {
id?: string; id?: string;

View File

@@ -1,7 +1,9 @@
<script lang="ts"> <script lang="ts">
import CreateNewButton from '$lib/components/FileManagement/CreateNewButton.svelte'; import CreateNewButton from '$lib/components/FileManagement/CreateNewButton.svelte';
import DeleteButton from '$lib/components/FileManagement/DeleteButton.svelte'; import DeleteButton from '$lib/components/FileManagement/DeleteButton.svelte';
import LoadButton from '$lib/components/FileManagement/LoadButton.svelte';
import SaveButton from '$lib/components/FileManagement/SaveButton.svelte'; import SaveButton from '$lib/components/FileManagement/SaveButton.svelte';
import SaveNewButton from '$lib/components/FileManagement/SaveNewButton.svelte';
import Upload from '$lib/components/FileManagement/Upload.svelte'; import Upload from '$lib/components/FileManagement/Upload.svelte';
import { db, selectedSpec } from '$lib/db'; import { db, selectedSpec } from '$lib/db';
import { liveQuery } from 'dexie'; import { liveQuery } from 'dexie';
@@ -26,6 +28,7 @@
<td>{spec.id}</td> <td>{spec.id}</td>
<td>{spec.name}</td> <td>{spec.name}</td>
<td> <td>
<LoadButton {spec} />
<DeleteButton {spec} /> <DeleteButton {spec} />
</td> </td>
</tr> </tr>
@@ -33,7 +36,7 @@
{/if} {/if}
</tbody> </tbody>
</table> </table>
{#if $selectedSpec?.name} {#if $selectedSpec}
<input <input
class="input" class="input"
bind:value={$selectedSpec.name} bind:value={$selectedSpec.name}
@@ -42,6 +45,7 @@
/> />
{/if} {/if}
<SaveButton /> <SaveButton />
<SaveNewButton />
<CreateNewButton /> <CreateNewButton />
<Upload /> <Upload />
</div> </div>