fix: load previews async

This commit is contained in:
Andras Bacsai 2022-07-25 18:55:58 +00:00
parent cf140ff4cf
commit cef87d9671

View File

@ -1,12 +1,10 @@
<script context="module" lang="ts"> <script context="module" lang="ts">
import type { Load } from '@sveltejs/kit'; import type { Load } from '@sveltejs/kit';
export const load: Load = async ({ fetch, params, stuff, url }) => { export const load: Load = async ({ stuff, url }) => {
try { try {
const response = await get(`/applications/${params.id}/previews`);
return { return {
props: { props: {
application: stuff.application, application: stuff.application
...response
} }
}; };
} catch (error) { } catch (error) {
@ -19,10 +17,7 @@
</script> </script>
<script lang="ts"> <script lang="ts">
export let containers: any;
export let application: any; export let application: any;
export let PRMRSecrets: any;
export let applicationSecrets: any;
import Secret from './_Secret.svelte'; import Secret from './_Secret.svelte';
import { get, post } from '$lib/api'; import { get, post } from '$lib/api';
import { page } from '$app/stores'; import { page } from '$app/stores';
@ -31,8 +26,15 @@
import { t } from '$lib/translations'; import { t } from '$lib/translations';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { errorNotification, getDomain } from '$lib/common'; import { errorNotification, getDomain } from '$lib/common';
import { onMount } from 'svelte';
import Loading from '$lib/components/Loading.svelte';
const { id } = $page.params; const { id } = $page.params;
let containers: any;
let PRMRSecrets: any;
let applicationSecrets: any;
let loading = true;
async function refreshSecrets() { async function refreshSecrets() {
const data = await get(`/applications/${id}/secrets`); const data = await get(`/applications/${id}/secrets`);
PRMRSecrets = [...data.secrets]; PRMRSecrets = [...data.secrets];
@ -55,6 +57,19 @@
return errorNotification(error); return errorNotification(error);
} }
} }
onMount(async () => {
try {
loading = true;
const response = await get(`/applications/${id}/previews`);
containers = response.containers;
PRMRSecrets = response.PRMRSecrets;
applicationSecrets = response.applicationSecrets;
} catch (error) {
return errorNotification(error);
} finally {
loading = false;
}
});
</script> </script>
<div class="flex items-center space-x-2 p-5 px-6 font-bold"> <div class="flex items-center space-x-2 p-5 px-6 font-bold">
@ -62,7 +77,7 @@
<div class="md:max-w-64 truncate text-base tracking-tight md:text-2xl lg:block"> <div class="md:max-w-64 truncate text-base tracking-tight md:text-2xl lg:block">
Preview Deployments Preview Deployments
</div> </div>
<span class="text-xs">{application.name} </span> <span class="text-xs">{application?.name}</span>
</div> </div>
{#if application.gitSource?.htmlUrl && application.repository && application.branch} {#if application.gitSource?.htmlUrl && application.repository && application.branch}
<a <a
@ -108,68 +123,72 @@
</a> </a>
{/if} {/if}
</div> </div>
<div class="mx-auto max-w-6xl px-6 pt-4"> {#if loading}
<div class="flex justify-center py-4 text-center"> <Loading />
<Explainer {:else}
customClass="w-full" <div class="mx-auto max-w-6xl px-6 pt-4">
text={applicationSecrets.length === 0 <div class="flex justify-center py-4 text-center">
? "You can add secrets to PR/MR deployments. Please add secrets to the application first. <br>Useful for creating <span class='text-green-500 font-bold'>staging</span> environments." <Explainer
: "These values overwrite application secrets in PR/MR deployments. <br>Useful for creating <span class='text-green-500 font-bold'>staging</span> environments."} customClass="w-full"
/> text={applicationSecrets.length === 0
</div> ? "You can add secrets to PR/MR deployments. Please add secrets to the application first. <br>Useful for creating <span class='text-green-500 font-bold'>staging</span> environments."
{#if applicationSecrets.length !== 0} : "These values overwrite application secrets in PR/MR deployments. <br>Useful for creating <span class='text-green-500 font-bold'>staging</span> environments."}
<table class="mx-auto border-separate text-left"> />
<thead> </div>
<tr class="h-12"> {#if applicationSecrets.length !== 0}
<th scope="col">{$t('forms.name')}</th> <table class="mx-auto border-separate text-left">
<th scope="col">{$t('forms.value')}</th> <thead>
<th scope="col" class="w-64 text-center" <tr class="h-12">
>{$t('application.preview.need_during_buildtime')}</th <th scope="col">{$t('forms.name')}</th>
> <th scope="col">{$t('forms.value')}</th>
<th scope="col" class="w-96 text-center">{$t('forms.action')}</th> <th scope="col" class="w-64 text-center"
</tr> >{$t('application.preview.need_during_buildtime')}</th
</thead> >
<tbody> <th scope="col" class="w-96 text-center">{$t('forms.action')}</th>
{#each applicationSecrets as secret} </tr>
{#key secret.id} </thead>
<tr> <tbody>
<Secret {#each applicationSecrets as secret}
PRMRSecret={PRMRSecrets.find((s) => s.name === secret.name)} {#key secret.id}
isPRMRSecret <tr>
name={secret.name} <Secret
value={secret.value} PRMRSecret={PRMRSecrets.find((s) => s.name === secret.name)}
isBuildSecret={secret.isBuildSecret} isPRMRSecret
on:refresh={refreshSecrets} name={secret.name}
/> value={secret.value}
</tr> isBuildSecret={secret.isBuildSecret}
{/key} on:refresh={refreshSecrets}
{/each} />
</tbody> </tr>
</table> {/key}
{/if} {/each}
</div> </tbody>
</table>
<div class="mx-auto max-w-4xl py-10">
<div class="flex flex-wrap justify-center space-x-2">
{#if containers.length > 0}
{#each containers as container}
<a href={container.fqdn} class="p-2 no-underline" target="_blank">
<div class="box-selection text-center hover:border-transparent hover:bg-coolgray-200">
<div class="truncate text-center text-xl font-bold">{getDomain(container.fqdn)}</div>
</div>
</a>
<div class="flex items-center justify-center">
<button class="bg-coollabs hover:bg-coollabs-100" on:click={() => redeploy(container)}
>{$t('application.preview.redeploy')}</button
>
</div>
{/each}
{:else}
<div class="flex-col">
<div class="text-center font-bold text-xl">
{$t('application.preview.no_previews_available')}
</div>
</div>
{/if} {/if}
</div> </div>
</div>
<div class="mx-auto max-w-4xl py-10">
<div class="flex flex-wrap justify-center space-x-2">
{#if containers.length > 0}
{#each containers as container}
<a href={container.fqdn} class="p-2 no-underline" target="_blank">
<div class="box-selection text-center hover:border-transparent hover:bg-coolgray-200">
<div class="truncate text-center text-xl font-bold">{getDomain(container.fqdn)}</div>
</div>
</a>
<div class="flex items-center justify-center">
<button class="bg-coollabs hover:bg-coollabs-100" on:click={() => redeploy(container)}
>{$t('application.preview.redeploy')}</button
>
</div>
{/each}
{:else}
<div class="flex-col">
<div class="text-center font-bold text-xl">
{$t('application.preview.no_previews_available')}
</div>
</div>
{/if}
</div>
</div>
{/if}