Compare commits
2 Commits
be2af1675a
...
f4b870f59c
| Author | SHA1 | Date | |
|---|---|---|---|
| f4b870f59c | |||
|
|
4834286005 |
257
app/components/inventory/AddItemForm.vue
Normal file
257
app/components/inventory/AddItemForm.vue
Normal file
@@ -0,0 +1,257 @@
|
||||
<template>
|
||||
<UCard>
|
||||
<template #header>
|
||||
<div class="flex items-center justify-between">
|
||||
<h3 class="text-lg font-semibold">Add New Item</h3>
|
||||
<UButton
|
||||
icon="i-heroicons-x-mark"
|
||||
color="gray"
|
||||
variant="ghost"
|
||||
@click="$emit('close')"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<form @submit.prevent="handleSubmit" class="space-y-4">
|
||||
<!-- Item Name -->
|
||||
<UFormGroup label="Item Name" required>
|
||||
<UInput
|
||||
v-model="form.name"
|
||||
placeholder="e.g. Whole Milk, Pasta, Tomatoes"
|
||||
size="lg"
|
||||
autofocus
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<!-- Quantity & Unit -->
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
<UFormGroup label="Quantity" required>
|
||||
<UInput
|
||||
v-model.number="form.quantity"
|
||||
type="number"
|
||||
min="0.01"
|
||||
step="0.01"
|
||||
placeholder="1"
|
||||
size="lg"
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<UFormGroup label="Unit" required>
|
||||
<USelect
|
||||
v-model="form.unit_id"
|
||||
:options="unitOptions"
|
||||
option-attribute="label"
|
||||
value-attribute="value"
|
||||
placeholder="Select unit"
|
||||
size="lg"
|
||||
/>
|
||||
</UFormGroup>
|
||||
</div>
|
||||
|
||||
<!-- Expiry Date -->
|
||||
<UFormGroup label="Expiry Date" hint="Optional">
|
||||
<UInput
|
||||
v-model="form.expiry_date"
|
||||
type="date"
|
||||
size="lg"
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<!-- Notes -->
|
||||
<UFormGroup label="Notes" hint="Optional">
|
||||
<UTextarea
|
||||
v-model="form.notes"
|
||||
placeholder="Any additional notes..."
|
||||
:rows="2"
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<!-- Tags -->
|
||||
<UFormGroup label="Tags" hint="Optional">
|
||||
<div class="space-y-2">
|
||||
<!-- Selected Tags -->
|
||||
<div v-if="selectedTags.length > 0" class="flex flex-wrap gap-1 mb-2">
|
||||
<UBadge
|
||||
v-for="tag in selectedTags"
|
||||
:key="tag.id"
|
||||
:style="{ backgroundColor: tag.color }"
|
||||
class="text-white cursor-pointer"
|
||||
@click="removeTag(tag.id)"
|
||||
>
|
||||
{{ tag.icon }} {{ tag.name }} ✕
|
||||
</UBadge>
|
||||
</div>
|
||||
|
||||
<!-- Tag Selection by Category -->
|
||||
<div v-for="category in tagCategories" :key="category.name" class="space-y-1">
|
||||
<p class="text-xs font-medium text-gray-500 uppercase">{{ category.name }}</p>
|
||||
<div class="flex flex-wrap gap-1">
|
||||
<UButton
|
||||
v-for="tag in category.tags"
|
||||
:key="tag.id"
|
||||
size="xs"
|
||||
:color="isTagSelected(tag.id) ? 'primary' : 'gray'"
|
||||
:variant="isTagSelected(tag.id) ? 'solid' : 'outline'"
|
||||
@click="toggleTag(tag)"
|
||||
>
|
||||
{{ tag.icon }} {{ tag.name }}
|
||||
</UButton>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</UFormGroup>
|
||||
|
||||
<!-- Submit -->
|
||||
<div class="flex gap-2 pt-2">
|
||||
<UButton
|
||||
type="submit"
|
||||
color="primary"
|
||||
size="lg"
|
||||
class="flex-1"
|
||||
:loading="submitting"
|
||||
:disabled="!isValid"
|
||||
>
|
||||
Add Item
|
||||
</UButton>
|
||||
<UButton
|
||||
color="gray"
|
||||
size="lg"
|
||||
variant="soft"
|
||||
@click="$emit('close')"
|
||||
>
|
||||
Cancel
|
||||
</UButton>
|
||||
</div>
|
||||
</form>
|
||||
</UCard>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
const { addInventoryItem, addItemTags } = useInventory()
|
||||
const { getUnits } = useUnits()
|
||||
const { getTags } = useTags()
|
||||
|
||||
const emit = defineEmits<{
|
||||
close: []
|
||||
added: [item: any]
|
||||
}>()
|
||||
|
||||
// Form state
|
||||
const form = reactive({
|
||||
name: '',
|
||||
quantity: 1,
|
||||
unit_id: '',
|
||||
expiry_date: '',
|
||||
notes: ''
|
||||
})
|
||||
|
||||
const submitting = ref(false)
|
||||
const selectedTags = ref<any[]>([])
|
||||
|
||||
// Load units and tags
|
||||
const units = ref<any[]>([])
|
||||
const tags = ref<any[]>([])
|
||||
|
||||
onMounted(async () => {
|
||||
const [unitsResult, tagsResult] = await Promise.all([
|
||||
getUnits(),
|
||||
getTags()
|
||||
])
|
||||
|
||||
units.value = unitsResult.data || []
|
||||
tags.value = tagsResult.data || []
|
||||
|
||||
// Set default unit (Piece)
|
||||
const defaultUnit = units.value.find(u => u.abbreviation === 'pc')
|
||||
if (defaultUnit) {
|
||||
form.unit_id = defaultUnit.id
|
||||
}
|
||||
})
|
||||
|
||||
// Unit options for select
|
||||
const unitOptions = computed(() => {
|
||||
const grouped: Record<string, any[]> = {}
|
||||
|
||||
for (const unit of units.value) {
|
||||
const type = unit.unit_type
|
||||
if (!grouped[type]) grouped[type] = []
|
||||
grouped[type].push({
|
||||
label: `${unit.name} (${unit.abbreviation})`,
|
||||
value: unit.id
|
||||
})
|
||||
}
|
||||
|
||||
return Object.entries(grouped).flatMap(([type, options]) => [
|
||||
{ label: `— ${type.charAt(0).toUpperCase() + type.slice(1)} —`, value: '', disabled: true },
|
||||
...options
|
||||
])
|
||||
})
|
||||
|
||||
// Tag categories for display
|
||||
const tagCategories = computed(() => {
|
||||
const categories: Record<string, any[]> = {}
|
||||
|
||||
for (const tag of tags.value) {
|
||||
const cat = tag.category
|
||||
if (!categories[cat]) categories[cat] = []
|
||||
categories[cat].push(tag)
|
||||
}
|
||||
|
||||
return Object.entries(categories).map(([name, tags]) => ({
|
||||
name,
|
||||
tags
|
||||
}))
|
||||
})
|
||||
|
||||
// Tag selection helpers
|
||||
const isTagSelected = (tagId: string) => {
|
||||
return selectedTags.value.some(t => t.id === tagId)
|
||||
}
|
||||
|
||||
const toggleTag = (tag: any) => {
|
||||
if (isTagSelected(tag.id)) {
|
||||
removeTag(tag.id)
|
||||
} else {
|
||||
selectedTags.value.push(tag)
|
||||
}
|
||||
}
|
||||
|
||||
const removeTag = (tagId: string) => {
|
||||
selectedTags.value = selectedTags.value.filter(t => t.id !== tagId)
|
||||
}
|
||||
|
||||
// Validation
|
||||
const isValid = computed(() => {
|
||||
return form.name.trim().length > 0 && form.quantity > 0 && form.unit_id
|
||||
})
|
||||
|
||||
// Submit
|
||||
const handleSubmit = async () => {
|
||||
if (!isValid.value) return
|
||||
|
||||
submitting.value = true
|
||||
|
||||
const { data, error } = await addInventoryItem({
|
||||
name: form.name.trim(),
|
||||
quantity: form.quantity,
|
||||
unit_id: form.unit_id,
|
||||
expiry_date: form.expiry_date || null,
|
||||
notes: form.notes.trim() || null
|
||||
})
|
||||
|
||||
if (error) {
|
||||
alert('Failed to add item: ' + error.message)
|
||||
submitting.value = false
|
||||
return
|
||||
}
|
||||
|
||||
// Add tags if any selected
|
||||
if (data && selectedTags.value.length > 0) {
|
||||
const tagIds = selectedTags.value.map(t => t.id)
|
||||
await addItemTags(data.id, tagIds)
|
||||
}
|
||||
|
||||
emit('added', data)
|
||||
submitting.value = false
|
||||
}
|
||||
</script>
|
||||
184
app/components/inventory/EditItemModal.vue
Normal file
184
app/components/inventory/EditItemModal.vue
Normal file
@@ -0,0 +1,184 @@
|
||||
<template>
|
||||
<UModal v-model="isOpen">
|
||||
<UCard>
|
||||
<template #header>
|
||||
<div class="flex items-center justify-between">
|
||||
<h3 class="text-lg font-semibold">Edit Item</h3>
|
||||
<UButton
|
||||
icon="i-heroicons-x-mark"
|
||||
color="gray"
|
||||
variant="ghost"
|
||||
@click="close"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<form @submit.prevent="handleSubmit" class="space-y-4">
|
||||
<!-- Item Name -->
|
||||
<UFormGroup label="Item Name" required>
|
||||
<UInput
|
||||
v-model="form.name"
|
||||
placeholder="Item name"
|
||||
size="lg"
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<!-- Quantity & Unit -->
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
<UFormGroup label="Quantity" required>
|
||||
<UInput
|
||||
v-model.number="form.quantity"
|
||||
type="number"
|
||||
min="0.01"
|
||||
step="0.01"
|
||||
size="lg"
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<UFormGroup label="Unit" required>
|
||||
<USelect
|
||||
v-model="form.unit_id"
|
||||
:options="unitOptions"
|
||||
option-attribute="label"
|
||||
value-attribute="value"
|
||||
size="lg"
|
||||
/>
|
||||
</UFormGroup>
|
||||
</div>
|
||||
|
||||
<!-- Expiry Date -->
|
||||
<UFormGroup label="Expiry Date" hint="Optional">
|
||||
<UInput
|
||||
v-model="form.expiry_date"
|
||||
type="date"
|
||||
size="lg"
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<!-- Notes -->
|
||||
<UFormGroup label="Notes" hint="Optional">
|
||||
<UTextarea
|
||||
v-model="form.notes"
|
||||
placeholder="Any additional notes..."
|
||||
:rows="2"
|
||||
/>
|
||||
</UFormGroup>
|
||||
|
||||
<!-- Submit -->
|
||||
<div class="flex gap-2 pt-2">
|
||||
<UButton
|
||||
type="submit"
|
||||
color="primary"
|
||||
size="lg"
|
||||
class="flex-1"
|
||||
:loading="submitting"
|
||||
:disabled="!isValid"
|
||||
>
|
||||
Save Changes
|
||||
</UButton>
|
||||
<UButton
|
||||
color="gray"
|
||||
size="lg"
|
||||
variant="soft"
|
||||
@click="close"
|
||||
>
|
||||
Cancel
|
||||
</UButton>
|
||||
</div>
|
||||
</form>
|
||||
</UCard>
|
||||
</UModal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
const { updateInventoryItem } = useInventory()
|
||||
const { getUnits } = useUnits()
|
||||
|
||||
const props = defineProps<{
|
||||
item: any | null
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
close: []
|
||||
updated: [item: any]
|
||||
}>()
|
||||
|
||||
const isOpen = ref(false)
|
||||
const submitting = ref(false)
|
||||
const units = ref<any[]>([])
|
||||
|
||||
const form = reactive({
|
||||
name: '',
|
||||
quantity: 1,
|
||||
unit_id: '',
|
||||
expiry_date: '',
|
||||
notes: ''
|
||||
})
|
||||
|
||||
// Load units
|
||||
onMounted(async () => {
|
||||
const { data } = await getUnits()
|
||||
units.value = data || []
|
||||
})
|
||||
|
||||
// Unit options for select
|
||||
const unitOptions = computed(() => {
|
||||
return units.value.map(unit => ({
|
||||
label: `${unit.name} (${unit.abbreviation})`,
|
||||
value: unit.id
|
||||
}))
|
||||
})
|
||||
|
||||
// Watch for item changes (open modal)
|
||||
watch(() => props.item, (newItem) => {
|
||||
if (newItem) {
|
||||
form.name = newItem.name
|
||||
form.quantity = Number(newItem.quantity)
|
||||
form.unit_id = newItem.unit_id
|
||||
form.expiry_date = newItem.expiry_date || ''
|
||||
form.notes = newItem.notes || ''
|
||||
isOpen.value = true
|
||||
}
|
||||
}, { immediate: true })
|
||||
|
||||
// Watch modal close
|
||||
watch(isOpen, (val) => {
|
||||
if (!val) {
|
||||
emit('close')
|
||||
}
|
||||
})
|
||||
|
||||
// Validation
|
||||
const isValid = computed(() => {
|
||||
return form.name.trim().length > 0 && form.quantity > 0 && form.unit_id
|
||||
})
|
||||
|
||||
const close = () => {
|
||||
isOpen.value = false
|
||||
}
|
||||
|
||||
// Submit
|
||||
const handleSubmit = async () => {
|
||||
if (!isValid.value || !props.item) return
|
||||
|
||||
submitting.value = true
|
||||
|
||||
const { data, error } = await updateInventoryItem(props.item.id, {
|
||||
name: form.name.trim(),
|
||||
quantity: form.quantity,
|
||||
unit_id: form.unit_id,
|
||||
expiry_date: form.expiry_date || null,
|
||||
notes: form.notes.trim() || null
|
||||
})
|
||||
|
||||
if (error) {
|
||||
alert('Failed to update item: ' + error.message)
|
||||
submitting.value = false
|
||||
return
|
||||
}
|
||||
|
||||
emit('updated', data)
|
||||
submitting.value = false
|
||||
close()
|
||||
}
|
||||
</script>
|
||||
151
app/components/inventory/InventoryCard.vue
Normal file
151
app/components/inventory/InventoryCard.vue
Normal file
@@ -0,0 +1,151 @@
|
||||
<template>
|
||||
<UCard class="hover:shadow-lg transition-shadow">
|
||||
<!-- Item Image -->
|
||||
<div class="aspect-square bg-gray-100 rounded-lg mb-3 overflow-hidden">
|
||||
<img
|
||||
v-if="item.product?.image_url"
|
||||
:src="item.product.image_url"
|
||||
:alt="item.name"
|
||||
class="w-full h-full object-cover"
|
||||
/>
|
||||
<div v-else class="w-full h-full flex items-center justify-center">
|
||||
<UIcon name="i-heroicons-cube" class="w-16 h-16 text-gray-300" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Item Info -->
|
||||
<div class="space-y-2">
|
||||
<div>
|
||||
<h3 class="font-semibold text-gray-900 truncate">{{ item.name }}</h3>
|
||||
<p v-if="item.product?.brand" class="text-sm text-gray-600 truncate">
|
||||
{{ item.product.brand }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- Quantity -->
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-lg font-medium text-gray-900">
|
||||
{{ item.quantity }} {{ item.unit?.abbreviation }}
|
||||
</span>
|
||||
|
||||
<!-- Quick Actions -->
|
||||
<div class="flex gap-1">
|
||||
<UButton
|
||||
icon="i-heroicons-minus"
|
||||
size="xs"
|
||||
color="gray"
|
||||
variant="ghost"
|
||||
@click="$emit('update-quantity', item.id, -1)"
|
||||
:disabled="item.quantity <= 1"
|
||||
/>
|
||||
<UButton
|
||||
icon="i-heroicons-plus"
|
||||
size="xs"
|
||||
color="gray"
|
||||
variant="ghost"
|
||||
@click="$emit('update-quantity', item.id, 1)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Tags -->
|
||||
<div v-if="item.tags && item.tags.length > 0" class="flex flex-wrap gap-1">
|
||||
<UBadge
|
||||
v-for="tagItem in item.tags.slice(0, 3)"
|
||||
:key="tagItem.tag.id"
|
||||
:style="{ backgroundColor: tagItem.tag.color }"
|
||||
size="xs"
|
||||
class="text-white"
|
||||
>
|
||||
{{ tagItem.tag.icon }} {{ tagItem.tag.name }}
|
||||
</UBadge>
|
||||
<UBadge v-if="item.tags.length > 3" size="xs" color="gray">
|
||||
+{{ item.tags.length - 3 }}
|
||||
</UBadge>
|
||||
</div>
|
||||
|
||||
<!-- Expiry Warning -->
|
||||
<div v-if="daysUntilExpiry !== null" class="text-xs">
|
||||
<UBadge
|
||||
:color="expiryColor"
|
||||
variant="soft"
|
||||
class="w-full justify-center"
|
||||
>
|
||||
<UIcon :name="expiryIcon" class="mr-1" />
|
||||
{{ expiryText }}
|
||||
</UBadge>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Action Buttons -->
|
||||
<template #footer>
|
||||
<div class="flex gap-2">
|
||||
<UButton
|
||||
icon="i-heroicons-pencil"
|
||||
size="sm"
|
||||
color="gray"
|
||||
variant="soft"
|
||||
class="flex-1"
|
||||
@click="$emit('edit', item)"
|
||||
>
|
||||
Edit
|
||||
</UButton>
|
||||
<UButton
|
||||
icon="i-heroicons-trash"
|
||||
size="sm"
|
||||
color="red"
|
||||
variant="soft"
|
||||
@click="$emit('delete', item.id)"
|
||||
>
|
||||
Delete
|
||||
</UButton>
|
||||
</div>
|
||||
</template>
|
||||
</UCard>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
const props = defineProps<{
|
||||
item: any
|
||||
}>()
|
||||
|
||||
defineEmits<{
|
||||
edit: [item: any]
|
||||
delete: [id: string]
|
||||
'update-quantity': [id: string, change: number]
|
||||
}>()
|
||||
|
||||
// Calculate days until expiry
|
||||
const daysUntilExpiry = computed(() => {
|
||||
if (!props.item.expiry_date) return null
|
||||
|
||||
const today = new Date()
|
||||
const expiry = new Date(props.item.expiry_date)
|
||||
const diff = Math.ceil((expiry.getTime() - today.getTime()) / (1000 * 60 * 60 * 24))
|
||||
|
||||
return diff
|
||||
})
|
||||
|
||||
// Expiry badge styling
|
||||
const expiryColor = computed(() => {
|
||||
if (daysUntilExpiry.value === null) return 'gray'
|
||||
if (daysUntilExpiry.value < 0) return 'red'
|
||||
if (daysUntilExpiry.value <= 3) return 'orange'
|
||||
if (daysUntilExpiry.value <= 7) return 'yellow'
|
||||
return 'green'
|
||||
})
|
||||
|
||||
const expiryIcon = computed(() => {
|
||||
if (daysUntilExpiry.value === null) return 'i-heroicons-calendar'
|
||||
if (daysUntilExpiry.value < 0) return 'i-heroicons-exclamation-triangle'
|
||||
return 'i-heroicons-clock'
|
||||
})
|
||||
|
||||
const expiryText = computed(() => {
|
||||
if (daysUntilExpiry.value === null) return 'No expiry'
|
||||
if (daysUntilExpiry.value < 0) return `Expired ${Math.abs(daysUntilExpiry.value)} days ago`
|
||||
if (daysUntilExpiry.value === 0) return 'Expires today'
|
||||
if (daysUntilExpiry.value === 1) return 'Expires tomorrow'
|
||||
return `Expires in ${daysUntilExpiry.value} days`
|
||||
})
|
||||
</script>
|
||||
131
app/components/inventory/InventoryList.vue
Normal file
131
app/components/inventory/InventoryList.vue
Normal file
@@ -0,0 +1,131 @@
|
||||
<template>
|
||||
<div class="space-y-4">
|
||||
<!-- Loading State -->
|
||||
<div v-if="loading" class="text-center py-12">
|
||||
<UIcon name="i-heroicons-arrow-path" class="w-8 h-8 text-gray-400 animate-spin mx-auto mb-2" />
|
||||
<p class="text-gray-600">Loading inventory...</p>
|
||||
</div>
|
||||
|
||||
<!-- Error State -->
|
||||
<div v-else-if="error" class="text-center py-12">
|
||||
<UIcon name="i-heroicons-exclamation-triangle" class="w-12 h-12 text-red-500 mx-auto mb-4" />
|
||||
<p class="text-red-600 mb-4">{{ error }}</p>
|
||||
<UButton @click="loadInventory" color="gray">Retry</UButton>
|
||||
</div>
|
||||
|
||||
<!-- Empty State -->
|
||||
<div v-else-if="!items || items.length === 0" class="text-center py-12">
|
||||
<UIcon name="i-heroicons-inbox" class="w-16 h-16 text-gray-400 mx-auto mb-4" />
|
||||
<h3 class="text-lg font-semibold text-gray-900 mb-2">
|
||||
No items yet
|
||||
</h3>
|
||||
<p class="text-gray-600 mb-6">
|
||||
Start by scanning a barcode or adding an item manually.
|
||||
</p>
|
||||
<div class="flex gap-2 justify-center">
|
||||
<UButton
|
||||
to="/scan"
|
||||
color="primary"
|
||||
icon="i-heroicons-qr-code"
|
||||
>
|
||||
Scan First Item
|
||||
</UButton>
|
||||
<UButton
|
||||
@click="$emit('add-item')"
|
||||
color="white"
|
||||
icon="i-heroicons-plus"
|
||||
>
|
||||
Add Manually
|
||||
</UButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Inventory Grid -->
|
||||
<div v-else class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
|
||||
<InventoryCard
|
||||
v-for="item in items"
|
||||
:key="item.id"
|
||||
:item="item"
|
||||
@edit="$emit('edit-item', item)"
|
||||
@delete="handleDelete(item.id)"
|
||||
@update-quantity="handleQuantityUpdate"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
const { getInventory, deleteInventoryItem, updateQuantity } = useInventory()
|
||||
|
||||
const props = defineProps<{
|
||||
refresh?: boolean
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'add-item': []
|
||||
'edit-item': [item: any]
|
||||
}>()
|
||||
|
||||
const items = ref<any[]>([])
|
||||
const loading = ref(true)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
const loadInventory = async () => {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
|
||||
const { data, error: fetchError } = await getInventory()
|
||||
|
||||
if (fetchError) {
|
||||
error.value = 'Failed to load inventory. Please try again.'
|
||||
loading.value = false
|
||||
return
|
||||
}
|
||||
|
||||
items.value = data || []
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!confirm('Are you sure you want to delete this item?')) {
|
||||
return
|
||||
}
|
||||
|
||||
const { error: deleteError } = await deleteInventoryItem(id)
|
||||
|
||||
if (deleteError) {
|
||||
alert('Failed to delete item')
|
||||
return
|
||||
}
|
||||
|
||||
// Remove from local list
|
||||
items.value = items.value.filter(item => item.id !== id)
|
||||
}
|
||||
|
||||
const handleQuantityUpdate = async (id: string, change: number) => {
|
||||
const result = await updateQuantity(id, change)
|
||||
|
||||
if (result.error) {
|
||||
alert('Failed to update quantity')
|
||||
return
|
||||
}
|
||||
|
||||
// Reload inventory after update
|
||||
await loadInventory()
|
||||
}
|
||||
|
||||
// Load on mount
|
||||
onMounted(loadInventory)
|
||||
|
||||
// Watch for refresh prop
|
||||
watch(() => props.refresh, (newVal) => {
|
||||
if (newVal) {
|
||||
loadInventory()
|
||||
}
|
||||
})
|
||||
|
||||
// Expose reload method
|
||||
defineExpose({
|
||||
reload: loadInventory
|
||||
})
|
||||
</script>
|
||||
201
app/composables/useInventory.ts
Normal file
201
app/composables/useInventory.ts
Normal file
@@ -0,0 +1,201 @@
|
||||
import type { Database } from '~/types/database.types'
|
||||
|
||||
type InventoryItem = Database['public']['Tables']['inventory_items']['Row']
|
||||
type InventoryItemInsert = Database['public']['Tables']['inventory_items']['Insert']
|
||||
type InventoryItemUpdate = Database['public']['Tables']['inventory_items']['Update']
|
||||
|
||||
export const useInventory = () => {
|
||||
const supabase = useSupabase()
|
||||
const { user } = useSupabaseAuth()
|
||||
|
||||
/**
|
||||
* Get all inventory items with denormalized data
|
||||
*/
|
||||
const getInventory = async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('inventory_items')
|
||||
.select(`
|
||||
*,
|
||||
product:products(*),
|
||||
unit:units(*),
|
||||
tags:item_tags(tag:tags(*))
|
||||
`)
|
||||
.order('created_at', { ascending: false })
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching inventory:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get single inventory item by ID
|
||||
*/
|
||||
const getInventoryItem = async (id: string) => {
|
||||
const { data, error } = await supabase
|
||||
.from('inventory_items')
|
||||
.select(`
|
||||
*,
|
||||
product:products(*),
|
||||
unit:units(*),
|
||||
tags:item_tags(tag:tags(*))
|
||||
`)
|
||||
.eq('id', id)
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching item:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new inventory item
|
||||
*/
|
||||
const addInventoryItem = async (item: Omit<InventoryItemInsert, 'added_by'>) => {
|
||||
if (!user.value) {
|
||||
return { data: null, error: { message: 'User not authenticated' } }
|
||||
}
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('inventory_items')
|
||||
.insert({
|
||||
...item,
|
||||
added_by: user.value.id
|
||||
})
|
||||
.select(`
|
||||
*,
|
||||
product:products(*),
|
||||
unit:units(*),
|
||||
tags:item_tags(tag:tags(*))
|
||||
`)
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
console.error('Error adding item:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Update inventory item
|
||||
*/
|
||||
const updateInventoryItem = async (id: string, updates: InventoryItemUpdate) => {
|
||||
const { data, error } = await supabase
|
||||
.from('inventory_items')
|
||||
.update(updates)
|
||||
.eq('id', id)
|
||||
.select(`
|
||||
*,
|
||||
product:products(*),
|
||||
unit:units(*),
|
||||
tags:item_tags(tag:tags(*))
|
||||
`)
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating item:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete inventory item
|
||||
*/
|
||||
const deleteInventoryItem = async (id: string) => {
|
||||
// First delete associated tags
|
||||
await supabase
|
||||
.from('item_tags')
|
||||
.delete()
|
||||
.eq('item_id', id)
|
||||
|
||||
const { error } = await supabase
|
||||
.from('inventory_items')
|
||||
.delete()
|
||||
.eq('id', id)
|
||||
|
||||
if (error) {
|
||||
console.error('Error deleting item:', error)
|
||||
return { error }
|
||||
}
|
||||
|
||||
return { error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Update item quantity (consume or restock)
|
||||
*/
|
||||
const updateQuantity = async (id: string, change: number) => {
|
||||
const { data: item, error: fetchError } = await getInventoryItem(id)
|
||||
if (fetchError || !item) {
|
||||
return { data: null, error: fetchError }
|
||||
}
|
||||
|
||||
const newQuantity = Number(item.quantity) + change
|
||||
|
||||
if (newQuantity <= 0) {
|
||||
// Auto-delete when quantity reaches zero
|
||||
return await deleteInventoryItem(id)
|
||||
}
|
||||
|
||||
return await updateInventoryItem(id, { quantity: newQuantity })
|
||||
}
|
||||
|
||||
/**
|
||||
* Add tags to item
|
||||
*/
|
||||
const addItemTags = async (itemId: string, tagIds: string[]) => {
|
||||
const items = tagIds.map(tagId => ({
|
||||
item_id: itemId,
|
||||
tag_id: tagId
|
||||
}))
|
||||
|
||||
const { error } = await supabase
|
||||
.from('item_tags')
|
||||
.insert(items)
|
||||
|
||||
if (error) {
|
||||
console.error('Error adding tags:', error)
|
||||
return { error }
|
||||
}
|
||||
|
||||
return { error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove tag from item
|
||||
*/
|
||||
const removeItemTag = async (itemId: string, tagId: string) => {
|
||||
const { error } = await supabase
|
||||
.from('item_tags')
|
||||
.delete()
|
||||
.eq('item_id', itemId)
|
||||
.eq('tag_id', tagId)
|
||||
|
||||
if (error) {
|
||||
console.error('Error removing tag:', error)
|
||||
return { error }
|
||||
}
|
||||
|
||||
return { error: null }
|
||||
}
|
||||
|
||||
return {
|
||||
getInventory,
|
||||
getInventoryItem,
|
||||
addInventoryItem,
|
||||
updateInventoryItem,
|
||||
deleteInventoryItem,
|
||||
updateQuantity,
|
||||
addItemTags,
|
||||
removeItemTag
|
||||
}
|
||||
}
|
||||
44
app/composables/useTags.ts
Normal file
44
app/composables/useTags.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
export const useTags = () => {
|
||||
const supabase = useSupabase()
|
||||
|
||||
/**
|
||||
* Get all tags
|
||||
*/
|
||||
const getTags = async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('tags')
|
||||
.select('*')
|
||||
.order('category', { ascending: true })
|
||||
.order('name', { ascending: true })
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching tags:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tags by category
|
||||
*/
|
||||
const getTagsByCategory = async (category: 'position' | 'type' | 'dietary' | 'custom') => {
|
||||
const { data, error } = await supabase
|
||||
.from('tags')
|
||||
.select('*')
|
||||
.eq('category', category)
|
||||
.order('name', { ascending: true })
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching tags by category:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
return {
|
||||
getTags,
|
||||
getTagsByCategory
|
||||
}
|
||||
}
|
||||
53
app/composables/useUnits.ts
Normal file
53
app/composables/useUnits.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
export const useUnits = () => {
|
||||
const supabase = useSupabase()
|
||||
|
||||
/**
|
||||
* Get all units
|
||||
*/
|
||||
const getUnits = async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('units')
|
||||
.select('*')
|
||||
.order('unit_type', { ascending: true })
|
||||
.order('name', { ascending: true })
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching units:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default unit for a type
|
||||
*/
|
||||
const getDefaultUnit = async (unitType: 'weight' | 'volume' | 'count' | 'custom') => {
|
||||
const { data, error } = await supabase
|
||||
.from('units')
|
||||
.select('*')
|
||||
.eq('unit_type', unitType)
|
||||
.eq('is_default', true)
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching default unit:', error)
|
||||
return { data: null, error }
|
||||
}
|
||||
|
||||
return { data, error: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert quantity between units
|
||||
*/
|
||||
const convertUnit = (quantity: number, fromFactor: number, toFactor: number): number => {
|
||||
return (quantity * fromFactor) / toFactor
|
||||
}
|
||||
|
||||
return {
|
||||
getUnits,
|
||||
getDefaultUnit,
|
||||
convertUnit
|
||||
}
|
||||
}
|
||||
@@ -17,36 +17,37 @@
|
||||
color="white"
|
||||
size="lg"
|
||||
icon="i-heroicons-plus"
|
||||
@click="showAddForm = true"
|
||||
>
|
||||
Add Manually
|
||||
</UButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Empty State -->
|
||||
<UCard v-if="true">
|
||||
<div class="text-center py-12">
|
||||
<UIcon
|
||||
name="i-heroicons-inbox"
|
||||
class="w-16 h-16 text-gray-400 mx-auto mb-4"
|
||||
<!-- Add Item Form (Overlay) -->
|
||||
<div v-if="showAddForm" class="fixed inset-0 z-50 flex items-start justify-center pt-20 px-4 bg-black/50">
|
||||
<div class="w-full max-w-lg">
|
||||
<AddItemForm
|
||||
@close="showAddForm = false"
|
||||
@added="handleItemAdded"
|
||||
/>
|
||||
<h3 class="text-lg font-semibold text-gray-900 mb-2">
|
||||
No items yet
|
||||
</h3>
|
||||
<p class="text-gray-600 mb-6">
|
||||
Start by scanning a barcode or adding an item manually.
|
||||
</p>
|
||||
<UButton
|
||||
to="/scan"
|
||||
color="primary"
|
||||
icon="i-heroicons-qr-code"
|
||||
>
|
||||
Scan First Item
|
||||
</UButton>
|
||||
</div>
|
||||
</UCard>
|
||||
</div>
|
||||
|
||||
<!-- TODO: Item list will go here -->
|
||||
<!-- Edit Item Modal -->
|
||||
<EditItemModal
|
||||
:item="editingItem"
|
||||
@close="editingItem = null"
|
||||
@updated="handleItemUpdated"
|
||||
/>
|
||||
|
||||
<!-- Inventory List -->
|
||||
<InventoryList
|
||||
ref="inventoryListRef"
|
||||
:refresh="refreshKey"
|
||||
@add-item="showAddForm = true"
|
||||
@edit-item="editingItem = $event"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -54,4 +55,20 @@
|
||||
definePageMeta({
|
||||
layout: 'default'
|
||||
})
|
||||
|
||||
const showAddForm = ref(false)
|
||||
const editingItem = ref<any>(null)
|
||||
const refreshKey = ref(0)
|
||||
const inventoryListRef = ref()
|
||||
|
||||
const handleItemAdded = (item: any) => {
|
||||
showAddForm.value = false
|
||||
// Reload the inventory list
|
||||
inventoryListRef.value?.reload()
|
||||
}
|
||||
|
||||
const handleItemUpdated = (item: any) => {
|
||||
editingItem.value = null
|
||||
inventoryListRef.value?.reload()
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user