Refactor MachineCard component and update API endpoints
This commit is contained in:
@@ -80,7 +80,7 @@ export default function ProtectedLayout() {
|
|||||||
options={{
|
options={{
|
||||||
title: 'Macchine',
|
title: 'Macchine',
|
||||||
tabBarIcon: ({ color, size }) => <Car pointerEvents="none" color={color} size={24} />,
|
tabBarIcon: ({ color, size }) => <Car pointerEvents="none" color={color} size={24} />,
|
||||||
href: authState.user?.isAdmin ? undefined : null,
|
href: !authState.user?.isAdmin ? undefined : null,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Tabs.Screen
|
<Tabs.Screen
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ export default function MachineScreen() {
|
|||||||
const onScan = async (data: string) => {
|
const onScan = async (data: string) => {
|
||||||
console.log('Scanned data:', data);
|
console.log('Scanned data:', data);
|
||||||
try {
|
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) {
|
if (response.data?.success) {
|
||||||
console.log('Scan data sent successfully:', response.data);
|
console.log('Scan data sent successfully:', response.data);
|
||||||
fetchAttendances();
|
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) {
|
if (isLoading && !refreshing) {
|
||||||
return <LoadingScreen />;
|
return <LoadingScreen />;
|
||||||
}
|
}
|
||||||
@@ -192,7 +170,7 @@ export default function MachineScreen() {
|
|||||||
) : (
|
) : (
|
||||||
<View>
|
<View>
|
||||||
{attendances.map((item, index) => (
|
{attendances.map((item, index) => (
|
||||||
<MachineCard key={index} item={item} onExitPress={handleExitPress} />
|
<MachineCard key={index} item={item} />
|
||||||
))}
|
))}
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -99,13 +99,13 @@ export default function PermitsScreen() {
|
|||||||
const deletePermitRequest = async (id: number, itemRef?: React.ElementRef<typeof Swipeable> | null) => {
|
const deletePermitRequest = async (id: number, itemRef?: React.ElementRef<typeof Swipeable> | null) => {
|
||||||
try {
|
try {
|
||||||
itemRef?.close();
|
itemRef?.close();
|
||||||
const res = await api.get(`/request/delete-request?id=${id}`);
|
const res = await api.post('/request/delete', { id });
|
||||||
if (res.data.code === 200) {
|
if (res.data?.success) {
|
||||||
// Optimistic update
|
// Optimistic update
|
||||||
setPermits(prevPermits => prevPermits.filter(p => p.id !== id));
|
setPermits(prevPermits => prevPermits.filter(p => p.id !== id));
|
||||||
alert.showAlert('success', 'Richiesta eliminata', 'La richiesta è stata eliminata con successo.');
|
alert.showAlert('success', 'Richiesta eliminata', 'La richiesta è stata eliminata con successo.');
|
||||||
} else {
|
} else {
|
||||||
alert.showAlert('error', 'Errore', 'Impossibile eliminare la richiesta.');
|
alert.showAlert('error', 'Errore', res.data?.message || 'Impossibile eliminare la richiesta.');
|
||||||
}
|
}
|
||||||
// Refresh
|
// Refresh
|
||||||
fetchPermits();
|
fetchPermits();
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import { AlertTriangle, Car, KeySquare } from 'lucide-react-native';
|
import { Car } from 'lucide-react-native';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Text, View, TouchableOpacity } from 'react-native';
|
import { Text, View } from 'react-native';
|
||||||
import { MachineAttendanceItem } from '@/types/types';
|
import { MachineAttendanceItem } from '@/types/types';
|
||||||
|
|
||||||
interface MachineCardProps {
|
interface MachineCardProps {
|
||||||
item: MachineAttendanceItem;
|
item: MachineAttendanceItem;
|
||||||
onExitPress: (item: MachineAttendanceItem) => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function MachineCard({ item, onExitPress }: MachineCardProps) {
|
export default function MachineCard({ item }: MachineCardProps) {
|
||||||
return (
|
return (
|
||||||
<View className="bg-white p-5 rounded-3xl shadow-sm border border-gray-100 flex-col mb-4">
|
<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">
|
<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>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
|
{/* The exit is registered automatically at check-in; older records
|
||||||
|
created before that change may still have no exit */}
|
||||||
{item.out ? (
|
{item.out ? (
|
||||||
<View className="flex-row items-center">
|
<View className="flex-row items-center">
|
||||||
<Text className="font-bold text-green-600">
|
<Text className="font-bold text-green-600">
|
||||||
Uscita: {item.out}
|
Uscita: {item.out}
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
) : (
|
) : null}
|
||||||
<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>
|
|
||||||
)}
|
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user