-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working version of calling categories from stores.ts
- Loading branch information
Showing
4 changed files
with
71 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,3 @@ | ||
import type { LayoutServerLoad } from './$types'; | ||
|
||
interface LoadResponse { | ||
mycats: any; | ||
status?: number; | ||
error?: string; | ||
} | ||
|
||
/** @type {import('./$types').PageServerLoad} */ | ||
export async function load(): Promise<LoadResponse> { | ||
console.log('load categories started'); | ||
try { | ||
|
||
const res = await fetch(`http://localhost:8000/api/categories`); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Failed to fetch categories with status ${res.status}`); | ||
} | ||
|
||
const categories = await res.json(); | ||
console.log(`loaded categories with len: ${Object.keys(categories).length}`); | ||
return { mycats: categories }; | ||
|
||
} catch (error) { | ||
console.error('Failed to load data:', error); | ||
return { | ||
mycats: null, | ||
status: 500, | ||
error: 'Failed to load categories' | ||
}; | ||
} | ||
} | ||
|
||
|
||
|
||
export const ssr = true; | ||
export const csr = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { writable } from 'svelte/store'; | ||
|
||
console.log(`stores start`); | ||
|
||
export interface Categories { | ||
mycats: any; | ||
status?: number; | ||
error?: string; | ||
} | ||
|
||
/** @type {import('./$types').PageServerLoad} */ | ||
export async function load(): Promise<Categories> { | ||
console.log(`stores load categories start`); | ||
try { | ||
|
||
const res = await fetch(`http://localhost:8000/api/categories`); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Failed to fetch categories with status ${res.status}`); | ||
} | ||
|
||
const categories = await res.json(); | ||
|
||
console.log(`stores loaded categories with len: ${Object.keys(categories).length}`); | ||
return { mycats: categories }; | ||
|
||
} catch (error) { | ||
console.error('Failed to load data:', error); | ||
return { | ||
mycats: null, | ||
status: 500, | ||
error: 'Failed to load categories' | ||
}; | ||
} | ||
} | ||
|
||
const defaultCategories: Categories = { | ||
mycats: [], | ||
status: 200, | ||
error: '' | ||
}; | ||
|
||
|
||
// export const myList = writable<Categories | null>(null); | ||
export const myList = writable<Categories>(defaultCategories); | ||
|
||
console.log("stores myList try set") | ||
|
||
load().then(result => { | ||
myList.set(result); | ||
console.log("stores myList set!") | ||
}); | ||
|
||
console.log("stores done") |