Add support for sorting categories

This commit is contained in:
Jonas Lochmann
2020-02-10 01:00:00 +01:00
parent 62d39a8fdd
commit 6fc9597d20
11 changed files with 213 additions and 7 deletions
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
* Copyright (C) 2019 - 2020 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -37,6 +37,17 @@ export async function dispatchCreateCategory ({ action, cache }: {
throw new Error('missing child for new category')
}
const oldMaxSort: number = await cache.database.category.max('sort', {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
childId: action.childId
}
})
// if there are no categories, then this is not a number
const sort = Number.isSafeInteger(oldMaxSort + 1) ? (oldMaxSort + 1) : 0
// no version number needs to be updated
await cache.database.category.create({
familyId: cache.familyId,
@@ -53,7 +64,8 @@ export async function dispatchCreateCategory ({ action, cache }: {
usedTimesVersion: generateVersionId(),
parentCategoryId: '',
blockAllNotifications: false,
timeWarningFlags: 0
timeWarningFlags: 0,
sort
}, { transaction: cache.transaction })
// update the cache
@@ -46,6 +46,7 @@ import {
UpdateCategoryBatteryLimitAction,
UpdateCategoryBlockAllNotificationsAction,
UpdateCategoryBlockedTimesAction,
UpdateCategorySortingAction,
UpdateCategoryTemporarilyBlockedAction,
UpdateCategoryTimeWarningsAction,
UpdateCategoryTitleAction,
@@ -86,6 +87,7 @@ import { dispatchSetUserTimezone } from './setusertimezone'
import { dispatchUpdateCategoryBatteryLimit } from './updatecategorybatterylimit'
import { dispatchUpdateCategoryBlockAllNotifications } from './updatecategoryblockallnotifications'
import { dispatchUpdateCategoryBlockedTimes } from './updatecategoryblockedtimes'
import { dispatchUpdateCategorySorting } from './updatecategorysorting'
import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked'
import { dispatchUpdateCategoryTimeWarnings } from './updatecategorytimewarnings'
import { dispatchUpdateCategoryTitle } from './updatecategorytitle'
@@ -148,6 +150,8 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
await dispatchUpdateCategoryBlockAllNotifications({ action, cache })
} else if (action instanceof UpdateCategoryBlockedTimesAction) {
await dispatchUpdateCategoryBlockedTimes({ action, cache })
} else if (action instanceof UpdateCategorySortingAction) {
await dispatchUpdateCategorySorting({ action, cache })
} else if (action instanceof IncrementCategoryExtraTimeAction) {
await dispatchIncrementCategoryExtraTime({ action, cache })
} else if (action instanceof UpdateCategoryTemporarilyBlockedAction) {
@@ -0,0 +1,45 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateCategorySortingAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateCategorySorting ({ action, cache }: {
action: UpdateCategorySortingAction
cache: Cache
}) {
// no validation here:
// - only parents can do it
// - using it over categories which don't belong together destroys the sorting for both,
// but does not cause any trouble
for (let i = 0; i < action.categoryIds.length; i++) {
const categoryId = action.categoryIds[i]
await cache.database.category.update({
sort: i
}, {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
categoryId
}
})
cache.categoriesWithModifiedBaseData.push(categoryId)
}
}