1
Fork 0

Factor out creating new user label IDs.

This commit is contained in:
Bauke 2023-10-10 17:49:29 +02:00
parent 3b7f73b9ac
commit 5dc92d0e1a
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 14 additions and 11 deletions

View File

@ -3,6 +3,7 @@ import {Component, render} from "preact";
import {
type UserLabelsData,
createValueUserLabel,
newUserLabelId,
saveUserLabels,
} from "../../storage/exports.js";
import {
@ -240,12 +241,7 @@ export class UserLabelsFeature extends Component<Props, State> {
const {userLabels} = this.props;
// If no ID is present then save a new label otherwise edit the existing one.
if (id === undefined) {
let newId = 1;
if (userLabels.length > 0) {
newId =
userLabels.sort((a, b) => b.value.id - a.value.id)[0].value.id + 1;
}
const newId = await newUserLabelId();
userLabels.push(
await createValueUserLabel({
color,

View File

@ -12,6 +12,7 @@ import {
Feature,
createValueUserLabel,
saveUserLabels,
newUserLabelId,
} from "../storage/exports.js";
import "../scss/index.scss";
import "../scss/user-label-editor.scss";
@ -56,11 +57,7 @@ class App extends Component<Props, State> {
username.toLowerCase() === newLabelUsername.toLowerCase(),
);
let id = 1;
if (userLabels.length > 0) {
id = userLabels.sort((a, b) => b.value.id - a.value.id)[0].value.id + 1;
}
const id = await newUserLabelId();
userLabels.push(
await createValueUserLabel({
color: "#ff00ff",

View File

@ -67,3 +67,13 @@ export async function saveUserLabels(
await label.save();
}
}
export async function newUserLabelId(): Promise<number> {
const userLabels = await collectUserLabels();
let newId = 1;
if (userLabels.length > 0) {
newId = userLabels.sort((a, b) => b.value.id - a.value.id)[0].value.id + 1;
}
return newId;
}