Refactor MachineCard component and update API endpoints
This commit is contained in:
@@ -80,7 +80,7 @@ export default function ProtectedLayout() {
|
||||
options={{
|
||||
title: 'Macchine',
|
||||
tabBarIcon: ({ color, size }) => <Car pointerEvents="none" color={color} size={24} />,
|
||||
href: authState.user?.isAdmin ? undefined : null,
|
||||
href: !authState.user?.isAdmin ? undefined : null,
|
||||
}}
|
||||
/>
|
||||
<Tabs.Screen
|
||||
|
||||
@@ -65,7 +65,7 @@ export default function MachineScreen() {
|
||||
const onScan = async (data: string) => {
|
||||
console.log('Scanned data:', data);
|
||||
try {
|
||||
const response = await api.post('/machine-attendance/scan', { uuid: data });
|
||||
const response = await api.post('/machine-attendance/in', { uuid: data });
|
||||
if (response.data?.success) {
|
||||
console.log('Scan data sent successfully:', response.data);
|
||||
fetchAttendances();
|
||||
@@ -84,28 +84,6 @@ export default function MachineScreen() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleExitPress = (item: MachineAttendanceItem) => {
|
||||
alert.showConfirm(
|
||||
'Conferma Uscita',
|
||||
`Vuoi davvero registrare l'uscita da ${item.name}?`,
|
||||
[
|
||||
{ text: 'Annulla', onPress: () => {}, style: 'cancel' },
|
||||
{ text: 'Conferma', style: 'default', onPress: async () => {
|
||||
try {
|
||||
const response = await api.post('/machine-attendance/out', { uuid: item.machine_uuid });
|
||||
if (response.data?.success) {
|
||||
fetchAttendances();
|
||||
} else {
|
||||
alert.showAlert('error', 'Errore', response.data?.message || 'Impossibile registrare l\'uscita.');
|
||||
}
|
||||
} catch (error) {
|
||||
alert.showAlert('error', 'Errore', 'Impossibile registrare l\'uscita. Riprova più tardi.');
|
||||
}
|
||||
}}
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
if (isLoading && !refreshing) {
|
||||
return <LoadingScreen />;
|
||||
}
|
||||
@@ -192,7 +170,7 @@ export default function MachineScreen() {
|
||||
) : (
|
||||
<View>
|
||||
{attendances.map((item, index) => (
|
||||
<MachineCard key={index} item={item} onExitPress={handleExitPress} />
|
||||
<MachineCard key={index} item={item} />
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
|
||||
@@ -99,13 +99,13 @@ export default function PermitsScreen() {
|
||||
const deletePermitRequest = async (id: number, itemRef?: React.ElementRef<typeof Swipeable> | null) => {
|
||||
try {
|
||||
itemRef?.close();
|
||||
const res = await api.get(`/request/delete-request?id=${id}`);
|
||||
if (res.data.code === 200) {
|
||||
const res = await api.post('/request/delete', { id });
|
||||
if (res.data?.success) {
|
||||
// Optimistic update
|
||||
setPermits(prevPermits => prevPermits.filter(p => p.id !== id));
|
||||
alert.showAlert('success', 'Richiesta eliminata', 'La richiesta è stata eliminata con successo.');
|
||||
} else {
|
||||
alert.showAlert('error', 'Errore', 'Impossibile eliminare la richiesta.');
|
||||
alert.showAlert('error', 'Errore', res.data?.message || 'Impossibile eliminare la richiesta.');
|
||||
}
|
||||
// Refresh
|
||||
fetchPermits();
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import { AlertTriangle, Car, KeySquare } from 'lucide-react-native';
|
||||
import { Car } from 'lucide-react-native';
|
||||
import React from 'react';
|
||||
import { Text, View, TouchableOpacity } from 'react-native';
|
||||
import { Text, View } from 'react-native';
|
||||
import { MachineAttendanceItem } from '@/types/types';
|
||||
|
||||
interface MachineCardProps {
|
||||
item: MachineAttendanceItem;
|
||||
onExitPress: (item: MachineAttendanceItem) => void;
|
||||
}
|
||||
|
||||
export default function MachineCard({ item, onExitPress }: MachineCardProps) {
|
||||
export default function MachineCard({ item }: MachineCardProps) {
|
||||
return (
|
||||
<View className="bg-white p-5 rounded-3xl shadow-sm border border-gray-100 flex-col mb-4">
|
||||
<View className="flex-row items-center border-b border-gray-50 pb-3 mb-3">
|
||||
@@ -34,23 +33,15 @@ export default function MachineCard({ item, onExitPress }: MachineCardProps) {
|
||||
</Text>
|
||||
</View>
|
||||
|
||||
{/* The exit is registered automatically at check-in; older records
|
||||
created before that change may still have no exit */}
|
||||
{item.out ? (
|
||||
<View className="flex-row items-center">
|
||||
<Text className="font-bold text-green-600">
|
||||
Uscita: {item.out}
|
||||
</Text>
|
||||
</View>
|
||||
) : (
|
||||
<View className="flex-row items-center flex-1 justify-end">
|
||||
<TouchableOpacity
|
||||
onPress={() => onExitPress(item)}
|
||||
className="bg-red-50 px-4 py-2 rounded-full border border-red-200 flex-row items-center shadow-sm active:bg-red-100"
|
||||
>
|
||||
<KeySquare size={14} color="#dc2626" />
|
||||
<Text className="text-red-600 font-bold ml-2 text-sm">Registra Uscita</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
)}
|
||||
) : null}
|
||||
</View>
|
||||
</View>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user