Carrello Spesa

This commit is contained in:
Paolo Arena
2020-12-25 03:55:16 +01:00
parent a9c4b09062
commit 9828f4818a
53 changed files with 5538 additions and 72 deletions

View File

@@ -13,7 +13,7 @@ import { costanti } from '@src/store/Modules/costanti'
import { tools } from '@src/store/Modules/tools'
import { toolsext } from '@src/store/Modules/toolsext'
import * as ApiTables from '@src/store/Modules/ApiTables'
import { CalendarStore, GlobalStore, MessageStore, Projects, Todos, UserStore } from '@store'
import { CalendarStore, GlobalStore, MessageStore, Products, Projects, Todos, UserStore } from '@store'
import messages from '../../statics/i18n'
import globalroutines from './../../globalroutines/index'
@@ -45,6 +45,7 @@ const state: IGlobalState = {
menuCollapse: true,
leftDrawerOpen: true,
RightDrawerOpen: false,
rightCartOpen: false,
stateConnection: stateConnDefault,
networkDataReceived: false,
cfgServer: [],
@@ -80,7 +81,9 @@ const state: IGlobalState = {
gallery: [],
mailinglist: [],
mypage: [],
calzoom: []
calzoom: [],
producers: [],
storehouses: []
}
async function getConfig(id) {
@@ -197,6 +200,10 @@ namespace Getters {
return GlobalStore.state.mypage
else if (table === tools.TABCALZOOM)
return GlobalStore.state.calzoom
else if (table === 'producers')
return GlobalStore.state.producers
else if (table === 'storehouses')
return GlobalStore.state.storehouses
else if (table === 'paymenttypes')
return GlobalStore.state.paymenttypes
else if (table === 'bookings')
@@ -1080,6 +1087,14 @@ namespace Actions {
GlobalStore.state.paymenttypes = (res.data.paymenttypes) ? [...res.data.paymenttypes] : []
GlobalStore.state.gallery = (res.data.gallery) ? [...res.data.gallery] : []
GlobalStore.state.calzoom = (res.data.calzoom) ? [...res.data.calzoom] : []
GlobalStore.state.producers = (res.data.producers) ? [...res.data.producers] : []
GlobalStore.state.storehouses = (res.data.storehouses) ? [...res.data.storehouses] : []
// console.log('res.data.cart', res.data.cart)
if (res.data.cart)
Products.state.cart = (res.data.cart) ? {...res.data.cart} : {}
else
Products.state.cart = {}
if (showall) {
GlobalStore.state.newstosent = (res.data.newstosent) ? [...res.data.newstosent] : []

311
src/store/Modules/Products.ts Executable file
View File

@@ -0,0 +1,311 @@
import { ICart, IOrder, IProduct, IProductsState } from 'model'
import { storeBuilder } from './Store/Store'
import Api from '@api'
import { tools } from './tools'
import { lists } from './lists'
import * as ApiTables from './ApiTables'
import { GlobalStore, Todos, UserStore } from '@store'
import globalroutines from './../../globalroutines/index'
import { Mutation } from 'vuex-module-decorators'
import { serv_constants } from '@src/store/Modules/serv_constants'
import { GetterTree } from 'vuex'
import objectId from '@src/js/objectId'
import { costanti } from '@src/store/Modules/costanti'
import { IAction } from '@src/model'
import * as Types from '@src/store/Api/ApiTypes'
import { static_data } from '@src/db/static_data'
const state: IProductsState = {
products: [],
cart: null,
}
// const listFieldsToChange: string [] = ['descr', 'statustodo', 'category', 'expiring_at', 'priority', 'id_prev', 'pos', 'enableExpiring', 'progress', 'phase', 'assigned_to_userId', 'hoursplanned', 'hoursworked', 'start_date', 'completed_at', 'themecolor', 'themebgcolor']
const b = storeBuilder.module<IProductsState>('Products', state)
const stateGetter = b.state()
function getProductsByCategory(category: string): any[] {
return state.products.filter((rec) => rec.category === category)
}
function createOrderByProduct(product: IProduct, order: IOrder): IOrder {
const myorder: IOrder = {
userId: UserStore.state.my._id,
idapp: process.env.APP_ID,
idProduct: product._id,
idProducer: product.idProducer,
status: tools.OrderStatus.IN_CART,
price: product.price,
color: product.color,
size: product.size,
weight: product.weight,
quantity: order.quantity,
idStorehouse: order.idStorehouse
}
if (product.storehouses.length === 1) {
order.idStorehouse = product.storehouses[0]._id
}
return myorder
}
function initcat() {
const rec = Getters.getters.getRecordEmpty()
// rec.userId = UserStore.state.my._id
return rec
}
namespace Getters {
const getProducts = b.read((stateparamf: IProductsState) => (): IProduct[] => {
return state.products
}, 'getProducts')
const getCart = b.read((stateparamf: IProductsState) => (): ICart => {
return state.cart
}, 'getCart')
const existProductInCart = b.read((stateparamf: IProductsState) => (idproduct): boolean => {
const ris = state.cart.items.filter((item) => item.order.idProduct === idproduct).reduce((sum, rec) => sum + 1, 0)
return ris > 0
}, 'existProductInCart')
const getRecordEmpty = b.read((stateparamf: IProductsState) => (): IProduct => {
const tomorrow = tools.getDateNow()
tomorrow.setDate(tomorrow.getDate() + 1)
const objproduct: IProduct = {
// _id: tools.getDateNow().toISOString(), // Create NEW
descr: '',
idProducer: '',
idStorehouses: [],
producer: null,
storehouses: null,
name: '',
department: '',
category: '',
price: 0.0,
color: '',
size: '',
quantityAvailable: 0,
weight: 0,
stars: 0,
date: tools.getDateNow(),
icon: '',
img: ''
}
return objproduct
}, 'getRecordEmpty')
export const getters = {
get getRecordEmpty() {
return getRecordEmpty()
},
get getProducts() {
return getProducts()
},
get getCart() {
return getCart()
},
get existProductInCart() {
return existProductInCart()
},
}
}
namespace Mutations {
export const mutations = {}
}
namespace Actions {
async function loadProducts(context) {
console.log('loadProducts')
if (!static_data.functionality.ENABLE_ECOMMERCE)
return null
console.log('getProducts', 'userid=', UserStore.state.my._id)
// if (UserStore.state.my._id === '') {
// return new Types.AxiosError(0, null, 0, '')
// }
let ris = null
ris = await Api.SendReq('/products', 'POST', null)
.then((res) => {
if (res.data.products) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
state.products = res.data.products
} else {
state.products = []
}
console.log('ARRAY PRODUCTS = ', state.products)
if (process.env.DEBUG === '1') {
console.log('dbLoad', 'state.products', state.products)
}
return res
})
.catch((error) => {
console.log('error getProducts', error)
UserStore.mutations.setErrorCatch(error)
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, error)
})
// ApiTables.aftercalling(ris, checkPending, 'categories')
return ris
}
async function loadCart(context) {
console.log('loadCart')
if (!static_data.functionality.ENABLE_ECOMMERCE)
return null
console.log('loadCart', 'userid=', UserStore.state.my._id)
// if (UserStore.state.my._id === '') {
// return new Types.AxiosError(0, null, 0, '')
// }
let ris = null
ris = await Api.SendReq('/cart/' + UserStore.state.my._id, 'GET', null)
.then((res) => {
if (res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
state.cart = res.data.cart
} else {
state.cart = null
}
return res
})
.catch((error) => {
console.log('error loadCart', error)
UserStore.mutations.setErrorCatch(error)
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, error)
})
// ApiTables.aftercalling(ris, checkPending, 'categories')
return ris
}
async function removeFromCart(context, { order }) {
const ris = await Api.SendReq('/cart/' + UserStore.state.my._id, 'DELETE', { orderId: order._id })
.then((res) => {
if (res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
state.cart = res.data.cart
} else {
state.cart = null
}
return res
})
return ris
}
async function addToCart(context, { product, order }) {
if (!static_data.functionality.ENABLE_ECOMMERCE)
return null
const neworder = createOrderByProduct(product, order)
if (!neworder.idStorehouse)
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, 'Nessuno Store')
console.log('addToCart', 'userid=', UserStore.state.my._id, neworder)
let ris = null
ris = await Api.SendReq('/cart/' + UserStore.state.my._id, 'POST', { order: neworder })
.then((res) => {
if (res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
state.cart = res.data.cart
} else {
state.cart = null
}
return res
})
.catch((error) => {
console.log('error addToCart', error)
UserStore.mutations.setErrorCatch(error)
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, error)
})
// ApiTables.aftercalling(ris, checkPending, 'categories')
return ris
}
async function addSubQtyToItem(context, { addqty, subqty, order }) {
if (!static_data.functionality.ENABLE_ECOMMERCE)
return null
// console.log('addSubQtyToItem', 'userid=', UserStore.state.my._id, order)
let ris = null
ris = await Api.SendReq('/cart/' + UserStore.state.my._id, 'POST', { addqty, subqty, order })
.then((res) => {
state.cart = res.data.cart
if (!!res.data.qty) {
// const ind = state.cart.items.findIndex((rec) => rec.order._id === order._id)
// state.cart.items[ind].order.quantity = res.data.qty
return res.data.qty
}
return 0
})
.catch((error) => {
console.log('error addSubQtyToItem', error)
UserStore.mutations.setErrorCatch(error)
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, error)
})
// ApiTables.aftercalling(ris, checkPending, 'categories')
return ris
}
export const actions = {
// loadCart: b.dispatch(loadCart),
loadProducts: b.dispatch(loadProducts),
addToCart: b.dispatch(addToCart),
addSubQtyToItem: b.dispatch(addSubQtyToItem),
removeFromCart: b.dispatch(removeFromCart),
}
}
// Module
const ProductsModule = {
get state() {
return stateGetter()
},
getters: Getters.getters,
mutations: Mutations.mutations,
actions: Actions.actions
}
export default ProductsModule

View File

@@ -49,7 +49,13 @@ export const DefaultUser: IUserFields = {
downline: [],
calcstat: DefaultCalc,
dashboard: null,
mydownline: null
mydownline: null,
cart: {
userId: '',
items: [],
totalPrice: 0,
totalQty: 0
}
}
export const DefaultProfile: IUserProfile = {

View File

@@ -163,18 +163,53 @@ const colTableWhere = [
AddCol(DeleteRec)
]
const colTableProducts = [
export const colTableProducer = [
AddCol({ name: 'name', label_trans: 'producer.name' }),
AddCol({ name: 'description', label_trans: 'producer.description' }),
AddCol({ name: 'referent', label_trans: 'producer.referent' }),
AddCol({ name: 'region', label_trans: 'producer.region' }),
AddCol({ name: 'city', label_trans: 'producer.city' }),
AddCol({ name: 'img', label_trans: 'producer.img' }),
AddCol({ name: 'website', label_trans: 'producer.website' }),
]
export const colTableStorehouse = [
AddCol({ name: 'name', label_trans: 'store.name' }),
AddCol({ name: 'description', label_trans: 'store.description' }),
AddCol({ name: 'referent', label_trans: 'store.referent' }),
AddCol({ name: 'address', label_trans: 'store.address' }),
AddCol({ name: 'city', label_trans: 'store.city' }),
AddCol({ name: 'region', label_trans: 'store.region' }),
AddCol({ name: 'img', label_trans: 'store.img' }),
AddCol({ name: 'website', label_trans: 'store.website' }),
]
export const colTableProducts = [
AddCol({ name: 'name', label_trans: 'products.name' }),
AddCol({ name: 'description', label_trans: 'products.description' }),
AddCol({ name: 'icon', label_trans: 'products.icon' }),
AddCol({ name: 'img', label_trans: 'products.img' }),
AddCol({ name: 'department', label_trans: 'products.department' }),
AddCol({ name: 'idProducer', label_trans: 'products.idProducer' }),
// AddCol({ name: 'idProducer', label_trans: 'products.idProducer' }),
AddCol({
name: 'idProducer',
label_trans: 'products.producer',
fieldtype: tools.FieldType.select,
jointable: 'producers'
}),
AddCol({
name: 'idStorehouses',
label_trans: 'storehouses.name',
fieldtype: tools.FieldType.multiselect,
jointable: 'storehouses'
}),
AddCol({ name: 'category', label_trans: 'products.category' }),
AddCol({ name: 'price', label_trans: 'products.price', fieldtype: tools.FieldType.number }),
AddCol({ name: 'color', label_trans: 'products.color' }),
AddCol({ name: 'size', label_trans: 'products.size' }),
AddCol({ name: 'quantity', label_trans: 'products.quantity', fieldtype: tools.FieldType.number }),
AddCol({ name: 'quantityAvailable', label_trans: 'products.quantityAvailable', fieldtype: tools.FieldType.number }),
AddCol({ name: 'weight', label_trans: 'products.weight', fieldtype: tools.FieldType.number }),
AddCol({ name: 'stars', label_trans: 'products.stars', fieldtype: tools.FieldType.number }),
AddCol({ name: 'date', label_trans: 'products.date', fieldtype: tools.FieldType.date }),
AddCol(DeleteRec)
]
@@ -857,9 +892,23 @@ export const fieldsTable = {
value: 'products',
label: 'Prodotti',
columns: colTableProducts,
colkey: 'id',
colkey: '_id',
collabel: 'name'
},
{
value: 'producers',
label: 'Produttori',
columns: colTableProducer,
colkey: '_id',
collabel: 'name'
},
{
value: 'storehouses',
label: 'Magazzini',
columns: colTableStorehouse,
colkey: '_id',
collabel: (rec) => rec.name + ' (' + rec.city + ')'
},
{
value: 'wheres',
label: 'Luoghi',

View File

@@ -164,6 +164,16 @@ export const tools = {
PRIORITY_LOW: 0
},
OrderStatus: {
NONE: 0,
IN_CART: 1,
CHECKOUT_CONFIRMED: 2,
PAYED: 3,
DELIVEDED: 4,
RECEIVED: 5,
CANCELED: 10,
},
Status: {
NONE: 0,
OPENED: 1,