feat: create function logic

This commit is contained in:
Arman committed 2022-09-27 17:36:53 +02:00
1 parent 6b257e99ed
commit b8f33f7e95
5 files changed
+148 -24

No files matched your search

-1
View File
@@ -9,7 +9,6 @@
export let href: string = null;
export let fullWidth = false;
export let ariaLabel: string = null;
//TODO: add option to add aria-label to buttons that are only icons
</script>
{#if href}
@@ -1,28 +1,21 @@
<script lang="ts">
import { Button, Form } from '$lib/elements/forms';
import { Modal } from '$lib/components';
import { createEventDispatcher } from 'svelte';
import { addNotification } from '$lib/stores/notifications';
import { Pill } from '$lib/elements';
import { createFunction } from './wizard/store';
export let showCreate = false;
let selectedService: typeof services[0] = null;
let selectedRequest: any;
let selectedRequest: typeof selectedService['requests'][0] = null;
let selectedEvent: string = null;
let selectedAttribute: string = null;
let showInfo: string = null;
const dispatch = createEventDispatcher();
const create = async () => {
try {
showCreate = false;
dispatch('created');
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
const create = () => {
showCreate = false;
const data = eventString.map((d) => d.value).join('.');
$createFunction.events.push(data);
$createFunction = $createFunction;
};
const events = ['create', 'update', 'delete'];
@@ -65,6 +58,51 @@
attributes: ['email', 'name', 'password', 'status', 'prefs']
}
];
//TODO: add types
function createEventString(service, request, event, attribute) {
let data = [];
//SERVICE
if (service) {
data.push({ value: service.name, description: 'service' });
data.push({ value: '*', description: `event` });
}
//REQUEST
if (request) {
data.push({ value: request.name, description: 'request' });
data[1].description = `ID of ${service?.name.slice(0, -1)}`;
data.push({ value: '*', description: `event` });
}
//EVENT
if ((event && request?.events?.includes(event)) ?? service?.events?.includes(event)) {
data[data.length - 1] = { value: event, description: 'event' };
} else if (service) {
data[data.length - 1] = { value: '*', description: 'event' };
}
//ATTRIBUTE
if (attribute && !request) {
data.push({ value: attribute, description: 'attribute' });
}
return data;
}
$: eventString = createEventString(
selectedService,
selectedRequest,
selectedEvent,
selectedAttribute
);
$: if (selectedService) {
selectedRequest = null;
selectedEvent = null;
selectedAttribute = null;
}
$: console.log(eventString);
</script>
<Form on:submit={create}>
@@ -130,7 +168,19 @@
</div>
</div>
{/if}
{selectedService?.name}.{selectedRequest?.name}.{selectedEvent}
<div class="u-flex">
{#each eventString as route, i}
<button
on:click|preventDefault={() => {
showInfo === route.description
? (showInfo = null)
: (showInfo = route.description);
}}>{route.value}{i + 1 < eventString?.length ? '.' : ''}</button>
{/each}
</div>
{#if showInfo}
<span>{showInfo}</span>
{/if}
{/if}
<svelte:fragment slot="footer">
@@ -26,7 +26,7 @@
$createFunction.timeout
);
addNotification({
message: 'Document has been created',
message: 'Function has been created',
type: 'success'
});
functionList.load('', 12, 0);
@@ -0,0 +1,37 @@
<script lang="ts">
import { Button, Form } from '$lib/elements/forms';
import { Modal } from '$lib/components';
import { InputText, InputPassword, FormList } from '$lib/elements/forms';
import { createFunction } from './wizard/store';
export let showCreate = false;
export let key: string = null;
let value = $createFunction.vars[key];
const create = async () => {
showCreate = false;
$createFunction.vars[key] = value;
};
</script>
<Form on:submit={create}>
<Modal bind:show={showCreate} size="big">
<svelte:fragment slot="header">Create Variable</svelte:fragment>
<FormList>
<InputText id="key" label="Key" placeholder="Enter key" bind:value={key} required />
<InputPassword
id="value"
label="Value"
minlength={0}
showPasswordButton
placeholder="Enter value"
bind:value
required />
</FormList>
<svelte:fragment slot="footer">
<Button secondary on:click={() => (showCreate = false)}>Cancel</Button>
<Button submit>Create</Button>
</svelte:fragment>
</Modal>
</Form>
@@ -1,6 +1,13 @@
<script lang="ts">
import { WizardStep } from '$lib/layout';
import { createFunction } from './store';
import Create from '../createVariable.svelte';
import { DropList, DropListItem } from '$lib/components';
let showCreate = false;
let selectedKey: string = null;
let showDropdown = [];
</script>
<WizardStep>
@@ -23,23 +30,52 @@
</thead>
<tbody class="table-tbody">
{#if $createFunction.vars}
{#each Object.entries($createFunction.vars) as [key, value]}
{#each Object.entries($createFunction.vars) as [key, value], i}
<tr class="table-row">
<td class="table-col" data-title="Key">
<span class="text">{key}</span>
</td>
<td class="table-col u-overflow-visible" data-title="value">
<div class="transparent-password-input">
<button class="" aria-label="show password">
<button
on:click|preventDefault={() =>
(showDropdown[i] = !showDropdown[i])}
aria-label="show password">
<span class="icon-eye" aria-hidden="true" />
</button>
<input type="password" class="input-text" {value} />
</div>
</td>
<td class="table-col u-overflow-visible" data-title="options">
<button class="button is-text is-only-icon" aria-label="more options">
<span class="icon-dots-horizontal" aria-hidden="true" />
</button>
<DropList
bind:show={showDropdown[i]}
position="bottom"
horizontal="left"
arrow={false}>
<button
class="button is-text is-only-icon"
aria-label="more options"
on:click|preventDefault={() => (showDropdown[i] = true)}>
<span class="icon-dots-horizontal" aria-hidden="true" />
</button>
<svelte:fragment slot="list">
<DropListItem
icon="pencil"
on:click={() => {
selectedKey = key;
showCreate = true;
}}>
Edit
</DropListItem>
<DropListItem
icon="trash"
on:click={() => {
delete $createFunction.vars[key];
}}>
Delete
</DropListItem>
</svelte:fragment>
</DropList>
</td>
</tr>
{/each}
@@ -51,10 +87,12 @@
class="button is-text u-padding-inline-0"
type="button"
on:click={() => {
$createFunction.vars[''] = '';
showCreate = true;
}}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Create variable</span>
</button>
</div>
</WizardStep>
<Create key={selectedKey} {showCreate} />