/**
* ds-project-list.js
* Project management component
* Create, edit, delete, and select projects
*/
import { useProjectStore } from '../../stores/project-store.js';
export default class ProjectList extends HTMLElement {
constructor() {
super();
this.projectStore = useProjectStore();
this.state = {
projects: this.projectStore.getProjects(),
currentProject: this.projectStore.getCurrentProject(),
showEditModal: false,
editingProject: null
};
}
connectedCallback() {
this.render();
this.setupEventListeners();
this.unsubscribe = this.projectStore.subscribe(() => {
this.state.projects = this.projectStore.getProjects();
this.state.currentProject = this.projectStore.getCurrentProject();
this.updateProjectList();
});
}
disconnectedCallback() {
if (this.unsubscribe) this.unsubscribe();
}
render() {
this.innerHTML = `
Projects
${this.renderProjectsList()}
${this.state.editingProject ? 'Edit Project' : 'New Project'}
`;
}
renderProjectsList() {
if (this.state.projects.length === 0) {
return 'No projects yet. Create one to get started!
';
}
return this.state.projects.map(project => `
${project.name}
ID: ${project.id} | Skin: ${project.skinSelected}
`).join('');
}
setupEventListeners() {
// Create button
this.querySelector('#create-project-btn').addEventListener('click', () => {
this.state.editingProject = null;
this.state.showEditModal = true;
this.render();
this.setupEventListeners();
});
// Project selection
this.querySelectorAll('[data-project-id]').forEach(el => {
el.addEventListener('click', (e) => {
if (!e.target.closest('button')) {
const projectId = el.dataset.projectId;
this.projectStore.selectProject(projectId);
}
});
});
// Edit buttons
this.querySelectorAll('.edit-project-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
const projectId = btn.dataset.projectId;
this.state.editingProject = this.projectStore.getProject(projectId);
this.state.showEditModal = true;
this.render();
this.setupEventListeners();
});
});
// Delete buttons
this.querySelectorAll('.delete-project-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
const projectId = btn.dataset.projectId;
const project = this.projectStore.getProject(projectId);
if (confirm(`Delete project "${project.name}"? This cannot be undone.`)) {
this.projectStore.deleteProject(projectId);
}
});
});
// Modal buttons
const modal = this.querySelector('#edit-modal');
if (modal) {
this.querySelector('#modal-cancel-btn').addEventListener('click', () => {
this.state.showEditModal = false;
this.state.editingProject = null;
this.render();
});
this.querySelector('#modal-save-btn').addEventListener('click', () => {
const id = this.querySelector('#modal-project-id').value.trim();
const name = this.querySelector('#modal-project-name').value.trim();
const skin = this.querySelector('#modal-skin-select').value;
if (!id || !name) {
alert('Please fill in all fields');
return;
}
if (this.state.editingProject) {
// Update
this.projectStore.updateProject(this.state.editingProject.id, {
name,
skinSelected: skin
});
} else {
// Create
this.projectStore.createProject({ id, name, skinSelected: skin });
}
this.state.showEditModal = false;
this.state.editingProject = null;
this.render();
this.setupEventListeners();
});
}
}
updateProjectList() {
const container = this.querySelector('#projects-container');
if (container) {
container.innerHTML = this.renderProjectsList();
this.setupEventListeners();
}
}
}
customElements.define('ds-project-list', ProjectList);