Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Webpack and switched to ts-loader to fix issue #10 #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
},
"devDependencies": {
"awesome-typescript-loader": "3.2.3",
"ts-loader": "^4.4.1",
"typescript": "2.5.3",
"webpack": "2.2.0",
"webpack-dev-server": "2.2.0"
"webpack": "4.12.0",
"webpack-cli": "^3.0.8",
"webpack-command": "^0.2.1",
"webpack-dev-server": "3.1.4"
}
}
41 changes: 31 additions & 10 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
import { renderTodos } from './utils';
import * as fromStore from "./store";

const input = document.querySelector('input') as HTMLInputElement;
const button = document.querySelector('button') as HTMLButtonElement;
const destroy = document.querySelector('.unsubscribe') as HTMLButtonElement;
const todoList = document.querySelector('.todos') as HTMLLIElement;
import { renderTodos } from "./utils";

const input = document.querySelector("input") as HTMLInputElement;
const button = document.querySelector("button") as HTMLButtonElement;
const destroy = document.querySelector(".unsubscribe") as HTMLButtonElement;
const todoList = document.querySelector(".todos") as HTMLLIElement;

const reducers = {
todos: fromStore.reducer
};

const store = new fromStore.Store(reducers);

console.log(store);

button.addEventListener(
'click',
"click",
() => {
if (!input.value.trim()) return;

const payload = { label: input.value, complete: false };

console.log(payload);
store.dispatch({
type: "ADD_TODO",
payload
});

input.value = '';
console.log(store.value);

input.value = "";
},
false
);

todoList.addEventListener('click', function(event) {
store.subscribe(state => {
renderTodos(state.todos.data);
});

todoList.addEventListener("click", function(event) {
const target = event.target as HTMLButtonElement;
if (target.nodeName.toLowerCase() === 'button') {
if (target.nodeName.toLowerCase() === "button") {
console.log(target);
}
});

store.subscribe(state => console.log('STATE:::', state));
2 changes: 2 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './store';
export * from './reducers';
22 changes: 22 additions & 0 deletions src/store/reducers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const initialState = {
loaded: false,
loading: false,
data: [{ label: "Eat pizza", complete: false }]
};

export function reducer(
state = initialState,
action: { type: string; payload: any }
) {
switch (action.type) {
case "ADD_TODO": {
const todo = action.payload;
const data = [...state.data, todo];
return {
...state,
data
};
}
}
return state;
}
37 changes: 37 additions & 0 deletions src/store/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export class Store {
private subscribers: Function[];
private reducers: { [key: string]: Function };
private state: { [key: string]: any };

constructor(reducers = {}, initialState = {}) {
this.subscribers = [];
this.reducers = reducers;
this.state = initialState;
}

get value() {
return this.state;
}

subscribe(fn) {
this.subscribers = [...this.subscribers, fn];
this.notify();
}

dispatch(action) {
this.state = this.reduce(this.state, action);
this.notify();
}

private notify() {
this.subscribers.forEach(fn => fn(this.value));
}

private reduce(state, action) {
const newState = {};
for (const prop in this.reducers) {
newState[prop] = this.reducers[prop](state[prop], action);
}
return newState;
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outDir": "dist",
"noEmitOnError": true,
"strictNullChecks": true,
"sourceMap": false,
"sourceMap": true,
"experimentalDecorators": true
}
}
5 changes: 4 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = {
mode: "development",
entry: './src/app.ts',
devtool: 'inline-source-map',
output: {
filename: 'app.js',
path: __dirname + '/dist/',
Expand All @@ -14,7 +16,8 @@ module.exports = {
rules: [
{
test: /\.ts$/,
use: 'awesome-typescript-loader',
use: 'ts-loader',
exclude: /node_modules/
},
],
},
Expand Down
Loading