Convert the app from using legacy Collection models to OrderItem-based data. Updated pages (app/page and app/map/page) and all related components (CollectionMap, CollectionItemCard, CollectModal, ProblemModal, LocationTracker) to use OrderItem and new field names (supplier, order, item_status, qty_collected/collected_date, problem_flag/notes, etc.). PocketBase client now queries/updates the order_items collection and maps status/field changes accordingly. Simplified location tracking logic and cleaned up map/navigation UI and Suspense fallbacks. Added import_suppliers.py script for migrating suppliers from NocoDB and included .DS_Store artifacts.
95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
import PocketBase from 'pocketbase';
|
|
import { AppSettings, DEFAULT_SETTINGS, OrderItem, DriverLocation } from './types';
|
|
|
|
const POCKETBASE_URL = process.env.NEXT_PUBLIC_POCKETBASE_URL || 'https://pocketbase-gnqc88suy8fqk6wyryjjdpvl.169.239.180.130.sslip.io';
|
|
|
|
export const pb = new PocketBase(POCKETBASE_URL);
|
|
|
|
// Auth helpers
|
|
export const isAuthenticated = () => pb.authStore.isValid;
|
|
|
|
export const getCurrentUser = () => pb.authStore.model;
|
|
|
|
export const logout = () => {
|
|
pb.authStore.clear();
|
|
if (typeof window !== 'undefined') {
|
|
window.location.href = '/login/';
|
|
}
|
|
};
|
|
|
|
export const login = async (email: string, password: string) => {
|
|
return await pb.collection('users').authWithPassword(email, password);
|
|
};
|
|
|
|
// Get assigned collections (order_items assigned to current driver)
|
|
export const getAssignedCollections = async () => {
|
|
const user = getCurrentUser();
|
|
if (!user) return [];
|
|
|
|
const records = await pb.collection('order_items').getFullList({
|
|
filter: `driver = "${user.id}" && (item_status = "assigned" || item_status = "driver_pool")`,
|
|
expand: 'supplier,order,purchase_order',
|
|
sort: 'supplier.suppliername',
|
|
});
|
|
|
|
return records as unknown as OrderItem[];
|
|
};
|
|
|
|
// Update order item status when collected
|
|
export const updateCollectionStatus = async (
|
|
id: string,
|
|
status: string,
|
|
data: { qty_collected?: number; notes?: string; collected_date?: string; problem_type?: string; problem_flag?: boolean }
|
|
) => {
|
|
return await pb.collection('order_items').update(id, {
|
|
item_status: status,
|
|
...data,
|
|
});
|
|
};
|
|
|
|
// Report a problem on an order item
|
|
export const reportProblem = async (
|
|
orderItemId: string,
|
|
problemType: string,
|
|
description: string
|
|
) => {
|
|
return await pb.collection('order_items').update(orderItemId, {
|
|
item_status: 'problem',
|
|
problem_type: problemType,
|
|
problem_flag: true,
|
|
problem_notes: description,
|
|
});
|
|
};
|
|
|
|
// Location tracking - tries to save, fails silently if table doesn't exist
|
|
export const saveDriverLocation = async (location: Omit<DriverLocation, 'id'>) => {
|
|
try {
|
|
return await pb.collection('driver_locations').create(location);
|
|
} catch {
|
|
// Silently fail - table may not exist yet
|
|
}
|
|
};
|
|
|
|
// Settings helpers
|
|
const SETTINGS_KEY = '1driver_settings';
|
|
|
|
export const getSettings = (): AppSettings => {
|
|
if (typeof window === 'undefined') return DEFAULT_SETTINGS;
|
|
const saved = localStorage.getItem(SETTINGS_KEY);
|
|
if (saved) {
|
|
try {
|
|
return { ...DEFAULT_SETTINGS, ...JSON.parse(saved) };
|
|
} catch {
|
|
return DEFAULT_SETTINGS;
|
|
}
|
|
}
|
|
return DEFAULT_SETTINGS;
|
|
};
|
|
|
|
export const saveSettings = (settings: Partial<AppSettings>) => {
|
|
if (typeof window === 'undefined') return;
|
|
const current = getSettings();
|
|
const updated = { ...current, ...settings };
|
|
localStorage.setItem(SETTINGS_KEY, JSON.stringify(updated));
|
|
return updated;
|
|
};
|