Make the rest of the application use "new" services
This commit is contained in:
@@ -1,28 +1,25 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Plus, Factory, TrendingUp, Briefcase } from 'lucide-react';
|
||||
import { IndJobResponse, IndTransactionResponse, IndJobRecord, IndTransactionRecord } from '@/lib/pbtypes';
|
||||
import { IndTransactionResponse, IndJobRecord, IndTransactionRecord, IndTransactionRecordNoId } from '@/lib/pbtypes';
|
||||
import * as jobService from '@/services/jobService';
|
||||
import * as transactionService from '@/services/transactionService';
|
||||
import * as billItemService from '@/services/billItemService';
|
||||
import { formatISK } from '@/utils/priceUtils';
|
||||
import JobCard from '@/components/JobCard';
|
||||
import JobForm from '@/components/JobForm';
|
||||
import TransactionForm from '@/components/TransactionForm';
|
||||
import TransactionTable from '@/components/TransactionTable';
|
||||
import { IndJob } from '@/lib/types';
|
||||
import { createJob } from '@/services/jobService';
|
||||
|
||||
// Extended job type for UI components
|
||||
interface JobWithRelations extends IndJobResponse {
|
||||
expenditures: IndTransactionResponse[];
|
||||
income: IndTransactionResponse[];
|
||||
billOfMaterials: any[];
|
||||
consumedMaterials: { name: string; required: number }[];
|
||||
}
|
||||
|
||||
const Index = () => {
|
||||
const [jobs, setJobs] = useState<JobWithRelations[]>([]);
|
||||
const [jobs, setJobs] = useState<IndJob[]>([]);
|
||||
const [showJobForm, setShowJobForm] = useState(false);
|
||||
const [editingJob, setEditingJob] = useState<JobWithRelations | null>(null);
|
||||
const [selectedJob, setSelectedJob] = useState<JobWithRelations | null>(null);
|
||||
const [editingJob, setEditingJob] = useState<IndJob | null>(null);
|
||||
const [selectedJob, setSelectedJob] = useState<IndJob | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
loadJobs();
|
||||
@@ -30,15 +27,9 @@ const Index = () => {
|
||||
|
||||
const loadJobs = async () => {
|
||||
try {
|
||||
const fetchedJobs = await jobService.getJobs();
|
||||
const fetchedJobs = await jobService.getJobsFull();
|
||||
// Convert to JobWithRelations format
|
||||
const jobsWithRelations: JobWithRelations[] = fetchedJobs.map(job => ({
|
||||
...job,
|
||||
expenditures: [],
|
||||
income: [],
|
||||
billOfMaterials: [],
|
||||
consumedMaterials: []
|
||||
}));
|
||||
const jobsWithRelations: IndJob[] = fetchedJobs;
|
||||
setJobs(jobsWithRelations);
|
||||
} catch (error) {
|
||||
console.error('Error loading jobs:', error);
|
||||
@@ -47,8 +38,8 @@ const Index = () => {
|
||||
|
||||
const handleCreateJob = async (jobData: Omit<IndJobRecord, 'id' | 'created' | 'updated'>) => {
|
||||
try {
|
||||
const newJob = await jobService.createJob(jobData);
|
||||
const jobWithRelations: JobWithRelations = {
|
||||
const newJob = await createJob(jobData);
|
||||
const jobWithRelations: IndJob = {
|
||||
...newJob,
|
||||
expenditures: [],
|
||||
income: [],
|
||||
@@ -62,28 +53,28 @@ const Index = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditJob = (job: JobWithRelations) => {
|
||||
const handleEditJob = (job: IndJob) => {
|
||||
setEditingJob(job);
|
||||
setShowJobForm(true);
|
||||
};
|
||||
|
||||
const handleUpdateJob = async (jobData: Omit<IndJobRecord, 'id' | 'created' | 'updated'>) => {
|
||||
if (!editingJob) return;
|
||||
|
||||
|
||||
try {
|
||||
const updatedJob = await jobService.updateJob(editingJob.id, jobData);
|
||||
const updatedJobWithRelations: JobWithRelations = {
|
||||
const updatedJobWithRelations: IndJob = {
|
||||
...updatedJob,
|
||||
expenditures: editingJob.expenditures,
|
||||
income: editingJob.income,
|
||||
billOfMaterials: editingJob.billOfMaterials,
|
||||
consumedMaterials: editingJob.consumedMaterials
|
||||
};
|
||||
|
||||
|
||||
setJobs(jobs.map(job => job.id === editingJob.id ? updatedJobWithRelations : job));
|
||||
setShowJobForm(false);
|
||||
setEditingJob(null);
|
||||
|
||||
|
||||
if (selectedJob?.id === editingJob.id) {
|
||||
setSelectedJob(updatedJobWithRelations);
|
||||
}
|
||||
@@ -106,24 +97,16 @@ const Index = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleTransactionsAdded = async (transactions: IndTransactionRecord[], type: 'expenditure' | 'income') => {
|
||||
const handleTransactionsAdded = async (transactions: IndTransactionRecordNoId[], type: 'expenditure' | 'income') => {
|
||||
if (!selectedJob) return;
|
||||
|
||||
|
||||
try {
|
||||
let updatedJob = selectedJob;
|
||||
for (const transaction of transactions) {
|
||||
await jobService.addTransaction(selectedJob.id, transaction, type);
|
||||
updatedJob = await transactionService.createTransaction(updatedJob, transaction, type);
|
||||
}
|
||||
|
||||
|
||||
// Update local state
|
||||
const updatedJob = { ...selectedJob };
|
||||
const newTransactions = transactions as unknown as IndTransactionResponse[];
|
||||
|
||||
if (type === 'expenditure') {
|
||||
updatedJob.expenditures = [...updatedJob.expenditures, ...newTransactions];
|
||||
} else {
|
||||
updatedJob.income = [...updatedJob.income, ...newTransactions];
|
||||
}
|
||||
|
||||
setSelectedJob(updatedJob);
|
||||
setJobs(jobs.map(job => job.id === selectedJob.id ? updatedJob : job));
|
||||
} catch (error) {
|
||||
@@ -135,17 +118,9 @@ const Index = () => {
|
||||
if (!selectedJob) return;
|
||||
|
||||
try {
|
||||
await jobService.updateTransaction(selectedJob.id, transactionId, updates);
|
||||
|
||||
let updatedJob = selectedJob;
|
||||
updatedJob = await transactionService.updateTransaction(updatedJob, transactionId, updates);
|
||||
// Update local state
|
||||
const updatedJob = { ...selectedJob };
|
||||
updatedJob.expenditures = updatedJob.expenditures.map(tx =>
|
||||
tx.id === transactionId ? { ...tx, ...updates } : tx
|
||||
);
|
||||
updatedJob.income = updatedJob.income.map(tx =>
|
||||
tx.id === transactionId ? { ...tx, ...updates } : tx
|
||||
);
|
||||
|
||||
setSelectedJob(updatedJob);
|
||||
setJobs(jobs.map(job => job.id === selectedJob.id ? updatedJob : job));
|
||||
} catch (error) {
|
||||
@@ -157,13 +132,13 @@ const Index = () => {
|
||||
if (!selectedJob) return;
|
||||
|
||||
try {
|
||||
await jobService.deleteTransaction(selectedJob.id, transactionId);
|
||||
|
||||
await transactionService.deleteTransaction(selectedJob, transactionId);
|
||||
|
||||
// Update local state
|
||||
const updatedJob = { ...selectedJob };
|
||||
updatedJob.expenditures = updatedJob.expenditures.filter(tx => tx.id !== transactionId);
|
||||
updatedJob.income = updatedJob.income.filter(tx => tx.id !== transactionId);
|
||||
|
||||
|
||||
setSelectedJob(updatedJob);
|
||||
setJobs(jobs.map(job => job.id === selectedJob.id ? updatedJob : job));
|
||||
} catch (error) {
|
||||
@@ -178,7 +153,7 @@ const Index = () => {
|
||||
return sum + (income - expenditure);
|
||||
}, 0);
|
||||
|
||||
const totalRevenue = jobs.reduce((sum, job) =>
|
||||
const totalRevenue = jobs.reduce((sum, job) =>
|
||||
sum + job.income.reduce((sum, tx) => sum + tx.totalPrice, 0), 0
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user