16
17 useEffect(() => {
18 fetchData();
19 }, []);
20
21 const fetchData = async () => {
22 try {
23 const [salesRes, productsRes, customersRes] = await Promise.all([
24 fetch('/api/sales'),
25 fetch('/api/inventory'),
26 fetch('/api/customers')
27 ]);
28
37 if (customersResult.success) setCustomers(customersResult.data);
38 } catch (error) {
39 console.error('Failed to fetch data:', error);
40 } finally {
41 setLoading(false);
45 const handleCreateSale = async (saleData: CreateSaleRequest) => {
46 try {
47 const response = await fetch('/api/sales', {
48 method: 'POST',
49 headers: { 'Content-Type': 'application/json' },
58 onDataChange();
59 // Refresh products to update stock levels
60 const productsRes = await fetch('/api/inventory');
61 const productsResult = await productsRes.json();
62 if (productsResult.success) setProducts(productsResult.data);
43 return c.json<ApiResponse<null>>({
44 success: false,
45 error: "Failed to fetch livestock"
46 }, 500);
47 }
43 return c.json<ApiResponse<null>>({
44 success: false,
45 error: "Failed to fetch crops"
46 }, 500);
47 }
17
18 useEffect(() => {
19 fetchProducts();
20 }, []);
21
22 const fetchProducts = async () => {
23 try {
24 const response = await fetch('/api/inventory');
25 const result = await response.json();
26 if (result.success) {
28 }
29 } catch (error) {
30 console.error('Failed to fetch products:', error);
31 } finally {
32 setLoading(false);
36 const handleAddProduct = async (productData: CreateProductRequest) => {
37 try {
38 const response = await fetch('/api/inventory', {
39 method: 'POST',
40 headers: { 'Content-Type': 'application/json' },
58 const handleUpdateStock = async (productId: number, newQuantity: number) => {
59 try {
60 const response = await fetch(`/api/inventory/${productId}/stock`, {
61 method: 'PATCH',
62 headers: { 'Content-Type': 'application/json' },
253 onSubmit={async (data) => {
254 try {
255 const response = await fetch(`/api/inventory/${editingProduct.id}`, {
256 method: 'PUT',
257 headers: { 'Content-Type': 'application/json' },
17 const response: ApiResponse<ChatMessage[]> = {
18 success: false,
19 error: "Failed to fetch messages"
20 };
21 return c.json(response, 500);
17 const response: ApiResponse<Job[]> = {
18 success: false,
19 error: "Failed to fetch jobs"
20 };
21 return c.json(response, 500);
34});
35
36export default app.fetch;
16 };
17
18 const fetchMessages = async () => {
19 try {
20 const response = await fetch('/api/chat/messages');
21 const data: ApiResponse<ChatMessage[]> = await response.json();
22
25 setError(null);
26 } else {
27 setError(data.error || 'Failed to fetch messages');
28 }
29 } catch (err) {
45
46 try {
47 const response = await fetch('/api/chat/messages', {
48 method: 'POST',
49 headers: {
74
75 useEffect(() => {
76 fetchMessages();
77 // Load saved username
78 const savedUsername = localStorage.getItem('chatUsername');
82
83 // Poll for new messages every 5 seconds
84 const interval = setInterval(fetchMessages, 5000);
85 return () => clearInterval(interval);
86 }, []);
109 <div className="flex justify-between items-center mb-6">
110 <h2 className="text-2xl font-bold text-gray-900">💬 Public Chat Room</h2>
111 <button onClick={fetchMessages} className="btn btn-secondary">
112 🔄 Refresh
113 </button>
15
16 useEffect(() => {
17 // Use initial data if available, otherwise fetch
18 if (window.__INITIAL_DATA__) {
19 setDashboardData(window.__INITIAL_DATA__);
20 setLoading(false);
21 } else {
22 fetchDashboardData();
23 }
24 }, []);
25
26 const fetchDashboardData = async () => {
27 try {
28 const response = await fetch('/api/analytics/dashboard');
29 const result = await response.json();
30 if (result.success) {
32 }
33 } catch (error) {
34 console.error('Failed to fetch dashboard data:', error);
35 } finally {
36 setLoading(false);
39
40 const refreshDashboard = () => {
41 fetchDashboardData();
42 };
43
32
33 try {
34 const response = await fetch('/api/jobs', {
35 method: 'POST',
36 headers: {