1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-02-01 02:39:26 +00:00

152 lines
4.3 KiB
Svelte
Raw Normal View History

2023-01-10 17:28:27 +01:00
<script>
2023-01-29 20:00:15 +01:00
import { views } from '../../../stores';
2023-01-13 16:56:48 +01:00
import { createEventDispatcher } from 'svelte';
2023-01-17 09:50:15 +01:00
import { InsertItems } from '../../../../wailsjs/go/app/App';
2023-01-29 20:00:15 +01:00
import { input } from '../../../actions';
2023-01-20 13:54:57 +01:00
import Icon from '../../../components/icon.svelte';
2023-01-29 20:00:15 +01:00
import Form from './components/form.svelte';
import ObjectViewer from '../../../components/objectviewer.svelte';
2023-01-31 16:58:23 +01:00
import Grid from '../../../components/grid.svelte';
import { inputTypes, randomString } from '../../../utils';
2023-01-10 17:28:27 +01:00
export let collection;
2023-01-13 16:56:48 +01:00
const dispatch = createEventDispatcher();
let json = '';
2023-01-29 20:00:15 +01:00
let newItems = [];
2023-01-10 17:28:27 +01:00
let insertedIds;
2023-01-29 20:00:15 +01:00
let objectViewerData = '';
let viewType = 'form';
let formValid = false;
$: viewsForCollection = views.forCollection(collection.hostKey, collection.dbKey, collection.key);
$: oppositeViewType = viewType === 'table' ? 'form' : 'table';
2023-01-10 17:28:27 +01:00
2023-01-29 20:16:31 +01:00
$: if (collection.viewKey !== 'list') {
json = JSON.stringify(newItems, undefined, 2);
}
2023-01-10 17:28:27 +01:00
async function insert() {
2023-01-29 20:16:31 +01:00
insertedIds = await InsertItems(collection.hostKey, collection.dbKey, collection.key, json);
if ((collection.viewKey === 'list') && insertedIds) {
newItems = [];
2023-01-29 20:00:15 +01:00
}
2023-01-13 16:56:48 +01:00
}
function showDocs() {
dispatch('performFind', {
query: insertedIds.length === 1
? `{ "_id": ${JSON.stringify(insertedIds[0])} }`
: `{ "_id": { "$in": [ ${insertedIds.map(id => JSON.stringify(id)).join(', ')} ] } }`,
});
2023-01-10 17:28:27 +01:00
}
2023-01-29 20:00:15 +01:00
function switchViewType() {
viewType = oppositeViewType;
}
function showJson() {
if (viewType === 'form') {
objectViewerData = { ...(newItems[0] || {}) };
}
else if (viewType === 'table') {
objectViewerData = [ ...newItems ];
}
}
2023-01-31 16:58:23 +01:00
function addRow() {
newItems = [ ...newItems, {} ];
}
2023-01-10 17:28:27 +01:00
</script>
<form on:submit|preventDefault={insert}>
2023-01-29 20:00:15 +01:00
{#if collection.viewKey === 'list'}
<label class="field">
<textarea
cols="30"
rows="10"
placeholder="[]"
class="code"
bind:value={json}
use:input={{ type: 'json', autofocus: true }}
></textarea>
</label>
2023-01-31 16:58:23 +01:00
{:else if viewType === 'form'}
2023-01-29 20:00:15 +01:00
<div class="form">
<Form bind:item={newItems[0]} bind:valid={formValid} view={$views[collection.viewKey]} />
</div>
2023-01-31 16:58:23 +01:00
{:else if viewType === 'table'}
<div class="table">
<Grid
key="id"
items={newItems}
columns={
$views[collection.viewKey]?.columns
?.filter(c => inputTypes.includes(c.inputType))
.map(c => ({ ...c, id: randomString(8), title: c.key })) || []
}
showHeaders={true}
canAddRows={true}
canSelect={false}
canRemoveItems={true}
hideChildrenToggles={true}
on:addRow={addRow}
bind:inputsValid={formValid}
/>
</div>
2023-01-29 20:00:15 +01:00
{/if}
2023-01-10 17:28:27 +01:00
<div class="flex">
<div>
{#if insertedIds}
2023-01-13 16:56:48 +01:00
<span class="flash-green">Success! {insertedIds.length} document{insertedIds.length > 1 ? 's' : ''} inserted</span>
{/if}
</div>
<div>
{#if insertedIds}
<button class="btn" type="button" on:click={showDocs}>View inserted docs</button>
2023-01-10 17:28:27 +01:00
{/if}
2023-01-29 20:00:15 +01:00
{#if collection.viewKey !== 'list'}
<button class="btn" type="button" on:click={showJson} title="Show JSON">
<Icon name="code" />
</button>
<button class="btn" type="button" on:click={switchViewType} title="Edit as {oppositeViewType}">
<Icon name={oppositeViewType} />
</button>
{/if}
<label class="field inline">
<select bind:value={collection.viewKey}>
{#each Object.entries(viewsForCollection) as [key, view]}
<option value={key}>{key === 'list' ? 'Raw JSON' : view.name}</option>
{/each}
</select>
<button class="btn" type="button" on:click={() => dispatch('openViewConfig')} title="Configure view">
<Icon name="cog" />
</button>
</label>
<button type="submit" class="btn" disabled={$views[collection.viewKey]?.type === 'list' ? !json : !formValid}>
2023-01-20 13:54:57 +01:00
<Icon name="+" /> Insert
</button>
2023-01-10 17:28:27 +01:00
</div>
</div>
</form>
2023-01-29 20:00:15 +01:00
<ObjectViewer data={objectViewerData} />
2023-01-10 17:28:27 +01:00
<style>
2023-01-13 16:56:48 +01:00
form {
display: grid;
grid-template-rows: 1fr auto;
gap: 0.5rem;
}
2023-01-31 16:58:23 +01:00
.table {
background-color: #fff;
border: 1px solid #ccc;
}
2023-01-10 17:28:27 +01:00
.flex {
display: flex;
justify-content: space-between;
}
</style>