Task lists WIP

This commit is contained in:
John Livingston
2024-04-30 17:11:10 +02:00
parent ad090eaca4
commit ca026c2e10
10 changed files with 187 additions and 12 deletions

View File

@ -1,5 +1,7 @@
import { Collection } from '@converse/skeletor/src/collection.js'
import { ChatRoomTaskList } from './task-list'
import { initStorage } from '@converse/headless/utils/storage.js'
import { getUniqueId } from '@converse/headless/utils/core.js'
/**
* A list of {@link _converse.ChatRoomTaskList} instances, representing task lists associated to a MUC.
@ -9,6 +11,66 @@ import { ChatRoomTaskList } from './task-list'
*/
class ChatRoomTaskLists extends Collection {
model = ChatRoomTaskList
initialize (models, options) {
super.initialize(arguments)
this.chatroom = options.chatroom
const id = `converse-livechat-tasks-lists-${this.chatroom.get('jid')}`
initStorage(this, id, 'session')
this.on('change:name', () => this.sort())
this.fetchTasksLists().catch(console.error)
}
comparator (tasklist1, tasklist2) {
// Case insensitive on task list name.
const name1 = tasklist1.get('name').toLowerCase()
const name2 = tasklist2.get('name').toLowerCase()
return name1 < name2 ? -1 : name1 > name2 ? 1 : 0
}
create (attrs, options) {
attrs.id ??= getUniqueId()
return super.create(attrs, options)
}
/**
* Requires Task lists from the server.
*/
async fetchTasksLists () {
// TODO: remove these test lines, and subscribe to pubsub.
const taskListsData = [
{
id: 'task-list-1',
name: 'Task List 1'
},
{
// id: 'task-list-2',
name: 'Task List 2'
}
]
for (const item of taskListsData) {
let id = item.id
const tasklist = id ? this.get(id) : undefined
if (tasklist) {
tasklist.save({
name: item.name
})
return
}
id ??= getUniqueId()
this.create({
id,
name: item.name
})
}
}
}
export {