Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Torkelsen committed Nov 6, 2024
2 parents 99546e1 + d994911 commit a37c155
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
28 changes: 25 additions & 3 deletions handlenett-frontend/components/Item.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<template>
<div class="groceryitem">
<label :class="{ 'complete': status }">
<input type="checkbox" :checked="status" @change="update"> {{ props.element?.name }}
</label>
<div>
<label :class="{ 'complete': status }">
<input type="checkbox" :checked="status" @change="update"> {{ props.element?.name }}
</label>
<span @click="lmfgi">❔</span>
<div class="gi-creator">📝{{ createdBy }}</div>
</div>
<div class="button-group">
<button v-if="false" class="btn primary" @click="editeleement">✏️
</button>
Expand All @@ -27,6 +31,10 @@ onMounted(() => {
status.value = props.element.isCompleted
})
const lmfgi = () => {
window.open(`https://www.google.com/search?q=bunnpris ${props.element.name}&udm=2`, '_blank')
}
const update = () => {
status.value = !status.value;
emit('changed', { id: props.element.id, name: props.element.name, isCompleted: status.value })
Expand All @@ -35,6 +43,14 @@ const deleteeleement = () => {
emit('delete', { id: props.element.id })
}
const createdBy = computed(() => {
const fullEmail = props.element.createdBy;
const dottedName = fullEmail.split('@')[0];
const firstName = dottedName.split('.')[0];
return firstName.charAt(0).toUpperCase() + firstName.slice(1);
});
</script>
<style scoped>
.groceryitem {
Expand All @@ -46,6 +62,12 @@ const deleteeleement = () => {
font-weight: bold;
}
.gi-creator {
font-size: 0.8rem;
font-weight: 100;
padding-left: 1rem;
}
.button-group {
display: flex;
gap: 1rem;
Expand Down
12 changes: 9 additions & 3 deletions handlenett-frontend/components/NewItem.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<template>
<form @submit.prevent="update">
<input type="text" placeholder="Trenger vi noe mer?" v-model="item">
<input type="text" placeholder="Trenger vi noe mer?" @keyup="typing" v-model="item">
<button class="btn" @click="update">🛒</button>
</form>
</template>
<script setup>
import { ref, defineEmits } from 'vue'
import { ref } from 'vue'
const item = ref('')
const emit = defineEmits(['changed'])
const emit = defineEmits(['changed', 'typing'])
const update = () => {
emit('changed', { name: item.value, isComplete: false })
item.value = ''
}
const typing = () => {
if (item.value.length !== 1) {
emit('typing', item.value)
}
}
</script>
<style scoped>
form {
Expand Down
13 changes: 11 additions & 2 deletions handlenett-frontend/composables/useHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ export default async function (url: string, method: string, data = {}) {
mode: "cors",
body: !!data && method != "GET" ? JSON.stringify(data) : null,
});
return response.json();

const responseText = await response.text();
try {
const responseJson = JSON.parse(responseText);
return responseJson;
} catch (e) {
// return without parsing.
// might be empty
return responseText;
}
} catch (error) {
console.info(error);
console.error("useHttp catch error", error);
return Promise.reject(error);
}
}
17 changes: 14 additions & 3 deletions handlenett-frontend/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
<div class="loader"></div>
</div>
<div v-else>
<NewItem @changed="newItem" @typing="typing"></NewItem>
<div v-if="itemsContainingSequence.length > 0">
Vi har allerede disse tilgjengelig for valg: {{ itemsContainingSequence }}
</div>

<div style="margin-bottom: 2rem;">
<Item v-for="i in items" :key="i.id" @changed="updatedItem" @delete="deleteItem"
:isElementDeletable="currentUser === i.createdBy" :isElementEditable="currentUser === i.createdBy"
:element="i" />
</div>
<NewItem @changed="newItem"></NewItem>
</div>
<!-- <pre>
{{ items }}
Expand All @@ -22,6 +26,7 @@ const { $getAccounts } = useNuxtApp();
const items = ref([])
const loading = ref(true)
const currentUser = ref("")
const itemsContainingSequence = ref([])
onMounted(async () => {
Expand Down Expand Up @@ -53,7 +58,13 @@ const updatedItem = (updatedItem) => {
items.value[idx] = data;
});
}
const typing = (item) => {
if (item === '') {
itemsContainingSequence.value = []
return
}
itemsContainingSequence.value = items.value.filter(i => i.name.toLowerCase().includes(item.toLowerCase())).map(i => i.name)
}
const newItem = (newItem) => {
if (newItem.name === '') return
Expand All @@ -64,7 +75,7 @@ const newItem = (newItem) => {
}
const deleteItem = (deleteItem) => {
useHttp(`Item/${deleteItem.id}`, 'DELETE').then(data => {
useHttp(`Item/${deleteItem.id}`, 'DELETE').then(() => {
let i = items.value.find(i => i.id === deleteItem.id)
const idx = items.value.indexOf(i)
items.value.splice(idx, 1)
Expand Down

0 comments on commit a37c155

Please sign in to comment.