Skip to content

Commit

Permalink
feat: Show checklist items
Browse files Browse the repository at this point in the history
Fixes #61
  • Loading branch information
Perdolique committed Aug 6, 2024
1 parent 28a0e01 commit dc3bd20
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 32 deletions.
39 changes: 39 additions & 0 deletions app/components/checklists/ChecklistItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<template>
<div :class="$style.component">
<div>
{{ item.equipment.name}}
</div>

<div :class="$style.weight">
{{ item.equipment.weight }}
</div>
</div>
</template>

<script lang="ts" setup>
import type { ChecklistItemModel } from '~/models/checklist';
interface Props {
readonly item: ChecklistItemModel;
}
defineProps<Props>();
</script>

<style module>
.component {
display: grid;
grid-template-columns: 1fr auto;
padding: var(--border-radius-8) var(--spacing-12);
background-color: var(--color-blue-100);
border: 1px solid var(--color-blue-300);
border-radius: var(--border-radius-12);
font-size: var(--font-size-16);
color: var(--color-blue-800);
}
.weight {
color: var(--color-blue-700);
font-size: var(--font-size-14);
}
</style>
28 changes: 28 additions & 0 deletions app/components/checklists/ChecklistItemsList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div :class="$style.component">
<ChecklistItem
v-for="item in items"
:key="item.id"
:item="item"
/>
</div>
</template>

<script lang="ts" setup>
import type { ChecklistItemModel } from '~/models/checklist';
import ChecklistItem from './ChecklistItem.vue';
interface Props {
readonly items: ChecklistItemModel[];
}
defineProps<Props>();
</script>

<style module>
.component {
width: 100%;
display: grid;
row-gap: var(--spacing-4);
}
</style>
28 changes: 3 additions & 25 deletions app/composables/use-checklist-items-data.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
interface EquipmentItem {
readonly id: string;
readonly name: string;
readonly weight: number;
}

interface ChecklistItem {
readonly id: string;
readonly equipment: EquipmentItem;
}
import type { ChecklistItemModel } from "~/models/checklist"

export async function useChecklistItemsData(checklistId: string) {
const items = ref<ChecklistItem[]>([])
const items = ref<ChecklistItemModel[]>([])
const { data } = await useFetch(`/api/checklists/${checklistId}/items`)

if (data.value !== undefined) {
items.value = data.value
}

async function updateItems() {
try {
const response = await $fetch(`/api/checklists/${checklistId}/items`)

items.value = response
} catch {
console.error('Failed to update equipment')
}
}

return {
items,
updateItems
}
return items
}
10 changes: 10 additions & 0 deletions app/models/checklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interface ChecklistEquipment {
readonly id: string;
readonly name: string;
readonly weight: number;
}

export interface ChecklistItemModel {
readonly id: string;
readonly equipment: ChecklistEquipment;
}
16 changes: 11 additions & 5 deletions app/pages/checklists/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
</SearchOptionAdd>
</template>
</PerdSearch>

<ChecklistItemsList :items="items" />
</div>
</PageContent>
</template>
Expand All @@ -32,6 +34,7 @@
import PerdButton from '~/components/PerdButton.vue';
import SearchOptionAdd from '~/components/PerdSearch/SearchOptionAdd.vue';
import PerdSearch from '~/components/PerdSearch/PerdSearch.vue';
import ChecklistItemsList from '~/components/checklists/ChecklistItemsList.vue';
interface InventoryItem {
readonly id: string;
Expand All @@ -45,7 +48,7 @@
const options = ref<InventoryItem[]>([]);
const isSearching = ref(false);
const { data: checklistData } = await useFetch(`/api/checklists/${checklistId}`)
const { items, updateItems } = await useChecklistItemsData(checklistId)
const items = await useChecklistItemsData(checklistId)
if (checklistData.value !== undefined) {
name.value = checklistData.value.name
Expand Down Expand Up @@ -99,7 +102,7 @@
async function addItem(item: InventoryItem) {
try {
await $fetch(`/api/checklists/${checklistId}/items`, {
const insertedItem = await $fetch(`/api/checklists/${checklistId}/items`, {
method: 'POST',
body: {
Expand All @@ -109,9 +112,12 @@
options.value = options.value.filter(({ id }) => id !== item.id)
await updateItems()
console.log('items', items.value);
if (insertedItem !== undefined) {
items.value.push({
id: insertedItem.id,
equipment: insertedItem.equipment
})
}
} catch (error) {
console.error(error)
}
Expand Down
26 changes: 25 additions & 1 deletion server/api/checklists/[id]/items.post.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { eq } from 'drizzle-orm'
import * as v from 'valibot'

const bodySchema = v.object({
Expand All @@ -23,14 +24,37 @@ export default defineEventHandler(async (event) => {
const { equipmentId } = await readValidatedBody(event, validateBody)

try {
await db
const [{ itemId }] = await db
.insert(tables.checklistItems)
.values({
checklistId,
equipmentId
})
.returning({
itemId: tables.checklistItems.id
})

const insertedItem = await db.query.checklistItems.findFirst({
where: eq(tables.checklistItems.id, itemId),

columns: {
id: true
},

with: {
equipment: {
columns: {
id: true,
name: true,
weight: true
}
}
}
})

setResponseStatus(event, 201)

return insertedItem
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
Expand Down
49 changes: 48 additions & 1 deletion server/database/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sql } from 'drizzle-orm'
import { relations, sql } from 'drizzle-orm'
import { ulid } from 'ulid'

import {
Expand Down Expand Up @@ -183,3 +183,50 @@ export const checklistItems = sqliteTable('checklistItems', {
}, (table) => ({
checklistIdEquipmentIdKey: unique().on(table.checklistId, table.equipmentId)
}))

/**
* Relations
*/

export const usersRelations = relations(users, ({ many }) => ({
userEquipment: many(userEquipment),
checklists: many(checklists)
}))

export const equipmentRelations = relations(equipment, ({ many }) => ({
userEquipment: many(userEquipment),
checklistItems: many(checklistItems)
}))

export const userEquipmentRelations = relations(userEquipment, ({ one }) => ({
user: one(users, {
fields: [userEquipment.userId],
references: [users.id]
}),

equipment: one(equipment, {
fields: [userEquipment.equipmentId],
references: [equipment.id]
})
}))

export const checklistsRelations = relations(checklists, ({ many, one }) => ({
checklistItems: many(checklistItems),

user: one(users, {
fields: [checklists.userId],
references: [users.id]
})
}))

export const checklistItemsRelations = relations(checklistItems, ({ one }) => ({
checklists: one(checklists, {
fields: [checklistItems.checklistId],
references: [checklists.id]
}),

equipment: one(equipment, {
fields: [checklistItems.equipmentId],
references: [equipment.id]
})
}))

0 comments on commit dc3bd20

Please sign in to comment.