Skip to content

Commit

Permalink
implemented generic classes for state,projectInput and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aubreyzulu committed Jul 14, 2020
1 parent 5a8af36 commit 91ecf83
Showing 1 changed file with 38 additions and 65 deletions.
103 changes: 38 additions & 65 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ class Project {
) {}
}

type Listener = (items: Project[]) => void;
type Listener<T> = (items: T[]) => void;

class State<T> {
protected listeners: Listener<T>[] = [];
addListener(listenerFn: Listener<T>) {
this.listeners.push(listenerFn);
}
}
// Project State Management

class ProjectState {
private listeners: Listener[] = [];
class ProjectState extends State<Project> {
private projects: Project[] = [];
private static instance: ProjectState;

private constructor() {}
private constructor() {
super();
}

static getInstance() {
if (this.instance) {
Expand All @@ -32,10 +40,6 @@ class ProjectState {
return this.instance;
}

addListener(listenerFn: Listener) {
this.listeners.push(listenerFn);
}

addProject(title: string, description: string, numOfPeople: number) {
const newProject = {
status: ProjectStatus.Active,
Expand Down Expand Up @@ -144,41 +148,20 @@ abstract class Component<T extends HTMLElement, U extends HTMLElement> {
this.element
);
}
abstract configure(): void;
abstract renderContent(): void;
}

// ProjectList Class
class ProjectList {
templateElement: HTMLTemplateElement;
hostElement: HTMLDivElement;
element: HTMLElement;
class ProjectList extends Component<HTMLElement, HTMLDivElement> {
assignedProjects: any[];

constructor(private type: 'active' | 'finished') {
this.templateElement = document.getElementById(
'project-list'
)! as HTMLTemplateElement;
this.hostElement = document.getElementById('app')! as HTMLDivElement;
this.assignedProjects = [];

const importedNode = document.importNode(
this.templateElement.content,
true
);
this.element = importedNode.firstElementChild as HTMLElement;
this.element.id = `${this.type}-projects`;
super('project-list', 'app', false, `${type}-projects`);

projectState.addListener((projects: Project[]) => {
const relevantProjects = projects.filter((project) => {
if (this.type === 'active') {
return project.status === ProjectStatus.Active;
}
return project.status === ProjectStatus.Finished;
});
this.assignedProjects = relevantProjects;
this.renderProjects();
});
this.assignedProjects = [];

this.attach();
this.configure();
this.renderContent();
}

Expand All @@ -193,40 +176,34 @@ class ProjectList {
listEl.appendChild(listItem);
}
}

private renderContent() {
configure() {
projectState.addListener((projects: Project[]) => {
const relevantProjects = projects.filter((project) => {
if (this.type === 'active') {
return project.status === ProjectStatus.Active;
}
return project.status === ProjectStatus.Finished;
});
this.assignedProjects = relevantProjects;
this.renderProjects();
});
}
renderContent() {
const listId = `${this.type}-projects-list`;
this.element.querySelector('ul')!.id = listId;
this.element.querySelector('h2')!.textContent =
this.type.toUpperCase() + ' PROJECTS';
}

private attach() {
this.hostElement.insertAdjacentElement('beforeend', this.element);
}
}

// ProjectInput Class
class ProjectInput {
templateElement: HTMLTemplateElement;
hostElement: HTMLDivElement;
element: HTMLFormElement;
class ProjectInput extends Component<HTMLElement, HTMLFormElement> {
titleInputElement: HTMLInputElement;
descriptionInputElement: HTMLInputElement;
peopleInputElement: HTMLInputElement;

constructor() {
this.templateElement = document.getElementById(
'project-input'
)! as HTMLTemplateElement;
this.hostElement = document.getElementById('app')! as HTMLDivElement;

const importedNode = document.importNode(
this.templateElement.content,
true
);
this.element = importedNode.firstElementChild as HTMLFormElement;
this.element.id = 'user-input';
super('project-input', 'app', true, 'user-input');

this.titleInputElement = this.element.querySelector(
'#title'
Expand All @@ -239,9 +216,13 @@ class ProjectInput {
) as HTMLInputElement;

this.configure();
this.attach();
}

configure() {
this.element.addEventListener('submit', this.submitHandler);
}
renderContent() {}

private gatherUserInput(): [string, string, number] | void {
const enteredTitle = this.titleInputElement.value;
const enteredDescription = this.descriptionInputElement.value;
Expand Down Expand Up @@ -291,14 +272,6 @@ class ProjectInput {
this.clearInputs();
}
}

private configure() {
this.element.addEventListener('submit', this.submitHandler);
}

private attach() {
this.hostElement.insertAdjacentElement('afterbegin', this.element);
}
}

const prjInput = new ProjectInput();
Expand Down

0 comments on commit 91ecf83

Please sign in to comment.