Finish template wizard

This commit is contained in:
Matej Bačo committed 2023-08-04 10:55:10 +02:00
1 parent 5304af17a5
commit cd583f44f7
10 files changed
+318 -82

No files matched your search

@@ -31,7 +31,17 @@
let search = '';
async function loadRepositories(installationId: string, search: string) {
return sdk.forProject.vcs.listRepositories(installationId, search || undefined);
const repositories = await sdk.forProject.vcs.listRepositories(
installationId,
search || undefined
);
if (repositories.providerRepositories.length) {
selectedRepository = repositories.providerRepositories[0].id;
$repository = repositories.providerRepositories[0];
}
return repositories;
}
function connectGitHub() {
@@ -104,7 +114,7 @@
<input
class="is-small u-margin-inline-end-8"
type="radio"
name={repo.name}
name="repositories"
bind:group={selectedRepository}
on:change={() => repository.set(repo)}
value={repo.id} />
+10 -1
View File
@@ -38,7 +38,16 @@
}
templateStore.set(template);
templateConfig.set({ $id: null, runtime, name: template.name, variables });
templateConfig.set({
$id: null,
runtime,
name: template.name,
variables,
repositoryBehaviour: 'new',
repositoryName: template.id,
repositoryPrivate: false,
repositoryId: null
});
wizard.start(CreateTemplate);
}
+26 -27
View File
@@ -5,49 +5,48 @@
import { sdk } from '$lib/stores/sdk';
import { wizard } from '$lib/stores/wizard';
import { goto } from '$app/navigation';
import {
choices,
createFunction,
installation,
repository,
template,
templateConfig
} from './store';
import { choices, installation, repository, template, templateConfig } from './store';
import { addNotification } from '$lib/stores/notifications';
import { Submit, trackEvent } from '$lib/actions/analytics';
import { base } from '$app/paths';
import { page } from '$app/stores';
import SelectRepository from './steps/selectRepository.svelte';
import GitConfiguration from './steps/gitConfiguration.svelte';
import TemplateConfiguration from './steps/templateConfiguration.svelte';
import TempalteFunctionDetails from './steps/tempalteFunctionDetails.svelte';
import RepositoryBehaviour from './steps/repositoryBehaviour.svelte';
import CreateRepository from './steps/createRepository.svelte';
async function create() {
const runtimeDetail = $template.runtimes.find((r) => r.name === $templateConfig.runtime);
const response = await sdk.forProject.functions.create(
$createFunction.$id || ID.unique(),
$createFunction.name,
$createFunction.runtime,
$createFunction.execute || undefined,
$createFunction.entrypoint,
$templateConfig.$id || ID.unique(),
$templateConfig.name,
$templateConfig.runtime,
$template.permissions || undefined,
runtimeDetail.entrypoint,
$template.events || undefined,
$template.cron || undefined,
$template.timeout || undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
$createFunction.commands,
runtimeDetail.commands,
$installation.$id,
$repository.id,
$choices.branch,
$choices.silentMode,
$choices.rootDir
$choices.rootDir,
$template.providerRepositoryId,
$template.providerOwner,
runtimeDetail.providerRootDirectory,
$template.providerBranch
);
goto(`${base}/console/project-${$page.params.project}/functions/function-${response.$id}`);
addNotification({
message: `${$createFunction.name} has been created`,
message: `${response.name} has been created`,
type: 'success'
});
trackEvent(Submit.FunctionCreate, {
customId: !!$createFunction.$id
customId: !!response.$id
});
resetState();
wizard.hide();
@@ -61,16 +60,16 @@
const stepsComponents: WizardStepsType = new Map();
stepsComponents.set(1, {
label: 'Template Configuration',
label: 'Function configuration',
component: TemplateConfiguration
});
stepsComponents.set(2, {
label: 'Function Details',
component: TempalteFunctionDetails
label: 'Repository behaviour',
component: RepositoryBehaviour
});
stepsComponents.set(3, {
label: 'Select Repository',
component: SelectRepository
label: 'Select repository',
component: CreateRepository
});
stepsComponents.set(4, {
label: 'Git configuration',
@@ -0,0 +1,151 @@
<script lang="ts">
import { page } from '$app/stores';
import Button from '$lib/elements/forms/button.svelte';
import FormList from '$lib/elements/forms/formList.svelte';
import InputChoice from '$lib/elements/forms/inputChoice.svelte';
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
import InputText from '$lib/elements/forms/inputText.svelte';
import { WizardStep } from '$lib/layout';
import { sdk } from '$lib/stores/sdk';
import Repositories from '../components/repositories.svelte';
import { installation, repository, templateConfig } from '../store';
let selectedInstallationId;
let hasInstallations;
let selectedRepository;
async function beforeSubmit() {
if (!hasInstallations || !$installation) {
throw new Error('Please connect a Git provider');
}
if ($templateConfig.repositoryBehaviour === 'new') {
const repo = await sdk.forProject.vcs.createRepository(
$installation.$id,
$templateConfig.repositoryName,
$templateConfig.repositoryPrivate
);
$repository = repo;
}
if (!$repository) {
throw new Error('Please select a repository');
}
}
function connectGitHub() {
const redirect = new URL($page.url);
redirect.searchParams.append('github-installed', 'true');
const target = new URL(`${sdk.forProject.client.config.endpoint}/v1/vcs/github/authorize`);
target.searchParams.set('projectId', $page.params.project);
target.searchParams.set('redirect', redirect.toString());
return target;
}
async function loadInstallations() {
const { installations } = await sdk.forProject.vcs.listInstallations();
if (installations.length) {
$installation = installations[0];
hasInstallations = true;
}
return installations;
}
function getProviderIcon(provider: string) {
switch (provider) {
case 'github':
return 'icon-github';
default:
return '';
}
}
</script>
<WizardStep {beforeSubmit}>
<svelte:fragment slot="title">Select repository</svelte:fragment>
<svelte:fragment slot="subtitle">
Select a Git repository that will trigger your function deployments when updated.
</svelte:fragment>
{#if $templateConfig.repositoryBehaviour === 'existing'}
<Repositories bind:hasInstallations bind:selectedRepository />
{:else}
{#await loadInstallations()}
<div class="u-flex u-gap-8 u-cross-center u-main-center">
<div class="loader u-margin-32" />
</div>
{:then installations}
{#if hasInstallations}
<div class="u-flex u-gap-16">
<div class="u-width-full-line">
<InputSelect
id="installation"
label="Git organization"
options={installations.map((entry) => {
return {
label: entry.organization,
value: entry.$id
};
})}
on:change={() => {
$installation = installations.find(
(entry) => entry.$id === selectedInstallationId
);
}}
bind:value={selectedInstallationId} />
</div>
</div>
{:else}
<div class="u-flex u-cross-center u-flex-vertical u-gap-16">
<Button href={connectGitHub().toString()} fullWidth secondary>
<span class="icon-github" aria-hidden="true" />
<span class="text">Continue with GitHub</span>
</Button>
<Button disabled fullWidth secondary>
<span class="icon-gitlab" aria-hidden="true" />
<span class="text">GitLab (coming soon)</span>
</Button>
<Button disabled fullWidth secondary>
<span class="icon-bitBucket" aria-hidden="true" />
<span class="text">BitBucket (coming soon)</span>
</Button>
<Button disabled fullWidth secondary>
<span class="icon-azure" aria-hidden="true" />
<span class="text">Azure (coming soon)</span>
</Button>
</div>
{/if}
{/await}
{#if $installation}
<div
class="box u-margin-block-start-20"
style:--box-border-radius="var(--border-radius-small)">
<div class="u-flex u-gap-16">
<div class="avatar is-size-x-small">
<span class={getProviderIcon($installation.provider)} />
</div>
<div class="u-cross-child-center u-line-height-1-5">
<h6 class="u-bold u-trim-1">
{$installation.organization}/{$templateConfig.repositoryName}
</h6>
</div>
</div>
<div class="u-margin-block-start-24">
<FormList>
<InputText
id="repositoryName"
label="Repository name"
placeholder="my-repository"
bind:value={$templateConfig.repositoryName} />
<InputChoice
id="repositoryPrivate"
label="Keep repository private"
bind:value={$templateConfig.repositoryPrivate} />
</FormList>
</div>
</div>
{/if}
{/if}
</WizardStep>
@@ -1,5 +1,6 @@
<script lang="ts">
import { FormList, InputChoice, InputSelect, InputText } from '$lib/elements/forms';
import { FormList, InputChoice, InputText } from '$lib/elements/forms';
import InputSelectSearch from '$lib/elements/forms/inputSelectSearch.svelte';
import { WizardStep } from '$lib/layout';
import { sdk } from '$lib/stores/sdk';
import { choices, installation, repository } from '../store';
@@ -26,7 +27,11 @@
return a.name > b.name ? -1 : 1;
});
$choices.branch = sorted[0].name ?? null;
$choices.branch = sorted[0]?.name ?? null;
if (!$choices.branch) {
$choices.branch = 'main';
}
return sorted;
}
@@ -61,16 +66,22 @@
{:then branches}
<div class="u-margin-block-start-24">
<FormList>
<InputSelect
<InputSelectSearch
id="branch"
label="Branch"
label="Select branch"
placeholder="main"
bind:value={$choices.branch}
bind:search={$choices.branch}
on:select={(event) => {
$choices.branch = event.detail.value;
}}
name="branch"
options={branches?.map((branch) => {
return {
value: branch.name,
label: branch.name
};
}) ?? []}
bind:value={$choices.branch} />
}) ?? []} />
<InputText
id="root"
label="Root directory"
@@ -0,0 +1,63 @@
<script lang="ts">
import { WizardStep } from '$lib/layout';
import { templateConfig } from '../store';
async function beforeSubmit() {
if (!$templateConfig.repositoryBehaviour) {
throw new Error('Please select repository behaviour.');
}
}
</script>
<WizardStep {beforeSubmit}>
<svelte:fragment slot="title">Repository Behaviour</svelte:fragment>
<svelte:fragment slot="subtitle">
Connect function to a new repository or to an existing one within a selected Git
organization.
</svelte:fragment>
<ul class="u-flex u-flex-vertical u-gap-24">
<li>
<label
class="card is-allow-focus u-cursor-pointer"
style="--card-padding:1rem; --card-border-radius:var(--border-radius-small);">
<div class="u-flex u-gap-16">
<input
bind:group={$templateConfig.repositoryBehaviour}
class="is-small u-margin-block-start-2"
value="new"
type="radio"
name="repositoryBehaviour" />
<div class="u-flex u-flex-vertical u-gap-4">
<h4 class="body-text-2 u-bold">Create new repository</h4>
<p class="u-color-text-gray">
Clone the template and create a new repository in your selected
organization.
</p>
</div>
</div>
</label>
</li>
<li>
<label
class="card is-allow-focus u-cursor-pointer"
style="--card-padding:1rem; --card-border-radius:var(--border-radius-small);">
<div class="u-flex u-gap-16">
<input
bind:group={$templateConfig.repositoryBehaviour}
class="is-small u-margin-block-start-2"
value="existing"
type="radio"
name="repositoryBehaviour" />
<div class="u-flex u-flex-vertical u-gap-4">
<h4 class="body-text-2 u-bold">Add to existing repository</h4>
<p class="u-color-text-gray">
Clone the template to an existing repository in your selected
organization.
</p>
</div>
</div>
</label>
</li>
</ul>
</WizardStep>
@@ -1,33 +0,0 @@
<script lang="ts">
import { CustomId } from '$lib/components';
import { Pill } from '$lib/elements';
import { FormList, InputText } from '$lib/elements/forms';
import { WizardStep } from '$lib/layout';
import { templateConfig } from '../store';
let showCustomId = false;
</script>
<WizardStep>
<svelte:fragment slot="title">Function details</svelte:fragment>
<svelte:fragment slot="subtitle"
>Configure Appwrite Function you are about to create.</svelte:fragment>
<FormList>
<InputText
label="Name"
id="name"
placeholder="Function name"
bind:value={$templateConfig.name}
required />
{#if !showCustomId}
<div>
<Pill button on:click={() => (showCustomId = !showCustomId)}>
<span class="icon-pencil" aria-hidden="true" />
<span class="text">Function ID </span>
</Pill>
</div>
{:else}
<CustomId bind:show={showCustomId} name="Function" bind:id={$templateConfig.$id} />
{/if}
</FormList>
</WizardStep>
@@ -1,21 +1,24 @@
<script lang="ts">
import Alert from '$lib/components/alert.svelte';
import Collapsible from '$lib/components/collapsible.svelte';
import CollapsibleItem from '$lib/components/collapsibleItem.svelte';
import CustomId from '$lib/components/customId.svelte';
import FormList from '$lib/elements/forms/formList.svelte';
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
import InputText from '$lib/elements/forms/inputText.svelte';
import Pill from '$lib/elements/pill.svelte';
import { WizardStep } from '$lib/layout';
import { sdk } from '$lib/stores/sdk';
import { template, templateConfig } from '../store';
let showCustomId = false;
$: runtimeDetail =
$templateConfig.runtime &&
$template.runtimes.find((r) => r.name === $templateConfig.runtime);
async function beforeSubmit() {
if (!$templateConfig.runtime) {
throw new Error('Please seelct a runtime.');
throw new Error('Please select a runtime.');
}
for (const variable of $template.variables) {
@@ -51,6 +54,25 @@
{$template.tagline}
</svelte:fragment>
<div class="u-flex u-flex-vertical u-gap-24">
<FormList>
<InputText
label="Name"
id="name"
placeholder="Function name"
bind:value={$templateConfig.name}
required />
{#if !showCustomId}
<div>
<Pill button on:click={() => (showCustomId = !showCustomId)}>
<span class="icon-pencil" aria-hidden="true" />
<span class="text">Function ID</span>
</Pill>
</div>
{:else}
<CustomId bind:show={showCustomId} name="Function" bind:id={$templateConfig.$id} />
{/if}
</FormList>
{#await loadRuntimes()}
<div class="avatar is-size-x-small">
<div class="loader u-margin-16" />
@@ -65,11 +87,10 @@
required />
{/await}
<div class="u-flex u-flex-vertical u-gap-16">
<p class="text u-bold">Environment variables</p>
{#if $template.variables.length <= 0}
<p>No variables to configure.</p>
{:else}
{#if $template.variables.length > 0}
<div class="u-flex u-flex-vertical u-gap-16">
<p class="text u-bold">Environment variables</p>
<div class="u-flex u-flex-vertical u-gap-16">
{#each $template.variables as variable}
<div>
@@ -81,15 +102,16 @@
autocomplete={false}
bind:value={$templateConfig.variables[variable.name]} />
<div class="u-margin-block-start-4">
<Alert type="info" isInline={true}>
{@html variable.description}
</Alert>
<p class="helper">
<span class="icon-info" aria-hidden="true" />
<span class="text">{@html variable.description}</span>
</p>
</div>
</div>
{/each}
</div>
{/if}
</div>
</div>
{/if}
<Collapsible>
<CollapsibleItem>
+4
View File
@@ -9,6 +9,10 @@ export const templateConfig = writable<{
name: string;
runtime: string;
variables: { [key: string]: string };
repositoryBehaviour: 'new' | 'existing';
repositoryName: string;
repositoryPrivate: boolean;
repositoryId: string;
}>();
export const repository = writable<Models.ProviderRepository>();
export const installation = writable<Models.Installation>();
@@ -156,7 +156,7 @@
<input
class="is-small u-margin-inline-end-8"
type="radio"
name={repo.name}
name="repositories"
bind:group={selectedRepoId}
value={repo.id} />
<div class="avatar is-size-x-small">