2019-07-23 20:44:06 +02:00
|
|
|
import Api from '@api'
|
2019-10-10 16:53:33 +02:00
|
|
|
import { IBookedEvent, ICalendarState, IEvents } from 'model'
|
2019-07-23 20:44:06 +02:00
|
|
|
import { ILinkReg, IResult, IIdToken, IToken } from 'model/other'
|
|
|
|
|
import { storeBuilder } from '../Store'
|
|
|
|
|
|
|
|
|
|
import { serv_constants } from '../../serv_constants'
|
|
|
|
|
import { tools } from '../../tools'
|
|
|
|
|
|
|
|
|
|
import translate from '../../../../globalroutines/util'
|
|
|
|
|
import * as Types from '../../../Api/ApiTypes'
|
2019-10-10 16:53:33 +02:00
|
|
|
import { db_data } from '@src/db/db_data'
|
|
|
|
|
import { UserStore } from '@store'
|
2019-10-12 23:34:58 +02:00
|
|
|
import { lists } from '@src/store/Modules/lists'
|
2019-07-23 20:44:06 +02:00
|
|
|
|
|
|
|
|
// State
|
|
|
|
|
const state: ICalendarState = {
|
2019-10-10 16:53:33 +02:00
|
|
|
editable: false,
|
|
|
|
|
eventlist: [],
|
|
|
|
|
bookedevent: [],
|
|
|
|
|
// ---------------
|
2019-07-23 20:44:06 +02:00
|
|
|
titlebarHeight: 0,
|
|
|
|
|
locale: 'it-IT',
|
|
|
|
|
maxDays: 1,
|
|
|
|
|
fiveDayWorkWeek: false,
|
|
|
|
|
shortMonthLabel: false,
|
|
|
|
|
showDayOfYearLabel: false,
|
2019-09-20 16:36:33 +02:00
|
|
|
shortWeekdayLabel: true,
|
2019-07-23 20:44:06 +02:00
|
|
|
shortIntervalLabel: false,
|
|
|
|
|
hour24Format: true,
|
|
|
|
|
hideHeader: false,
|
|
|
|
|
noScroll: false,
|
2019-09-20 16:36:33 +02:00
|
|
|
showMonthLabel: false,
|
2019-07-23 20:44:06 +02:00
|
|
|
showWorkWeeks: false,
|
2019-10-10 16:53:33 +02:00
|
|
|
intervalRange: { min: 9, max: 23 },
|
2019-07-23 20:44:06 +02:00
|
|
|
intervalRangeStep: 1,
|
|
|
|
|
intervalHeight: 35,
|
|
|
|
|
resourceHeight: 60,
|
|
|
|
|
resourceWidth: 100,
|
|
|
|
|
dayHeight: 100,
|
|
|
|
|
enableThemes: false,
|
|
|
|
|
theme: {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const b = storeBuilder.module<ICalendarState>('CalendarModule', state)
|
|
|
|
|
const stateGetter = b.state()
|
|
|
|
|
|
|
|
|
|
namespace Getters {
|
|
|
|
|
|
2019-10-10 16:53:33 +02:00
|
|
|
const findEventBooked = b.read((mystate: ICalendarState) => (myevent: IEvents, isconfirmed: boolean) => {
|
|
|
|
|
return mystate.bookedevent.find((bookedevent) => (bookedevent.id_bookedevent === myevent._id) && ((isconfirmed && bookedevent.booked) || (!isconfirmed)))
|
|
|
|
|
}, 'findEventBooked')
|
|
|
|
|
|
2019-10-12 23:34:58 +02:00
|
|
|
const getNumParticipants = b.read((mystate: ICalendarState) => (myevent: IEvents) => {
|
|
|
|
|
const myarr = mystate.bookedevent.filter((bookedevent) => (bookedevent.id_bookedevent === myevent._id) && (bookedevent.booked))
|
|
|
|
|
if (myarr)
|
|
|
|
|
return myarr.reduce((sum, bookedevent) => sum + bookedevent.numpeople, 0)
|
|
|
|
|
else
|
|
|
|
|
return 0
|
|
|
|
|
}, 'getNumParticipants')
|
|
|
|
|
|
|
|
|
|
const getEventsBookedByIdEvent = b.read((mystate: ICalendarState) => (idevent) => {
|
|
|
|
|
return mystate.bookedevent.filter((bookedevent) => (bookedevent.id_bookedevent === idevent) && (bookedevent.booked))
|
|
|
|
|
}, 'getEventsBookedByIdEvent')
|
|
|
|
|
|
2019-10-10 16:53:33 +02:00
|
|
|
export const getters = {
|
|
|
|
|
get findEventBooked() {
|
|
|
|
|
return findEventBooked()
|
2019-10-12 23:34:58 +02:00
|
|
|
},
|
|
|
|
|
get getNumParticipants() {
|
|
|
|
|
return getNumParticipants()
|
|
|
|
|
},
|
|
|
|
|
get getEventsBookedByIdEvent() {
|
|
|
|
|
return getEventsBookedByIdEvent()
|
2019-10-10 16:53:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-23 20:44:06 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Mutations {
|
|
|
|
|
// function authUser(state: ICalendarState, data: ICalendarState) {
|
|
|
|
|
// state.userId = data.userId
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// export const mutations = {
|
|
|
|
|
// authUser: b.commit(authUser),
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Actions {
|
2019-10-10 16:53:33 +02:00
|
|
|
async function loadAfterLogin(context) {
|
|
|
|
|
console.log('CalendarStore: loadAfterLogin')
|
|
|
|
|
// Load local data
|
|
|
|
|
state.editable = db_data.userdata.calendar_editable
|
|
|
|
|
state.eventlist = db_data.events
|
|
|
|
|
// state.bookedevent = db_data.userdata.bookedevent
|
|
|
|
|
|
|
|
|
|
if (UserStore.getters.isUserInvalid) {
|
|
|
|
|
state.bookedevent = []
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load local data
|
|
|
|
|
console.log('CALENDAR loadAfterLogin', 'userid=', UserStore.state.userId)
|
|
|
|
|
|
|
|
|
|
let ris = null
|
|
|
|
|
|
2019-10-12 23:34:58 +02:00
|
|
|
const showall = UserStore.state.isAdmin ? '1' : '0'
|
|
|
|
|
|
|
|
|
|
ris = await Api.SendReq('/booking/' + UserStore.state.userId + '/' + process.env.APP_ID + '/' + showall, 'GET', null)
|
2019-10-10 16:53:33 +02:00
|
|
|
.then((res) => {
|
|
|
|
|
if (res.data.bookedevent) {
|
|
|
|
|
state.bookedevent = res.data.bookedevent
|
|
|
|
|
} else {
|
|
|
|
|
state.bookedevent = []
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.log('error dbLoad', error)
|
|
|
|
|
// UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, error)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ris
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getparambyevent(bookevent) {
|
|
|
|
|
return {
|
2019-10-12 23:34:58 +02:00
|
|
|
_id: bookevent._id,
|
2019-10-10 16:53:33 +02:00
|
|
|
id_bookedevent: bookevent.id_bookedevent,
|
|
|
|
|
infoevent: bookevent.infoevent,
|
|
|
|
|
numpeople: bookevent.numpeople,
|
|
|
|
|
msgbooking: bookevent.msgbooking,
|
|
|
|
|
datebooked: bookevent.datebooked,
|
|
|
|
|
userId: UserStore.state.userId,
|
|
|
|
|
booked: bookevent.booked,
|
2019-10-12 23:34:58 +02:00
|
|
|
modified: bookevent.modified
|
2019-10-10 16:53:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function BookEvent(context, bookevent: IBookedEvent) {
|
|
|
|
|
console.log('BookEvent', bookevent)
|
|
|
|
|
|
|
|
|
|
const param = getparambyevent(bookevent)
|
|
|
|
|
|
|
|
|
|
return await Api.SendReq('/booking', 'POST', param)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
|
if (res.data.code === serv_constants.RIS_CODE_OK) {
|
2019-10-12 23:34:58 +02:00
|
|
|
bookevent._id = res.data.id
|
2019-10-10 21:08:29 +02:00
|
|
|
if (bookevent.modified) {
|
|
|
|
|
|
|
|
|
|
const foundIndex = state.bookedevent.findIndex((x) => x.id_bookedevent === bookevent.id_bookedevent)
|
|
|
|
|
if (foundIndex >= 0)
|
|
|
|
|
state.bookedevent[foundIndex] = bookevent
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
state.bookedevent.push(bookevent)
|
|
|
|
|
}
|
2019-10-10 16:53:33 +02:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error(error)
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 23:34:58 +02:00
|
|
|
async function CancelBookingEvent(context, { ideventbook, notify }) {
|
|
|
|
|
console.log('CALSTORE: CancelBookingEvent', ideventbook, notify)
|
2019-10-10 16:53:33 +02:00
|
|
|
|
2019-10-12 23:34:58 +02:00
|
|
|
return await Api.SendReq('/booking/' + ideventbook + '/' + notify + '/' + process.env.APP_ID, 'DELETE', null)
|
2019-10-10 16:53:33 +02:00
|
|
|
.then((res) => {
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
|
if (res.data.code === serv_constants.RIS_CODE_OK) {
|
|
|
|
|
|
2019-10-12 23:34:58 +02:00
|
|
|
// Remove this record from my list
|
|
|
|
|
state.bookedevent = state.bookedevent.filter((eventbooked) => (eventbooked._id !== ideventbook))
|
2019-10-10 16:53:33 +02:00
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error(error)
|
|
|
|
|
// UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
|
loadAfterLogin: b.dispatch(loadAfterLogin),
|
|
|
|
|
BookEvent: b.dispatch(BookEvent),
|
|
|
|
|
CancelBookingEvent: b.dispatch(CancelBookingEvent)
|
|
|
|
|
}
|
2019-07-23 20:44:06 +02:00
|
|
|
|
|
|
|
|
// async function resetpwd(context, paramquery: ICalendarState) {
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// export const actions = {
|
|
|
|
|
// autologin_FromLocalStorage: b.dispatch(autologin_FromLocalStorage)
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Module
|
|
|
|
|
const CalendarModule = {
|
|
|
|
|
get state() {
|
|
|
|
|
return stateGetter()
|
2019-10-10 16:53:33 +02:00
|
|
|
},
|
|
|
|
|
actions: Actions.actions,
|
|
|
|
|
getters: Getters.getters
|
2019-07-23 20:44:06 +02:00
|
|
|
// mutations: Mutations.mutations
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CalendarModule
|