420 lines
10 KiB
TypeScript
420 lines
10 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import {
|
|
View,
|
|
Text,
|
|
FlatList,
|
|
Pressable,
|
|
StyleSheet,
|
|
ActivityIndicator,
|
|
Platform,
|
|
Modal,
|
|
TextInput,
|
|
Alert,
|
|
} from 'react-native';
|
|
import { useRouter, useLocalSearchParams } from 'expo-router';
|
|
import { useStoresStore } from '@/store/stores';
|
|
import { api } from '@/lib/api';
|
|
import type { Store, Aisle } from '@shop/types';
|
|
|
|
/**
|
|
* Aisle management screen
|
|
* Allows managing aisles for a store
|
|
*/
|
|
export default function AislesScreen() {
|
|
const router = useRouter();
|
|
const { id } = useLocalSearchParams<{ id: string }>();
|
|
const { stores, updateStore } = useStoresStore();
|
|
|
|
const store = stores.find((s) => s.id === id);
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [showAddModal, setShowAddModal] = useState(false);
|
|
const [aisleName, setAisleName] = useState('');
|
|
const [aisleColor, setAisleColor] = useState('#FF6B6B');
|
|
const [isAddingAisle, setIsAddingAisle] = useState(false);
|
|
|
|
const colors = [
|
|
'#FF6B6B',
|
|
'#4ECDC4',
|
|
'#45B7D1',
|
|
'#FFA07A',
|
|
'#98D8C8',
|
|
'#F7DC6F',
|
|
'#BB8FCE',
|
|
'#85C1E2',
|
|
];
|
|
|
|
const handleAddAisle = async () => {
|
|
if (!aisleName.trim() || !store) {
|
|
Alert.alert('Error', 'Please enter an aisle name');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsAddingAisle(true);
|
|
const newAisle = await api.post<Aisle>(`/api/stores/${store.id}/aisles`, {
|
|
name: aisleName,
|
|
colour: aisleColor,
|
|
});
|
|
|
|
// Update local store
|
|
const updatedStore: Store = {
|
|
...store,
|
|
aisles: [...(store.aisles || []), newAisle],
|
|
};
|
|
updateStore(updatedStore);
|
|
|
|
setAisleName('');
|
|
setAisleColor('#FF6B6B');
|
|
setShowAddModal(false);
|
|
} catch (err) {
|
|
console.error('Failed to add aisle:', err);
|
|
Alert.alert('Error', 'Failed to add aisle');
|
|
} finally {
|
|
setIsAddingAisle(false);
|
|
}
|
|
};
|
|
|
|
const handleDeleteAisle = async (aisleId: string) => {
|
|
if (!store) return;
|
|
|
|
Alert.alert('Delete Aisle', 'Are you sure?', [
|
|
{ text: 'Cancel' },
|
|
{
|
|
text: 'Delete',
|
|
onPress: async () => {
|
|
try {
|
|
await api.delete(`/api/stores/${store.id}/aisles/${aisleId}`);
|
|
|
|
// Update local store
|
|
const updatedStore: Store = {
|
|
...store,
|
|
aisles: store.aisles?.filter((a) => a.id !== aisleId) || [],
|
|
};
|
|
updateStore(updatedStore);
|
|
} catch (err) {
|
|
console.error('Failed to delete aisle:', err);
|
|
Alert.alert('Error', 'Failed to delete aisle');
|
|
}
|
|
},
|
|
},
|
|
]);
|
|
};
|
|
|
|
if (!store) {
|
|
return (
|
|
<View style={styles.container}>
|
|
<Text>Store not found</Text>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<View style={styles.container}>
|
|
{/* Header */}
|
|
<View style={styles.header}>
|
|
<Pressable onPress={() => router.back()}>
|
|
<Text style={styles.backButton}>← Back</Text>
|
|
</Pressable>
|
|
<View style={styles.titleContainer}>
|
|
<Text style={styles.title}>{store.name}</Text>
|
|
<Text style={styles.subtitle}>Aisles</Text>
|
|
</View>
|
|
<Pressable onPress={() => setShowAddModal(true)}>
|
|
<Text style={styles.addButton}>+ Add</Text>
|
|
</Pressable>
|
|
</View>
|
|
|
|
{/* Aisles list */}
|
|
<FlatList
|
|
data={store.aisles || []}
|
|
keyExtractor={(item) => item.id}
|
|
renderItem={({ item: aisle }) => (
|
|
<View style={[styles.aisleCard, { borderLeftColor: aisle.colour }]}>
|
|
<View style={styles.aisleContent}>
|
|
<View style={[styles.colorSwatch, { backgroundColor: aisle.colour }]} />
|
|
<View style={styles.aisleInfo}>
|
|
<Text style={styles.aisleName}>{aisle.name}</Text>
|
|
<Text style={styles.aisleOrder}>Order: {aisle.order}</Text>
|
|
</View>
|
|
</View>
|
|
<Pressable style={styles.deleteButton} onPress={() => handleDeleteAisle(aisle.id)}>
|
|
<Text style={styles.deleteButtonText}>🗑</Text>
|
|
</Pressable>
|
|
</View>
|
|
)}
|
|
ListEmptyComponent={
|
|
<View style={styles.emptyContainer}>
|
|
<Text style={styles.emptyText}>No aisles yet</Text>
|
|
<Text style={styles.emptySubtext}>Add an aisle to organize items</Text>
|
|
</View>
|
|
}
|
|
/>
|
|
|
|
{/* FAB to add aisle */}
|
|
<Pressable style={styles.fab} onPress={() => setShowAddModal(true)}>
|
|
<Text style={styles.fabText}>+</Text>
|
|
</Pressable>
|
|
|
|
{/* Add Aisle Modal */}
|
|
<Modal visible={showAddModal} transparent animationType="fade">
|
|
<Pressable style={styles.modalOverlay} onPress={() => setShowAddModal(false)}>
|
|
<View style={styles.modal}>
|
|
<Text style={styles.modalTitle}>Add Aisle</Text>
|
|
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="Aisle name (e.g., Produce, Dairy...)"
|
|
value={aisleName}
|
|
onChangeText={setAisleName}
|
|
placeholderTextColor="#999"
|
|
/>
|
|
|
|
<Text style={styles.colorLabel}>Color</Text>
|
|
<View style={styles.colorGrid}>
|
|
{colors.map((color) => (
|
|
<Pressable
|
|
key={color}
|
|
style={[
|
|
styles.colorOption,
|
|
{ backgroundColor: color },
|
|
aisleColor === color && styles.colorOptionSelected,
|
|
]}
|
|
onPress={() => setAisleColor(color)}
|
|
>
|
|
{aisleColor === color && <Text style={styles.colorCheckmark}>✓</Text>}
|
|
</Pressable>
|
|
))}
|
|
</View>
|
|
|
|
<View style={styles.modalButtons}>
|
|
<Pressable style={styles.cancelButton} onPress={() => setShowAddModal(false)}>
|
|
<Text style={styles.cancelButtonText}>Cancel</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={[styles.addAisleButton, isAddingAisle && styles.buttonDisabled]}
|
|
onPress={handleAddAisle}
|
|
disabled={isAddingAisle}
|
|
>
|
|
{isAddingAisle ? (
|
|
<ActivityIndicator size="small" color="#fff" />
|
|
) : (
|
|
<Text style={styles.addAisleButtonText}>Add</Text>
|
|
)}
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
</Pressable>
|
|
</Modal>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
flex: 1,
|
|
backgroundColor: '#fff',
|
|
},
|
|
header: {
|
|
flexDirection: 'row',
|
|
justifyContent: 'space-between',
|
|
alignItems: 'center',
|
|
paddingHorizontal: 16,
|
|
paddingVertical: 12,
|
|
marginTop: Platform.OS === 'ios' ? 44 : 0,
|
|
borderBottomWidth: 1,
|
|
borderBottomColor: '#e0e0e0',
|
|
},
|
|
backButton: {
|
|
fontSize: 16,
|
|
color: '#007AFF',
|
|
},
|
|
titleContainer: {
|
|
flex: 1,
|
|
alignItems: 'center',
|
|
},
|
|
title: {
|
|
fontSize: 16,
|
|
fontWeight: '600',
|
|
color: '#000',
|
|
},
|
|
subtitle: {
|
|
fontSize: 12,
|
|
color: '#999',
|
|
marginTop: 2,
|
|
},
|
|
addButton: {
|
|
fontSize: 16,
|
|
color: '#007AFF',
|
|
fontWeight: '600',
|
|
},
|
|
aisleCard: {
|
|
flexDirection: 'row',
|
|
justifyContent: 'space-between',
|
|
alignItems: 'center',
|
|
paddingVertical: 12,
|
|
paddingHorizontal: 16,
|
|
marginVertical: 4,
|
|
marginHorizontal: 8,
|
|
backgroundColor: '#f9f9f9',
|
|
borderRadius: 8,
|
|
borderLeftWidth: 4,
|
|
},
|
|
aisleContent: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
flex: 1,
|
|
},
|
|
colorSwatch: {
|
|
width: 32,
|
|
height: 32,
|
|
borderRadius: 8,
|
|
marginRight: 12,
|
|
},
|
|
aisleInfo: {
|
|
flex: 1,
|
|
},
|
|
aisleName: {
|
|
fontSize: 16,
|
|
fontWeight: '600',
|
|
color: '#000',
|
|
},
|
|
aisleOrder: {
|
|
fontSize: 12,
|
|
color: '#999',
|
|
marginTop: 2,
|
|
},
|
|
deleteButton: {
|
|
paddingHorizontal: 8,
|
|
},
|
|
deleteButtonText: {
|
|
fontSize: 18,
|
|
},
|
|
emptyContainer: {
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
paddingVertical: 48,
|
|
},
|
|
emptyText: {
|
|
fontSize: 16,
|
|
fontWeight: '600',
|
|
color: '#000',
|
|
marginBottom: 8,
|
|
},
|
|
emptySubtext: {
|
|
fontSize: 14,
|
|
color: '#999',
|
|
},
|
|
fab: {
|
|
position: 'absolute',
|
|
bottom: 24,
|
|
right: 24,
|
|
width: 56,
|
|
height: 56,
|
|
borderRadius: 28,
|
|
backgroundColor: '#007AFF',
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
shadowColor: '#000',
|
|
shadowOffset: { width: 0, height: 2 },
|
|
shadowOpacity: 0.25,
|
|
shadowRadius: 3.84,
|
|
elevation: 5,
|
|
},
|
|
fabText: {
|
|
fontSize: 32,
|
|
color: '#fff',
|
|
fontWeight: '300',
|
|
},
|
|
modalOverlay: {
|
|
flex: 1,
|
|
backgroundColor: 'rgba(0, 0, 0, 0.5)',
|
|
justifyContent: 'flex-end',
|
|
},
|
|
modal: {
|
|
backgroundColor: '#fff',
|
|
borderTopLeftRadius: 20,
|
|
borderTopRightRadius: 20,
|
|
paddingHorizontal: 16,
|
|
paddingTop: 20,
|
|
paddingBottom: 24,
|
|
},
|
|
modalTitle: {
|
|
fontSize: 18,
|
|
fontWeight: '700',
|
|
color: '#000',
|
|
marginBottom: 16,
|
|
},
|
|
input: {
|
|
borderWidth: 1,
|
|
borderColor: '#ddd',
|
|
borderRadius: 8,
|
|
paddingHorizontal: 12,
|
|
paddingVertical: 10,
|
|
fontSize: 16,
|
|
marginBottom: 16,
|
|
backgroundColor: '#f9f9f9',
|
|
},
|
|
colorLabel: {
|
|
fontSize: 14,
|
|
fontWeight: '600',
|
|
color: '#000',
|
|
marginBottom: 8,
|
|
},
|
|
colorGrid: {
|
|
flexDirection: 'row',
|
|
flexWrap: 'wrap',
|
|
gap: 8,
|
|
marginBottom: 24,
|
|
},
|
|
colorOption: {
|
|
width: '22.5%',
|
|
aspectRatio: 1,
|
|
borderRadius: 8,
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
borderWidth: 2,
|
|
borderColor: 'transparent',
|
|
},
|
|
colorOptionSelected: {
|
|
borderColor: '#000',
|
|
},
|
|
colorCheckmark: {
|
|
color: '#fff',
|
|
fontSize: 18,
|
|
fontWeight: 'bold',
|
|
},
|
|
modalButtons: {
|
|
flexDirection: 'row',
|
|
gap: 8,
|
|
},
|
|
cancelButton: {
|
|
flex: 1,
|
|
paddingVertical: 12,
|
|
borderRadius: 8,
|
|
borderWidth: 1,
|
|
borderColor: '#ddd',
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
},
|
|
cancelButtonText: {
|
|
fontSize: 16,
|
|
fontWeight: '600',
|
|
color: '#666',
|
|
},
|
|
addAisleButton: {
|
|
flex: 1,
|
|
paddingVertical: 12,
|
|
backgroundColor: '#007AFF',
|
|
borderRadius: 8,
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
},
|
|
addAisleButtonText: {
|
|
color: '#fff',
|
|
fontSize: 16,
|
|
fontWeight: '600',
|
|
},
|
|
buttonDisabled: {
|
|
opacity: 0.6,
|
|
},
|
|
});
|