Factor out creating new user label IDs.
This commit is contained in:
parent
3b7f73b9ac
commit
5dc92d0e1a
|
@ -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,
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue