add: reactive store

Add useLocalStorage from VueUse
This commit is contained in:
Fiona Lena Urban 2025-05-10 14:32:40 +02:00
parent 27f051cf14
commit 9498911e7a
4 changed files with 81 additions and 98 deletions

View file

@ -33,21 +33,21 @@
<div class="info flex-col"> <div class="info flex-col">
<div class="price"> <div class="price">
<Icon class="icon" name="uil:toilet-paper" mode="svg" /> <Icon class="icon" name="uil:toilet-paper" mode="svg" />
<span class="value">{{ intl.format(card.ppr) }}</span> <span class="value">{{ intl.format(ppr) }}</span>
</div> </div>
<span class="pro">Pro 1 {{ card.roles ? `(${card.roles})` : '' }}</span> <span class="pro">Pro 1 {{ card.roles ? `(${card.roles})` : '' }}</span>
</div> </div>
<div class="info flex-col"> <div class="info flex-col">
<div class="price"> <div class="price">
<Icon class="icon" name="uil:file-landscape" mode="svg" /> <Icon class="icon" name="uil:file-landscape" mode="svg" />
<span class="value">{{ intl.format(card.pps) }}</span> <span class="value">{{ intl.format(pps) }}</span>
</div> </div>
<span class="pro">Pro 100 {{ card.sheets ? `(${card.sheets})` : '' }}</span> <span class="pro">Pro 100 {{ card.sheets ? `(${card.sheets})` : '' }}</span>
</div> </div>
<div class="info flex-col"> <div class="info flex-col">
<div class="price"> <div class="price">
<Icon class="icon" name="uil:layer-group" mode="svg" /> <Icon class="icon" name="uil:layer-group" mode="svg" />
<span class="value">{{ intl.format(card.ppl) }}</span> <span class="value">{{ intl.format(ppl) }}</span>
</div> </div>
<span class="pro">Pro 100 {{ card.layers ? `(${card.layers})` : '' }}</span> <span class="pro">Pro 100 {{ card.layers ? `(${card.layers})` : '' }}</span>
</div> </div>
@ -92,7 +92,17 @@ const { lengthX, direction, isSwiping } = useSwipe(top, {
}, },
}) })
const priceClean = computed<number>(() => +replaceComma(card.price))
const ppr = computed(() => priceClean.value / +card.roles)
const pps = computed(() => (ppr.value / +card.sheets) * 100)
const ppl = computed(() => (pps.value / +card.layers))
const update = () => emit('update') const update = () => emit('update')
const deleteCard = () => emit('remove') const deleteCard = () => emit('remove')
defineExpose({
ppr, pps, ppl, uuid : card.uuid,
})
</script> </script>

View file

@ -83,7 +83,6 @@ const { currentCardIndex, currentCard } = defineProps<Props>()
const emit = defineEmits(['update']) const emit = defineEmits(['update'])
const dialog = useTemplateRef<HTMLDialogElement>('dialog') const dialog = useTemplateRef<HTMLDialogElement>('dialog')
const cardLabel = computed(() => currentCardIndex > -1 ? 'Bearbeiten' : 'Hinzufügen')
const checkPrice = () => { const checkPrice = () => {
if (!currentCard) { return false } if (!currentCard) { return false }

View file

@ -1,61 +1,66 @@
<template> <template>
<PpDeleteDialog <ClientOnly>
ref="deleteModal" <PpDeleteDialog
:current-card-index="currentCardIndex" ref="deleteModal"
@delete="removeCard(currentCardIndex)" :current-card-index="currentCardIndex"
/> @delete="removeCard(currentCardIndex)"
<PpPriceCardDialog />
ref="modal" <PpPriceCardDialog
:current-card="currentCard" ref="modal"
:current-card-index="currentCardIndex" :current-card="currentCard"
@update="updateCard()" :current-card-index="currentCardIndex"
/> @update="updateCard()"
<section class="content flex-col"> />
<aside class="filter-bar"> <section class="content flex-col">
<PpButtonGroup <aside class="filter-bar">
:buttons="filterButtons" <PpButtonGroup
@click="sort" :buttons="filterButtons"
/> @click="sort"
</aside> />
<div class="flex-col" role="list"> </aside>
<PpPriceCard <div class="flex-col" role="list">
v-for="(card, index) in cards" <PpPriceCard
:key="card.uuid" ref="priceCard"
:deletable="cards.length > 1" v-for="(card, index) in cards"
:card="card" :key="card.uuid"
@update="openModal(false, index)" :deletable="cards.length > 1"
@remove="openDeleteModal()" :card="card"
/> @update="openModal(false, index)"
</div> @remove="openDeleteModal()"
</section> />
<PpToolbar> </div>
<PpButton class="mini-button text-white transparent" @click="sort(currentSort)"> </section>
<Icon class="icon" name="uil:refresh" mode="svg" /> <PpToolbar>
<span>Neu sortieren</span> <PpButton class="mini-button text-white transparent" @click="sort(currentSort)">
<span <Icon class="icon" name="uil:refresh" mode="svg" />
class="dot" <span>Neu sortieren</span>
:class="{ visible : isDirty}" <span
aria-hidden="true" class="dot"
/> :class="{ visible : isDirty}"
</PpButton> aria-hidden="true"
<PpButton class="mini-button text-white transparent" @click="openModal(true, -1)"> />
<Icon class="icon" name="uil:plus" mode="svg" /> </PpButton>
<span>Hinzufügen</span> <PpButton class="mini-button text-white transparent" @click="openModal(true, -1)">
</PpButton> <Icon class="icon" name="uil:plus" mode="svg" />
</PpToolbar> <span>Hinzufügen</span>
</PpButton>
</PpToolbar>
</ClientOnly>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import type { Card } from '../../shared/Card' import type { Card } from '../../shared/Card'
import type { Button } from '../../shared/ButtonGroup' import type { Button } from '../../shared/ButtonGroup'
import { PpPriceCardDialog, PpDeleteDialog } from '#components' import { PpPriceCardDialog, PpDeleteDialog, PpPriceCard } from '#components'
const currentSort = ref(0) const cards = useLocalStorage<Card[]>('cards', [])
const currentSort = useLocalStorage<number>('sort', 0)
const isDirty = ref(false) const isDirty = ref(false)
const currentCard = ref<Card>() const currentCard = ref<Card>()
const currentCardIndex = ref<number>(-1) const currentCardIndex = ref<number>(-1)
const modal = useTemplateRef<typeof PpPriceCardDialog>('modal') const modal = useTemplateRef<typeof PpPriceCardDialog>('modal')
const deleteModal = useTemplateRef<typeof PpDeleteDialog>('deleteModal') const deleteModal = useTemplateRef<typeof PpDeleteDialog>('deleteModal')
const priceCards = useTemplateRef<(typeof PpPriceCard)[]>('priceCard')
const createCard = (uuid : string) : Card => ({ const createCard = (uuid : string) : Card => ({
uuid, uuid,
@ -64,26 +69,17 @@ const createCard = (uuid : string) : Card => ({
roles: '', roles: '',
sheets: '', sheets: '',
layers: '', layers: '',
ppr: 0,
pps: 0,
ppl: 0,
}) })
const cards = useState<Card[]>('cards', () => [
createCard(randomUUID()),
])
const addCard = (card : Card) => { const addCard = (card : Card) => {
const price = calculate(card) cards.value.unshift({ ...card })
cards.value.unshift({ ...card, ...price })
isDirty.value = true isDirty.value = true
updateLocalStorage()
} }
const removeCard = (index : number) => { const removeCard = (index : number) => {
cards.value.splice(index, 1) cards.value.splice(index, 1)
isDirty.value = true isDirty.value = true
updateLocalStorage()
} }
const updateCard = () => { const updateCard = () => {
@ -92,11 +88,9 @@ const updateCard = () => {
return return
} }
const price = calculate(currentCard.value!) const newCard = { ...currentCard.value! }
const newCard = { ...currentCard.value!, ...price }
cards.value.splice(currentCardIndex.value, 1, newCard) cards.value.splice(currentCardIndex.value, 1, newCard)
isDirty.value = true isDirty.value = true
updateLocalStorage()
} }
const openModal = (createNew : boolean, index : number) => { const openModal = (createNew : boolean, index : number) => {
@ -118,31 +112,31 @@ const openDeleteModal = () => {
deleteModal.value?.$el.showModal() deleteModal.value?.$el.showModal()
} }
const updateLocalStorage = () => {
localStorage.setItem('cards', JSON.stringify(cards.value.map(card => {
const { uuid, name, price, roles, sheets, layers } = card
return { uuid, name, price, roles, sheets, layers }
})))
localStorage.setItem('sort', JSON.stringify(currentSort.value))
}
const filterButtons = ref<Button[]>([ const filterButtons = ref<Button[]>([
{ {
label: 'Rollen', label: 'Rollen',
icon: 'uil:toilet-paper', icon: 'uil:toilet-paper',
active: currentSort.value === 0,
}, },
{ {
label: 'Blatt', label: 'Blatt',
icon: 'uil:file-landscape', icon: 'uil:file-landscape',
active: currentSort.value === 1,
}, },
{ {
label: 'Lagen', label: 'Lagen',
icon: 'uil:layer-group', icon: 'uil:layer-group',
active: currentSort.value === 2,
}, },
]) ])
const sortBy = (key : 'ppr' | 'pps' | 'ppl') => { const sortBy = (key : 'ppr' | 'pps' | 'ppl') => {
cards.value.sort((a : Card, b : Card) => a[key] - b[key]) cards.value.sort((a, b) => {
const aCard = priceCards.value?.find(card => card.uuid === a.uuid) || null
const bCard = priceCards.value?.find(card => card.uuid === b.uuid) || null
if (!aCard || !bCard) return 0
return aCard[key] - bCard[key]
})
} }
const sort = (index : number) => { const sort = (index : number) => {
@ -162,23 +156,6 @@ const sort = (index : number) => {
break break
} }
updateLocalStorage()
isDirty.value = false isDirty.value = false
} }
const calculate = (card : Card) => {
const ppr = +replaceComma(card.price) / +card.roles
const pps = (ppr / +card.sheets) * 100
const ppl = (pps / +card.layers)
return { ppr, pps, ppl }
}
onMounted(() => {
const cardsFromStorage = JSON.parse(localStorage.getItem('cards') ?? '[]').map((card : Card) => ({ ...card, ...calculate(card) }))
cards.value = cardsFromStorage.length !== 0 ? cardsFromStorage : cards.value
const sortFromStorage = +JSON.parse(localStorage.getItem('sort') ?? '0')
sort(sortFromStorage)
filterButtons.value[sortFromStorage]!.active = true
})
</script> </script>

View file

@ -1,11 +1,8 @@
export type Card = { export type Card = {
ppr: number uuid : string
pps: number name : string
ppl: number price : string
uuid: string roles : string
name: string sheets : string
price: string layers : string
roles: string
sheets: string
layers: string
} }