-
Notifications
You must be signed in to change notification settings - Fork 1
composition-api を用いて todo-app を作ってみる #1
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
Merged
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
dbea36a
add tailwind
r-toki 0e1e9a6
tailwind やめた
r-toki 119182a
ひとまず完成
r-toki 082b283
refactor: review the usage of TS in todos-service.ts
r-toki 22da6c0
refactor: directory structure
r-toki 1e31f21
fix: after PR
r-toki 41d5162
fix: change payload to concrete object
r-toki 675b103
fix: css a bit
r-toki 0124007
fix: after review
r-toki 35820fa
fix: after review. add useTodos.ts & rename handle
r-toki 7ef7476
fix: use-todos.ts to fetch data. & use flex insted of float
r-toki 0c3076f
fix: move states & handlers to container component (= Index.vue)
r-toki 8ff35c2
fix: focus edit-form & trim new-form
r-toki 528ebc2
fix: CreateTodoForm.vue
r-toki e395b8f
fix: a bit
r-toki 94f0bd1
fix: move type statement of handlers to child component to use them.
r-toki f818b8b
fix: watch newTodoTitle in CreateTodoForm.vue
r-toki a1c6afa
fix: change todoIdBeingEdited to TodoList.vue
r-toki cd30489
fix
r-toki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,11 @@ | ||
<template> | ||
<div id="nav"> | ||
<router-link to="/">Home</router-link> | | ||
<router-link to="/about">About</router-link> | ||
</div> | ||
<router-view /> | ||
</template> | ||
|
||
<style> | ||
html, | ||
body, | ||
#app { | ||
font-family: Avenir, Helvetica, Arial, sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
text-align: center; | ||
color: #2c3e50; | ||
} | ||
|
||
#nav { | ||
padding: 30px; | ||
} | ||
|
||
#nav a { | ||
font-weight: bold; | ||
color: #2c3e50; | ||
} | ||
|
||
#nav a.router-link-exact-active { | ||
color: #42b983; | ||
height: 100%; | ||
} | ||
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<template> | ||
<form @submit.prevent="onSubmit"> | ||
<input | ||
ref="input" | ||
type="text" | ||
class="form-control px-5 py-4" | ||
v-model="todoBeingEdited.title" | ||
@blur="$emit('complete-edit-todo')" | ||
/> | ||
</form> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, PropType, inject } from "vue"; | ||
import Todo from "@/models/todo"; | ||
import { TodosStore } from "@/composables/use-todos-store"; | ||
import TodosStoreKey from "@/composables/use-todos-store-key"; | ||
|
||
export default defineComponent({ | ||
name: "EditTodoForm", | ||
|
||
props: { | ||
todo: { | ||
type: Object as PropType<Todo>, | ||
required: true | ||
} | ||
}, | ||
|
||
setup() { | ||
const { updateTodo } = inject(TodosStoreKey) as TodosStore; | ||
return { updateTodo }; | ||
}, | ||
|
||
data(): { todoBeingEdited: Todo } { | ||
return { | ||
todoBeingEdited: { ...this.todo } | ||
}; | ||
}, | ||
|
||
mounted() { | ||
(this.$refs.input as HTMLElement).focus(); | ||
}, | ||
|
||
methods: { | ||
onSubmit() { | ||
this.updateTodo({ | ||
id: this.todo.id, | ||
completed: this.todo.completed, | ||
title: this.todoBeingEdited.title | ||
}); | ||
this.$emit("complete-edit-todo"); | ||
} | ||
} | ||
}); | ||
</script> |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<template> | ||
<form @submit.prevent="onSubmit"> | ||
<input | ||
type="text" | ||
class="form-control px-5 py-4" | ||
placeholder="Add new todo" | ||
v-model.trim="newTodoTitle" | ||
/> | ||
</form> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, inject } from "vue"; | ||
import { TodosStore } from "@/composables/use-todos-store"; | ||
import TodosStoreKey from "@/composables/use-todos-store-key"; | ||
|
||
export default defineComponent({ | ||
name: "NewTodoForm", | ||
|
||
setup() { | ||
const { createTodo } = inject(TodosStoreKey) as TodosStore; | ||
return { createTodo }; | ||
}, | ||
|
||
data(): { newTodoTitle: string } { | ||
return { | ||
newTodoTitle: "" | ||
}; | ||
}, | ||
|
||
methods: { | ||
onSubmit() { | ||
if (!this.newTodoTitle) { | ||
return; | ||
} | ||
this.createTodo({ title: this.newTodoTitle, completed: false }); | ||
this.newTodoTitle = ""; | ||
} | ||
} | ||
}); | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<template> | ||
<li class="list-group-item clearfix"> | ||
<input | ||
type="checkbox" | ||
class="mr-3" | ||
:checked="todo.completed" | ||
@change="onToggleChecked()" | ||
/> | ||
<span>{{ todo.title }}</span> | ||
<span class="float-right"> | ||
<button | ||
class="btn py-0 px-1 mr-1" | ||
@click="$emit('click-edit-todo', todo)" | ||
> | ||
<fa icon="edit" style="width: 15px; height: 15px;" class="my-0"></fa> | ||
</button> | ||
<button class="btn py-0 px-1" @click="removeTodo({ id: todo.id })"> | ||
<fa icon="trash" style="width: 15px; height: 15px;" class="my-0"></fa> | ||
</button> | ||
</span> | ||
</li> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, PropType, inject } from "vue"; | ||
import Todo from "@/models/todo"; | ||
import { TodosStore } from "@/composables/use-todos-store"; | ||
import TodosStoreKey from "@/composables/use-todos-store-key"; | ||
|
||
export default defineComponent({ | ||
name: "TodoItem", | ||
|
||
props: { | ||
todo: { | ||
type: Object as PropType<Todo>, | ||
required: true | ||
} | ||
}, | ||
|
||
setup() { | ||
const { updateTodo, removeTodo } = inject(TodosStoreKey) as TodosStore; | ||
return { updateTodo, removeTodo }; | ||
}, | ||
|
||
methods: { | ||
onToggleChecked() { | ||
this.updateTodo({ | ||
id: this.todo.id, | ||
title: this.todo.title, | ||
completed: !this.todo.completed | ||
}); | ||
} | ||
} | ||
}); | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { InjectionKey } from "vue"; | ||
import { TodosStore } from "@/composables/use-todos-store"; | ||
|
||
const TodosStoreKey: InjectionKey<TodosStore> = Symbol("TodosStore"); | ||
export default TodosStoreKey; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Todo from "@/models/todo"; | ||
import { reactive } from "vue"; | ||
import * as TodosService from "@/storage/todos-service"; | ||
|
||
export default function useTodosStore() { | ||
const state = reactive<{ todos: Todo[] }>({ todos: [] }); | ||
|
||
const fetchTodos = () => { | ||
state.todos = TodosService.getTodos(); | ||
}; | ||
|
||
const createTodo = ({ | ||
title, | ||
completed | ||
}: { | ||
title: string; | ||
completed: boolean; | ||
}) => { | ||
TodosService.postTodo({ title, completed }); | ||
fetchTodos(); | ||
}; | ||
|
||
const updateTodo = ({ | ||
id, | ||
title, | ||
completed | ||
}: { | ||
id: string; | ||
title: string; | ||
completed: boolean; | ||
}) => { | ||
TodosService.putTodo({ id, title, completed }); | ||
fetchTodos(); | ||
}; | ||
|
||
const removeTodo = ({ id }: { id: string }) => { | ||
TodosService.deleteTodo({ id }); | ||
fetchTodos(); | ||
}; | ||
|
||
return { state, fetchTodos, createTodo, updateTodo, removeTodo }; | ||
} | ||
|
||
export type TodosStore = ReturnType<typeof useTodosStore>; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.