add edit items
continuous-integration/drone/push Build is passing Details

master
adb-sh 2 years ago
parent d5b29bdc91
commit 42a82a2b31

@ -0,0 +1,51 @@
<script setup lang="ts">
import { useShoppingListStore } from '../store/shoppingList';
import { ref, reactive, defineProps } from 'vue';
import { useRouter } from 'vue-router';
const store = useShoppingListStore();
const router = useRouter();
const props = defineProps({
item: Object,
});
const isValid = ref(false);
const item = reactive({
name: props.item.name,
price: props.item.price / 100,
user: props.item.user.name,
});
const form = ref(null);
const submit = () => {
props.item.name = item.name;
props.item.price = item.price * 100;
props.item.user = store.users.find(user => user.name === item.user);
router.push('/shopping-list');
};
</script>
<template>
<v-form ref="form" v-model="isValid" lazy-validation>
<v-text-field
label="Item Name"
required
v-model="item.name"
/>
<v-text-field
label="Price"
suffix="€"
required
v-model="item.price"
/>
<v-select
label="User"
:items="store.users.map(user => user.name)"
required
v-model="item.user"
/>
<v-btn color="primary" @click="submit()">edit item</v-btn>
</v-form>
</template>

@ -12,9 +12,13 @@ defineProps({
<div v-for="(items, user) of groupItemsByUser(items)"> <div v-for="(items, user) of groupItemsByUser(items)">
<v-list-subheader>{{ user }}</v-list-subheader> <v-list-subheader>{{ user }}</v-list-subheader>
<div v-for="item in items"> <div v-for="item in items">
<v-list-item @click="item.isBought = !item.isBought"> <v-list-item>
<v-list-item-title>{{ item.name }}</v-list-item-title> <v-list-item-title>{{ item.name }}</v-list-item-title>
<v-list-item-subtitle>{{ item.price / 100 }} </v-list-item-subtitle> <v-list-item-subtitle>{{ item.price / 100 }} </v-list-item-subtitle>
<template v-slot:append>
<v-btn icon="mdi-pencil" variant="text" :to="`/edit-item/${ item.id }`"/>
<v-btn icon="mdi-check" variant="text" @click="item.isBought = !item.isBought"/>
</template>
</v-list-item> </v-list-item>
</div> </div>
</div> </div>

@ -2,6 +2,7 @@ import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import HomeView from '../views/HomeView.vue'; import HomeView from '../views/HomeView.vue';
import ShoppingList from '../views/ShoppingList.vue'; import ShoppingList from '../views/ShoppingList.vue';
import AddItem from '../views/AddItem.vue'; import AddItem from '../views/AddItem.vue';
import EditItem from '../views/EditItem.vue';
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ {
@ -19,6 +20,11 @@ const routes: Array<RouteRecordRaw> = [
name: "add-item", name: "add-item",
component: AddItem, component: AddItem,
}, },
{
path: "/edit-item/:id",
name: "edit-item",
component: EditItem,
},
]; ];
const router = createRouter({ const router = createRouter({

@ -14,17 +14,20 @@ export const dummyList = [
price: 200, price: 200,
user: lukas, user: lukas,
isBought: false, isBought: false,
id: 0,
}, },
{ {
name: 'Apple', name: 'Apple',
price: 150, price: 150,
user: lukas, user: lukas,
isBought: false, isBought: false,
id: 1,
}, },
{ {
name: 'Butter', name: 'Butter',
price: 149, price: 149,
user: laura, user: laura,
isBought: false, isBought: false,
id: 2,
}, },
]; ];

@ -10,6 +10,7 @@ type ShoppingItem = {
price: number; price: number;
isBought: boolean; isBought: boolean;
user: User; user: User;
id: number;
}; };
export const useShoppingListStore = defineStore('shoppingList', { export const useShoppingListStore = defineStore('shoppingList', {

@ -0,0 +1,24 @@
<script setup lang="ts">
import DefaultPage from '../components/DefaultPage.vue';
import EditItemForm from '../components/EditItemForm.vue';
import { useRoute } from 'vue-router';
import { useShoppingListStore} from '../store/shoppingList';
import { ref } from 'vue';
const route = useRoute();
const store = useShoppingListStore();
const item = ref(store.items.find(item => item.id === Number(route.params.id)));
</script>
<template>
<DefaultPage title="Add Item">
<v-container>
<EditItemForm v-if="item" :item="item"/>
<div v-else>
<p>This item does not exist.</p>
<v-btn color="primary" to="/shopping-list">back to shopping list</v-btn>
</div>
</v-container>
</DefaultPage>
</template>
Loading…
Cancel
Save