Skip to content

Commit

Permalink
Update fetchTimeRanges store usages
Browse files Browse the repository at this point in the history
Signed-off-by: Grigory V <[email protected]>
  • Loading branch information
GVodyanov committed Jan 28, 2024
1 parent 20eeed1 commit 8b2853e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 42 deletions.
13 changes: 7 additions & 6 deletions src/fullcalendar/eventSources/eventSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import getTimezoneManager from '../../services/timezoneDataProviderService.js'
import { getUnixTimestampFromDate } from '../../utils/date.js'
import { eventSourceFunction } from './eventSourceFunction.js'
import logger from '../../utils/logger.js'
import useFetchedTimeRangesStore from '../../store/fetchedTimeRanges.js'

/**
* Returns a function to generate a FullCalendar event-source based on the Vuex calendar model
*
* @param {object} store The Vuex store
* @return {function(*=): {backgroundColor: *, borderColor: *, className: *, id: *, textColor: *, events: events}}
*/
export default function(store) {
export default function() {
return function(calendar) {
const source = {
id: calendar.id,
Expand All @@ -50,10 +50,11 @@ export default function(store) {
}

// This code assumes that once a time range has been fetched it won't be changed
// outside of the vuex store. Triggering a refetch will just update all known
// outside of the vuex(Pinia) store. Triggering a refetch will just update all known
// calendar objects inside this time range. New events that were added to a cached
// time range externally will not be fetched and have to be added manually.
const timeRange = store.getters.getTimeRangeForCalendarCoveringRange(calendar.id, getUnixTimestampFromDate(start), getUnixTimestampFromDate(end))
const fetchedTimeRangesStore = useFetchedTimeRangesStore()
const timeRange = fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange(calendar.id, getUnixTimestampFromDate(start), getUnixTimestampFromDate(end))
if (!timeRange) {
let timeRangeId
try {
Expand All @@ -67,10 +68,10 @@ export default function(store) {
return
}

const calendarObjects = store.getters.getCalendarObjectsByTimeRangeId(timeRangeId)
const calendarObjects = fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId(timeRangeId)
successCallback(eventSourceFunction(calendarObjects, calendar, start, end, timezoneObject))
} else {
const calendarObjects = store.getters.getCalendarObjectsByTimeRangeId(timeRange.id)
const calendarObjects = fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId(timeRange.id)
successCallback(eventSourceFunction(calendarObjects, calendar, start, end, timezoneObject))
}
},
Expand Down
22 changes: 16 additions & 6 deletions src/store/calendarObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
getTimezoneManager,
DateTimeValue,
} from '@nextcloud/calendar-js'
import useFetchedTimeRangesStore from './fetchedTimeRanges.js'

const state = {
calendarObjects: {},
Expand Down Expand Up @@ -169,6 +170,8 @@ const actions = {
return
}

const fetchedTimeRangesStore = useFetchedTimeRangesStore()

const oldCalendarObjectId = calendarObject.id
const oldCalendarId = calendarObject.calendarId

Expand Down Expand Up @@ -196,7 +199,7 @@ const actions = {
},
calendarObjectId: calendarObject.id,
})
context.commit('addCalendarObjectIdToAllTimeRangesOfCalendar', {
fetchedTimeRangesStore.addCalendarObjectIdToAllTimeRangesOfCalendar({
calendarId: newCalendarId,
calendarObjectId: calendarObject.id,
})
Expand All @@ -207,7 +210,7 @@ const actions = {
},
calendarObjectId: oldCalendarObjectId,
})
context.commit('removeCalendarObjectIdFromAllTimeRangesOfCalendar', {
fetchedTimeRangesStore.removeCalendarObjectIdFromAllTimeRangesOfCalendar({
calendarId: oldCalendarId,
calendarObjectId: oldCalendarObjectId,
})
Expand All @@ -224,11 +227,13 @@ const actions = {
* @return {Promise<void>}
*/
async updateCalendarObject(context, { calendarObject }) {
const fetchedTimeRangesStore = useFetchedTimeRangesStore()

if (calendarObject.existsOnServer) {
calendarObject.dav.data = calendarObject.calendarComponent.toICS()
await calendarObject.dav.update()

context.commit('addCalendarObjectIdToAllTimeRangesOfCalendar', {
fetchedTimeRangesStore.addCalendarObjectIdToAllTimeRangesOfCalendar({
calendarId: calendarObject.calendarId,
calendarObjectId: calendarObject.id,
})
Expand All @@ -251,7 +256,7 @@ const actions = {
},
calendarObjectId: calendarObject.id,
})
context.commit('addCalendarObjectIdToAllTimeRangesOfCalendar', {
fetchedTimeRangesStore.addCalendarObjectIdToAllTimeRangesOfCalendar({
calendarId: calendarObject.calendarId,
calendarObjectId: calendarObject.id,
})
Expand All @@ -271,6 +276,9 @@ const actions = {
async createCalendarObjectFromFork(context, { eventComponent, calendarId }) {
const calendar = context.getters.getCalendarById(calendarId)
const calendarObject = mapCalendarJsToCalendarObject(eventComponent.root, calendar.id)

const fetchedTimeRangesStore = useFetchedTimeRangesStore()

calendarObject.dav = await calendar.dav.createVObject(calendarObject.calendarComponent.toICS())
calendarObject.existsOnServer = true
context.commit('updateCalendarObjectId', { calendarObject })
Expand All @@ -282,7 +290,7 @@ const actions = {
},
calendarObjectId: calendarObject.id,
})
context.commit('addCalendarObjectIdToAllTimeRangesOfCalendar', {
fetchedTimeRangesStore.addCalendarObjectIdToAllTimeRangesOfCalendar({
calendarId: calendar.id,
calendarObjectId: calendarObject.id,
})
Expand All @@ -298,6 +306,7 @@ const actions = {
* @return {Promise<void>}
*/
async deleteCalendarObject(context, { calendarObject }) {
const fetchedTimeRangesStore = useFetchedTimeRangesStore()
// If this calendar-object was not created on the server yet,
// no need to send requests to the server
if (calendarObject.existsOnServer) {
Expand All @@ -311,9 +320,10 @@ const actions = {
},
calendarObjectId: calendarObject.id,
})
context.commit('removeCalendarObjectIdFromAnyTimeRange', {
fetchedTimeRangesStore.removeCalendarObjectIdFromAnyTimeRange({
calendarObjectId: calendarObject.id,
})

context.commit('incrementModificationCount')
},

Expand Down
17 changes: 11 additions & 6 deletions src/store/calendars.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import {
import { showError } from '@nextcloud/dialogs'
import useImportStateStore from './importState.js'
import useImportFilesStore from './importFiles.js'
import useFetchedTimeRangesStore from './fetchedTimeRanges.js'

Check failure on line 54 in src/store/calendars.js

View workflow job for this annotation

GitHub Actions / eslint

'/home/runner/work/calendar/calendar/src/store/fetchedTimeRanges.js' imported multiple times
import fetchedTimeRanges from './fetchedTimeRanges.js'

Check failure on line 55 in src/store/calendars.js

View workflow job for this annotation

GitHub Actions / eslint

'fetchedTimeRanges' is defined but never used

Check failure on line 55 in src/store/calendars.js

View workflow job for this annotation

GitHub Actions / eslint

'/home/runner/work/calendar/calendar/src/store/fetchedTimeRanges.js' imported multiple times

const state = {
calendars: [],
Expand Down Expand Up @@ -788,7 +790,8 @@ const actions = {
commit('removeDeletedCalendar', { calendar })
},

async restoreCalendarObject({ commit, state, getters }, { vobject }) {
async restoreCalendarObject({ commit, state }, { vobject }) {
const fetchedTimeRangesStore = useFetchedTimeRangesStore()
await state.trashBin.restore(vobject.uri)

// Clean up the data locally
Expand All @@ -797,7 +800,7 @@ const actions = {
// Delete cached time range that includes the restored event
const calendarObject = mapCDavObjectToCalendarObject(vobject.dav, undefined)
const component = calendarObject.calendarComponent.getFirstComponent(vobject.objectType)
const timeRange = getters.getTimeRangeForCalendarCoveringRange(
const timeRange = fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange(
vobject.calendar.id,
component.startDate?.unixTime,
component.endDate?.unixTime,
Expand All @@ -807,7 +810,7 @@ const actions = {
calendar: vobject.calendar,
fetchedTimeRangeId: timeRange.id,
})
commit('removeTimeRange', {
fetchedTimeRangesStore.removeTimeRange({
timeRangeId: timeRange.id,
})
}
Expand Down Expand Up @@ -970,13 +973,14 @@ const actions = {
* @return {Promise<void>}
*/
async getEventsFromCalendarInTimeRange(context, { calendar, from, to }) {
const fetchedTimeRangesStore = useFetchedTimeRangesStore()
context.commit('markCalendarAsLoading', { calendar })
const response = await calendar.dav.findByTypeInTimeRange('VEVENT', from, to)
let responseTodo = []
if (context.rootState.settings.showTasks) {
responseTodo = await calendar.dav.findByTypeInTimeRange('VTODO', from, to)
}
context.commit('addTimeRange', {
fetchedTimeRangesStore.addTimeRange({
calendarId: calendar.id,
from: getUnixTimestampFromDate(from),
to: getUnixTimestampFromDate(to),
Expand Down Expand Up @@ -1005,7 +1009,7 @@ const actions = {

context.commit('appendCalendarObjects', { calendarObjects })
context.commit('appendCalendarObjectsToCalendar', { calendar, calendarObjectIds })
context.commit('appendCalendarObjectIdsToTimeFrame', {
fetchedTimeRangesStore.appendCalendarObjectIdsToTimeFrame({
timeRangeId: insertId,
calendarObjectIds,
})
Expand Down Expand Up @@ -1105,6 +1109,7 @@ const actions = {
const calendarId = context.rootState.importFiles.importCalendarRelation[file.id]
const calendar = context.getters.getCalendarById(calendarId)
const importStateStore = useImportStateStore()
const fetchedTimeRangesStore = useFetchedTimeRangesStore()

for (const item of file.parser.getItemIterator()) {
requests.push(limit(async () => {
Expand All @@ -1125,7 +1130,7 @@ const actions = {
calendar,
calendarObjectId: calendarObject.id,
})
context.commit('addCalendarObjectIdToAllTimeRangesOfCalendar', {
fetchedTimeRangesStore.addCalendarObjectIdToAllTimeRangesOfCalendar({
calendarId: calendar.id,
calendarObjectId: calendarObject.id,
})
Expand Down
27 changes: 7 additions & 20 deletions src/store/fetchedTimeRanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Vue from 'vue'
import { defineStore } from 'pinia'
import useCalendarObjectsStore from './calendarObjects'
import useCalendarObjectsStore from './calendarObjects.js'

export default defineStore('fetchedTimeRanges', {
state: () => {
Expand Down Expand Up @@ -62,7 +61,6 @@ export default defineStore('fetchedTimeRanges', {
getAllTimeRangesOlderThan: (state) => (olderThan) =>
state.fetchedTimeRanges.filter(f => (f.lastFetched <= olderThan)),


/**
*
* @param {object} state The Pinia state
Expand Down Expand Up @@ -154,7 +152,7 @@ export default defineStore('fetchedTimeRanges', {
* @param {string} data.calendarObjectId The id of the calendar-object to remove
*/
removeCalendarObjectIdFromTimeRange({ timeRangeId, calendarObjectId }) {
const index = state.fetchedTimeRangesById[timeRangeId]
const index = this.fetchedTimeRangesById[timeRangeId]
.calendarObjectIds
.indexOf(calendarObjectId)
if (index !== -1) {
Expand Down Expand Up @@ -183,17 +181,6 @@ export default defineStore('fetchedTimeRanges', {
}
},

/**
* Updates the last-fetched timestamp of a time-range
*
* @param {object} data The destructuring object
* @param {number} data.timeRangeId The id of the timerange
* @param {number} data.lastFetched Timestamp of last-fetched
*/
updateTimestampOfLastFetched({ timeRangeId, lastFetched }) {
this.fetchedTimeRangesById[timeRangeId].lastFetched = lastFetched
},

/**
* Adds a calendar-object-id to all time-ranges of a given caloendar
*
Expand All @@ -202,19 +189,19 @@ export default defineStore('fetchedTimeRanges', {
* @param {string} data.calendarId The id of the calendar
*/
addCalendarObjectIdToAllTimeRangesOfCalendar({ calendarObjectId, calendarId }) {
for (const timerange of this.fetchedTimeRanges) {
if (timerange.calendarId !== calendarId) {
for (const timeRange of this.fetchedTimeRanges) {
if (timeRange.calendarId !== calendarId) {
continue
}

if (timerange.calendarObjectIds.indexOf(calendarObjectId) === -1) {
timerange.calendarObjectIds.push(calendarObjectId)
if (timeRange.calendarObjectIds.indexOf(calendarObjectId) === -1) {
timeRange.calendarObjectIds.push(calendarObjectId)
}
}
},

/**
* Removes a calendar-object-id to all time-ranges of a given caloendar
* Removes a calendar-object-id to all time-ranges of a given calendar
*
* @param {object} data The destructuring object
* @param {string} data.calendarObjectId The id of the calendar-object
Expand Down
6 changes: 4 additions & 2 deletions src/store/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { defineStore } from 'pinia'
import getTimezoneManager from '../services/timezoneDataProviderService.js'
import * as AttachmentService from '../services/attachmentService.js'
import usePrincipalsStore from './principals.js'
import useFetchedTimeRangesStore from './fetchedTimeRanges.js'

export default defineStore('settings', {
state: () => {
Expand Down Expand Up @@ -153,18 +154,19 @@ export default defineStore('settings', {
},

/**
* TODO make this work with eventual calendarObjects.js and fetchedTimeRanges.js
* TODO make this work with eventual calendarObjects.js
* Updates the user's setting for visibility of tasks
*
* @return {Promise<void>}
*/
async toggleTasksEnabled() {
const fetchedTimeRangesStore = useFetchedTimeRangesStore()
const newState = !this.showTasks
const value = newState ? 'yes' : 'no'

await setConfig('showTasks', value)
this.showTasks = !this.showTasks
commit('clearFetchedTimeRanges')
fetchedTimeRangesStore.clearFetchedTimeRanges()
commit('incrementModificationCount')

Check failure on line 170 in src/store/settings.js

View workflow job for this annotation

GitHub Actions / eslint

'commit' is not defined
},

Expand Down
7 changes: 5 additions & 2 deletions src/views/Calendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ import {
import '@nextcloud/dialogs/dist/index.css'
import Trashbin from '../components/AppNavigation/CalendarList/Trashbin.vue'
import AppointmentConfigList from '../components/AppNavigation/AppointmentConfigList.vue'
import useFetchedTimeRangesStore from '../store/fetchedTimeRanges.js'
import { mapStores } from 'pinia'

export default {
name: 'Calendar',
Expand Down Expand Up @@ -132,6 +134,7 @@ export default {
}
},
computed: {
...mapStores(useFetchedTimeRangesStore()),
...mapGetters({
timezoneId: 'getResolvedTimezone',
hasTrashBin: 'hasTrashBin',
Expand Down Expand Up @@ -190,10 +193,10 @@ export default {
created() {
this.timeFrameCacheExpiryJob = setInterval(() => {
const timestamp = (getUnixTimestampFromDate(dateFactory()) - 60 * 10)
const timeRanges = this.$store.getters.getAllTimeRangesOlderThan(timestamp)
const timeRanges = this.fetchedTimeRangesStore.getAllTimeRangesOlderThan(timestamp)

for (const timeRange of timeRanges) {
this.$store.commit('removeTimeRange', {
this.fetchedTimeRangesStore.removeTimeRange({
timeRangeId: timeRange.id,
})
this.$store.commit('deleteFetchedTimeRangeFromCalendar', {
Expand Down

0 comments on commit 8b2853e

Please sign in to comment.