Skip to content

Commit

Permalink
#91 add notification on page component if a axios call faild
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBlaa committed Jul 11, 2024
1 parent e7252e0 commit 4cbb423
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 65 deletions.
15 changes: 12 additions & 3 deletions src/lib/components/page/Page.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import { onMount } from 'svelte';
import type { linkType } from '$lib/models/Models';
import { pageContentLayoutType } from '$lib/models/Enums';
import type { errorType, linkType } from '$lib/models/Models';
import { pageContentLayoutType, notificationType } from '$lib/models/Enums';
// ui components
import { AppShell, AppBar } from '@skeletonlabs/skeleton';
Expand All @@ -16,7 +16,8 @@
//popup
import { computePosition, autoUpdate, offset, shift, flip, arrow } from '@floating-ui/dom';
import { storePopup } from '@skeletonlabs/skeleton';
import { breadcrumbStore } from '$store/pageStores';
import { breadcrumbStore,notificationStore } from '$store/pageStores';
import { errorStore } from '$store/apiStores';
storePopup.set({ computePosition, autoUpdate, offset, shift, flip, arrow });
Expand All @@ -38,6 +39,14 @@ import type { helpItemType, helpStoreType } from '$models/Models';
export let contentLayoutType: pageContentLayoutType = pageContentLayoutType.center;
export let fixLeft: boolean = true;
errorStore.subscribe((error:errorType) => {
console.log("🚀 ~ errorStore.subscribe ~ value:", error.error)
notificationStore.showNotification({
notificationType: notificationType.error,
message: error.error
})
})
onMount(async () => {
console.log('page');
breadcrumbStore.clean();
Expand Down
16 changes: 16 additions & 0 deletions src/lib/models/Models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,19 @@ export class Receive {
this.columns = [];
}
}


export class errorType {
statusText: string;
status: number;
error:string;
stackTrace:string

constructor() {
this.statusText = "";
this.status = 0;
this.error = "";
this.stackTrace = "";
}

};
20 changes: 18 additions & 2 deletions src/lib/services/Api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Api.js
import axios from 'axios';
import { host, username, password } from '../stores/apiStores';
import { host, username, password, errorStore } from '../stores/apiStores';
import type { errorType } from '$models/Models';

console.log('setup axios');

Expand All @@ -23,9 +24,24 @@ const apiRequest = (method, url, request) => {
headers
})
.then((res) => {
// console.log("res",res);

return Promise.resolve(res);

})
.catch((err) => {
.catch((er) => {
//console.log("🚀 ~ apiRequest ~ err:", er)
const err = er.response;

let error:errorType = {
status: err.status,
statusText: err.statusText,
error: err.data.error,
stackTrace: err.data.stackTrace,
}

errorStore.set(error);

return Promise.reject(err);
});
};
Expand Down
3 changes: 3 additions & 0 deletions src/lib/stores/apiStores.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { writable } from 'svelte/store';
import { errorType } from '$models/Models'

export let host = 'window.location.origin';
export let username = '';
Expand All @@ -8,6 +9,8 @@ const hostStore = writable(''); //writable(window.location.origin);
const usernameStore = writable('');
const passwordStore = writable('');

export const errorStore = writable(new errorType());

hostStore.subscribe((value) => {
host = value;
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stores/pageStores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function createBreadcrumbStore() {
value = value === undefined ? new BreadcrumbModel() : value;

let v = value.items.find((i) => i.link === item.link);
console.log(value.items, v);
//console.log(value.items, v);

if (v) {
const i = value.items.indexOf(v);
Expand Down
3 changes: 0 additions & 3 deletions src/routes/+layout.js

This file was deleted.

9 changes: 7 additions & 2 deletions src/routes/components/core/+page.js → src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
export const prerender = true;
export const ssr = false;
export const trailingSlash = 'always';

import { setApiConfig } from '$lib/stores/apiStores';

/** @type {import('./$types').PageLoad} */
export function load() {
export async function load({ fetch }) {
console.log('load and set apiconfig');

if (import.meta.env.DEV) {
console.log('dev');

setApiConfig('https://dev.bexis2.uni-jena.de/', 'admin', '123456');
setApiConfig('http://localhost:44345/', 'davidschoene', '123456');

} else if (import.meta.env.PROD) {
console.log('PROD');
setApiConfig('https://dev.bexis2.uni-jena.de/', 'admin', '123456');
Expand Down
17 changes: 0 additions & 17 deletions src/routes/components/form/+page.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/routes/components/page/+page.js

This file was deleted.

30 changes: 10 additions & 20 deletions src/routes/components/page/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script lang="ts">
import Page from '$lib/components/page/Page.svelte';
import type { linkType } from '$lib/models/Models';
import type { errorType, linkType } from '$lib/models/Models';
import { pageContentLayoutType } from '$lib/models/Enums';
import { helpStore } from '$store/pageStores';
import { errorStore } from '$store/apiStores';
import type { helpItemType, helpStoreType } from '$models/Models';
import TextInput from '$lib/components/form/TextInput.svelte';
import ErrorMessage from '$lib/components/page/ErrorMessage.svelte';
let links: linkType[] = [
{
Expand All @@ -16,6 +15,9 @@
];
import { HelpExampleData } from './data/help';
import { onMount } from 'svelte';
import { Api } from '$lib';
let helpItems: helpItemType[] = HelpExampleData.helpItems;
helpStore.setHelpItemList(helpItems);
Expand All @@ -32,6 +34,10 @@
function updateTitle() {
title += Date.now();
}
onMount(async()=>{
var res = await Api.get("test/errortest");
})
</script>

<Page
Expand All @@ -44,29 +50,13 @@
<svelte:fragment slot="left">
<button class="btn variant-filled-error" on:click={updateTitle}>change title </button>
<div class="w-screen">
<h2 class="h2">Content</h2>
<TextInput id="name" label="name" help={true} />
<p>Lorem ipsum dolor sit amet, con</p>

{#if e}
<ErrorMessage error={e} />
{/if}
</div>
</svelte:fragment>
<div>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo
dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus
est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit
esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et
iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait
nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
sit amet.
</div>
</Page>

0 comments on commit 4cbb423

Please sign in to comment.