Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Basic #23

Open
wants to merge 10 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
130 changes: 130 additions & 0 deletions basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
10 changes: 10 additions & 0 deletions basic/client/js/components/todo-form.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { createAddTodoAction } from "../flux/index.js";
import store from "../store.js";

class TodoForm {
constructor() {
this.button = document.querySelector(".todo-form__submit");
Expand All @@ -7,6 +10,13 @@ class TodoForm {
mount() {
// TODO:
// ここに 作成ボタンが押されたら todo を作成するような処理を追記する
this.button.addEventListener("click", function (event) {
console.log("clicked!");
const name = document.querySelector(".todo-form__input").value;
console.log(`todo name at listener: ${name}`)
event.preventDefault();
store.dispatch(createAddTodoAction(name));
});
}
}

Expand Down
10 changes: 10 additions & 0 deletions basic/client/js/components/todo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { createUpdateTodoAction } from "../flux/index.js";
// import store from "../store.js";
// import文で、index.js:99 Uncaught ReferenceError: Cannot access 'defaultState' before initialization

class Todo {
constructor(parent, { id, name, done }) {
this.parent = parent;
Expand All @@ -9,6 +13,12 @@ class Todo {
if (this.mounted) return;
// TODO: ここにTODOの削除ボタンが押されたときの処理を追記
// TODO: ここにTODOのチェックボックスが押されたときの処理を追記

const checkbox = this.parent.querySelector(".todo-toggle");
checkbox.addEventListener("change", function (event) {
console.log(checkbox);
console.log("changed!");
});
this.mounted = true;
}

Expand Down
39 changes: 38 additions & 1 deletion basic/client/js/flux/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Todo from "../components/todo.js";

/**
* Dispatcher
*/
Expand All @@ -17,7 +19,19 @@ class Dispatcher extends EventTarget {
const FETCH_TODO_ACTION_TYPE = "Fetch todo list from server";
export const createFetchTodoListAction = () => ({
type: FETCH_TODO_ACTION_TYPE,
paylaod: undefined,
payload: undefined,
});

const ADD_TODO_ACTION_TYPE = "Add a todo";
export const createAddTodoAction = (name) => ({
type: ADD_TODO_ACTION_TYPE,
payload: name,
});

const UPDATE_TODO_ACTION_TYPE = "Togle isDone of a todo";
export const createUpdateTodoAction = (todo) => ({
type: UPDATE_TODO_ACTION_TYPE,
payload: todo,
});

const CLEAR_ERROR = "Clear error from state";
Expand Down Expand Up @@ -53,6 +67,29 @@ const reducer = async (prevState, { type, payload }) => {
case CLEAR_ERROR: {
return { ...prevState, error: null };
}
case ADD_TODO_ACTION_TYPE: {
const newTodo = {id: 0, name: payload, done: false};
prevState.todoList.push(newTodo);

const obj = {name: payload, done: false};
const method = "POST";
const body = JSON.stringify(obj);
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
try {
const resp = await fetch(api, {method, headers, body}).then((res)=> res.json()).then(console.log).catch(console.error);;
return { ...prevState, error: null };
} catch (err) {
return { ...prevState, error: err };
}
}
case UPDATE_TODO_ACTION_TYPE: {
const updatedTodo = payload;
console.log(`udpate todo: ${updatedTodo}`);
return { ...prevState, error: err };
}
default: {
throw new Error("unexpected action type: %o", { type, payload });
}
Expand Down