feat: add vite env and bulk delete

This commit is contained in:
2025-06-13 21:03:36 -03:00
parent a1cc868f33
commit ada0776d4e
4 changed files with 29 additions and 19 deletions
+5 -5
View File
@@ -5,17 +5,17 @@ import { FoodStorage, UserSettings } from './types';
const client = new Client(); const client = new Client();
client client
.setEndpoint('https://cloud.appwrite.io/v1') // Your Appwrite endpoint .setEndpoint(import.meta.env.VITE_APPWRITE_ENDPOINT)
.setProject("684ac3bf000ee8950f5a"); // Replace with your project ID .setProject("684ac3bf000ee8950f5a");
// Initialize services // Initialize services
export const account = new Account(client); export const account = new Account(client);
export const databases = new Databases(client); export const databases = new Databases(client);
// Configuration constants // Configuration constants
export const DATABASE_ID = '684b38db000fac8c781c'; // Replace with your database ID export const DATABASE_ID = import.meta.env.VITE_APPWRITE_DBID
export const FOOD_ENTRIES_COLLECTION_ID = '684b38e300336fc605dc'; // Replace with your collection ID export const FOOD_ENTRIES_COLLECTION_ID = import.meta.env.VITE_APPWRITE_FOODENTRIESID
export const USER_SETTINGS_COLLECTION_ID = '684c75140026689fe603'; // Replace with your collection ID export const USER_SETTINGS_COLLECTION_ID = import.meta.env.VITE_APPWRITE_USERSETTINGSID
// Auth helper functions // Auth helper functions
export class AppwriteAuth { export class AppwriteAuth {
+11 -13
View File
@@ -129,23 +129,17 @@ async function processResponseItems<T, R>(
return results; return results;
} }
// Example usage with API calls async function fetchUserSettings(documentId: string): Promise<any> {
interface ResponseItem { return AppwriteDB.deleteUserSettings(documentId);
id: string;
data: string;
}
async function fetchItemDetails(item: ResponseItem): Promise<any> {
const response = await fetch(`/api/items/${item.id}`);
return response.json();
} }
// Main function that processes the response // Main function that processes the response
async function handleResponse(response: ResponseItem[]): Promise<void> { async function handleBulkDelete(idsToDelete: string[]): Promise<void> {
try { try {
// This creates and executes all promises concurrently // This creates and executes all promises concurrently
const results = await processResponseItems(response, fetchItemDetails); const results = await processResponseItems(idsToDelete, fetchUserSettings);
console.log('All requests completed:', results); console.debug('All requests completed:', results);
} catch (error) { } catch (error) {
console.error('One or more requests failed:', error); console.error('One or more requests failed:', error);
} }
@@ -163,9 +157,13 @@ async function handleSaveSettings(e: SubmitEvent) {
// Delete existing settings - keep only the new one // Delete existing settings - keep only the new one
const documents = await AppwriteDB.getUserSettings(); const documents = await AppwriteDB.getUserSettings();
const promises = []; const documentsToDelete: string[] = [];
for (let i=0; i<documents.length; i++) { for (let i=0; i<documents.length; i++) {
// keep going from here, the promises documentsToDelete.push(documents[i].$id);
}
if (documentsToDelete) {
await handleBulkDelete(documentsToDelete);
} }
// Save to Appwrite // Save to Appwrite
+11
View File
@@ -0,0 +1,11 @@
interface ImportMetaEnv {
readonly VITE_APPWRITE_PROJECT_ID: string;
readonly VITE_APPWRITE_ENDPOINT: string;
readonly VITE_APPWRITE_DBID: string;
readonly VITE_APPWRITE_FOODENTRIESID: string;
readonly VITE_APPWRITE_USERSETTINGSID: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}
+2 -1
View File
@@ -1,5 +1,6 @@
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
export default defineConfig({ export default defineConfig({
base: '/calories-tracker/' base: '/calories-tracker/',
include: ['src', 'vite-end.d.ts']
}) })