Skip to content

Commit

Permalink
test.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomideusz committed Apr 8, 2024
2 parents 68abec2 + 04f5564 commit a9b1962
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
4 changes: 0 additions & 4 deletions local_env_template

This file was deleted.

16 changes: 16 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const api = (customFetch = fetch) => ({
getAllAds: async () => {
const response = await customFetch(
'api',
)
const data = (await response.json())
return data
},
getAdsByCategory: async (cat: string) => {
const response = await customFetch(
`api?category=${cat}`,
)
const data = (await response.json())
return data
},
})
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { onMount } from "svelte"
import { createQuery, useQueryClient } from "@tanstack/svelte-query"
import { subscribeToAds } from "$lib/supabase/subscribeToAds"
import { fetchAdsForCategories } from "./helpers"
import { fetchAdsForCategoriess } from "./api/helpers"
import { WebsiteName } from "../config"
import logo from "$lib/img/zaur.png?enhanced&w=373"
import { Button } from "bits-ui"
Expand Down
8 changes: 7 additions & 1 deletion src/routes/+page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAdsForCategories } from './helpers';
import { fetchAdsForCategories } from './api/helpers';

export async function load({ fetch, parent }) {
const { queryClient } = await parent();
Expand All @@ -11,4 +11,10 @@ export async function load({ fetch, parent }) {
queryFn: () => fetchAdsForCategories({ fetch }, category),
})
));

// Cachowanie danych dla 'sales'
await queryClient.prefetchQuery({
queryKey: ['ads', route.id],
queryFn: () => api(fetch).getAdsByCategory('rental')
});
}
40 changes: 23 additions & 17 deletions src/routes/api/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,34 @@ const categoryToTableMap = {
// Dodaj tutaj więcej kategorii w przyszłości
};

async function fetchDataForCategory(category) {
const tableName = categoryToTableMap[category];
if (!tableName) {
throw new Error('Invalid category provided');
}
const response = await supabase.from(tableName).select('*').order('created_at', { ascending: false }).range(0, 9);
return response.data; // Zakładamy, że 'response.data' zawiera potrzebne dane
}

export async function GET({ url }) {
const category = url.searchParams.get('category');

try {
// Obsługa zapytania dla wszystkich kategorii
if (category === '/' || category === null) {
const responses = await Promise.all(Object.values(categoryToTableMap).map(tableName =>
supabase.from(tableName).select('*').order('created_at', { ascending: false }).range(0, 9)
));

const combinedResponse = responses.flatMap(response => response.data);
return json(combinedResponse);
if (category) {
// Logika dla pojedynczej kategorii, gdy parametr jest obecny
const data = await fetchDataForCategory(category);
return json({ [category]: data });
} else {
// Sprawdzenie, czy kategoria odpowiada znanej tabeli
const tableName = categoryToTableMap[category];
if (!tableName) {
return json({ error: 'Invalid category provided' }, { status: 400 });
}

// Logika dla pojedynczej kategorii
const response = await supabase.from(tableName).select('*').order('created_at', { ascending: false }).range(0, 9);
return json(response.data);
// Pobieranie danych dla obu kategorii jednocześnie, gdy parametr nie jest obecny
const [salesData, rentalData] = await Promise.all([
fetchDataForCategory('sales'),
fetchDataForCategory('rental'),
]);

return json({
sales: salesData,
rental: rentalData,
});
}
} catch (error) {
return json({ error: error.message }, { status: 500 });
Expand Down
7 changes: 2 additions & 5 deletions src/routes/api/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
export async function fetchAdsForCategories(fetch, category) {
console.log(category);
try {
const response = await fetch(`api/?category=${category}`);
console.log('banan');
const response = await fetch(`api?category=${category}`);
if (!response.ok) {
console.log('ner')
throw new Error(`Network response was not ok for category: ${category}`);

throw new Error(`Network response was not ok.`);
}
return await response.json();
} catch (error) {
Expand Down

0 comments on commit a9b1962

Please sign in to comment.