1
0
mirror of https://github.com/morris/vanilla-todo.git synced 2025-01-17 20:58:22 +01:00
This commit is contained in:
Morris Brodersen 2024-06-23 17:03:30 +02:00
parent 5ab98c2740
commit 1bc68ef68a
3 changed files with 95 additions and 4 deletions

View File

@ -44,7 +44,10 @@ export function TodoController(el) {
function load() {
try {
if (localStorage?.todo) {
todoData = { ...todoData, ...JSON.parse(localStorage.todo) };
todoData = TodoLogic.movePastTodoItems({
...todoData,
...JSON.parse(localStorage.todo),
});
}
} catch (err) {
// eslint-disable-next-line no-console

View File

@ -8,6 +8,7 @@ import { uuid } from './uuid.js';
* index: number;
* label: string;
* done: boolean;
* fixed: boolean;
* }} TodoDataItem
*/
@ -71,7 +72,7 @@ export class TodoLogic {
* @param {{listId: string, label: string}} input
* @returns {TodoData}
*/
static addTodoItem(data, input) {
static addTodoItem(data, input, now = new Date()) {
let index = 0;
for (const item of data.items) {
@ -89,6 +90,7 @@ export class TodoLogic {
id: uuid(),
index,
done: false,
fixed: this.isListInThePast(input.listId, now),
},
],
};
@ -127,14 +129,20 @@ export class TodoLogic {
* @param {{id: string, listId: string, index: number}} input
* @returns {TodoData}
*/
static moveTodoItem(data, input) {
static moveTodoItem(data, input, now = new Date()) {
const itemToMove = data.items.find((item) => item.id === input.id);
if (!itemToMove) return data;
// Reinsert item at target list and index
let list = data.items.filter(
(item) => item.listId === input.listId && item.id !== input.id,
);
list.splice(input.index, 0, { ...itemToMove, listId: input.listId });
list.splice(input.index, 0, {
...itemToMove,
listId: input.listId,
fixed: this.isListInThePast(input.listId, now),
});
list = TodoLogic.setIndexes(list);
// Reinsert updated list
@ -161,6 +169,44 @@ export class TodoLogic {
};
}
/**
* @param {TodoData} data
* @param {Date} now
* @returns {TodoData}
*/
static movePastTodoItems(data, now = new Date()) {
const todayListId = formatDateId(now);
let targetIndex = 0;
for (const item of data.items) {
if (item.listId === todayListId && item.index > targetIndex) {
targetIndex = item.index;
}
}
return {
...data,
items: data.items.map((item) => {
if (
!item.done &&
!item.fixed &&
this.isListInThePast(item.listId, now)
) {
return { ...item, listId: todayListId, index: targetIndex++ };
}
return item;
}),
};
}
static isListInThePast(listId, now = new Date()) {
const todayListId = formatDateId(now);
return listId.match(/\d\d\d\d-\d\d-\d\d/) && listId < todayListId;
}
//
static getCustomTodoLists(data) {

View File

@ -29,6 +29,7 @@ test('TodoLogic.addTodoItem', () => {
label: 'foo',
index: 0,
done: false,
fixed: true,
},
],
});
@ -42,6 +43,7 @@ test('TodoLogic.addTodoItem', () => {
label: 'foo',
index: 0,
done: false,
fixed: true,
},
{
id: expect.stringMatching(/./),
@ -49,6 +51,7 @@ test('TodoLogic.addTodoItem', () => {
label: 'bar',
index: 1,
done: false,
fixed: true,
},
]);
@ -61,6 +64,7 @@ test('TodoLogic.addTodoItem', () => {
label: 'foo',
index: 0,
done: false,
fixed: true,
},
{
id: expect.stringMatching(/./),
@ -68,6 +72,7 @@ test('TodoLogic.addTodoItem', () => {
label: 'bar',
index: 1,
done: false,
fixed: true,
},
{
id: expect.stringMatching(/./),
@ -75,6 +80,7 @@ test('TodoLogic.addTodoItem', () => {
label: 'baz',
index: 0,
done: false,
fixed: true,
},
]);
});
@ -136,6 +142,42 @@ test('TodoLogic.moveTodoItem', () => {
label: 'foo',
index: 1,
done: false,
fixed: true,
},
]);
data = TodoLogic.moveTodoItem(
data,
{
id: 'a',
listId: '1970-01-02',
index: 0,
},
new Date('1970-01-01'),
);
expect(data.items).toEqual([
{
id: 'b',
listId: '1970-01-01',
label: 'bar',
index: 0,
done: false,
},
{
id: 'a',
listId: '1970-01-02',
label: 'foo',
index: 0,
done: false,
fixed: false,
},
{
id: 'c',
listId: '1970-01-02',
label: 'baz',
index: 1,
done: false,
},
]);
});