/
vdele
/
app
Обзор
Документация
Войти
/
vdele
/
app
Код
Вики
Пакеты
0
Релизы
1
Аналитика
dev
src/stores/basket.ts
65 строк
2 KB
Vasilii Polshakov
last fixes for 0.0.1-alpha
02 апр 2025, 12:32
02 апр 2025, 12:32
ef3d4ef
Код
Авторство
О чём код?
import { ref, computed, triggerRef } from 'vue'; export function useBasket() { // услуги const state = ref<{ items: {uid: string, caption: string, count:number, amount:number }[] | null}>({ items: null }); // итоговая стоимость const sum = computed(() => state.value.items?.reduce((accumulator, service) => accumulator + (service.amount * service.count), 0)); // добавить позицию const add = (position: {uid: string, caption: string, count: number, amount: number}) => { //console.log('Before:', state.value.items); if(state.value.items){ const idx = state.value.items.findIndex((val) => val.uid === position.uid ); //console.log('Current uid:', position.uid); //console.log(idx, position); if(idx > -1){ // позиция существует, заменяем позицию state.value.items.splice(idx,1, position); //console.log('Replaced:', state.value.items); }else{ state.value.items.push(position); //console.log('Pushed:', state.value.items); } triggerRef(state); } }; // удалить позицию const deleteByUid = (uid: string) => { /** @ts-ignore */ const idx = state.items.findIndex((val) => val.uid === uid ); if(idx > -1) /** @ts-ignore */ if(state.items.length >= 1) /** @ts-ignore */ state.items.splice(idx,1); }; //увеличить на 1 const increaseByOne = (uid: string) => { /** @ts-ignore */ const item = state.items.find((val) => val.uid === uid); if(item) item.count++; }; //уменьшить на 1 const decreaseByOne = (uid: string) => { /** @ts-ignore */ const item = state.items.find((val) => val.uid === uid); if(item && item.count >= 2) item.count--; }; return { state, sum, increaseByOne, decreaseByOne, deleteByUid, add, }; };