Skip to content

Commit

Permalink
Created a type for todos
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosGDCJ committed May 9, 2023
1 parent b73fcd2 commit ec94270
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/components/MoreActions.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import type { TodoType } from '../types/todo.type';
const dispatch = createEventDispatcher();
export let todos: { id: number; name: string; completed: boolean }[];
export let todos: TodoType[];
let completed = true;
Expand Down
6 changes: 2 additions & 4 deletions src/components/Todo.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import { createEventDispatcher, tick } from 'svelte';
import { focusOnInit, selectOnFocus } from '$lib/actions';
export let todo: { id: number; name: string; completed: boolean };
import type { TodoType } from '../types/todo.type';
export let todo: TodoType;
const dispatch = createEventDispatcher();
Expand All @@ -11,9 +12,6 @@
let editButtonPressed = false;
function update(updatedTodo: { id?: number; name?: string; completed?: boolean }) {
console.log('Todo: ', todo);
console.log('updatedTodo: ', updatedTodo);
console.log('Combined: ', { ...todo, ...updatedTodo });
dispatch('update', { todo: { ...todo, ...updatedTodo } });
}
Expand Down
9 changes: 5 additions & 4 deletions src/components/Todos.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import Todo from './Todo.svelte';
import TodosStatus from './TodosStatus.svelte';
import { alert } from '$lib/stores';
import type { TodoType } from '../types/todo.type';
export let todos: { id: number; name: string; completed: boolean }[] = [];
export let todos: TodoType[] = [];
let newTodoId: number;
$: {
Expand All @@ -30,21 +31,21 @@
$alert = 'Browsing completed todos';
}
}
function filterTodos(filter: string, todos: { id: number; name: string; completed: boolean }[]) {
function filterTodos(filter: string, todos: TodoType[]) {
return filter === 'active'
? todos.filter((t) => !t.completed)
: filter === 'completed'
? todos.filter((t) => t.completed)
: todos;
}
function removeTodo(todo: { id: number; name: string; completed: boolean }) {
function removeTodo(todo: TodoType) {
todos = todos.filter((t) => t.id !== todo.id);
$alert = `Todo named '${todo.name}' has been deleted`;
todosStatus.focus();
}
function updateTodo(todo: { id: number; name: string; completed: boolean }) {
function updateTodo(todo: TodoType) {
const i = todos.findIndex((t) => t.id === todo.id);
if (todos[i].name !== todo.name) $alert = `Todo '${todos[i].name}' renamed to '${todo.name}'`;
Expand Down
3 changes: 2 additions & 1 deletion src/components/TodosStatus.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
export let todos: { id: number; name: string; completed: boolean }[];
import type { TodoType } from '../types/todo.type';
export let todos: TodoType[];
export function focus() {
headingEl.focus();
Expand Down
10 changes: 4 additions & 6 deletions src/lib/localstore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { writable } from 'svelte/store';
import { browser } from '$app/environment';
import type { TodoType } from '../types/todo.type';

export function localStore(
key: string,
initial: { id: number; name: string; completed: boolean }[]
) {
function toString(value: { id: number; name: string; completed: boolean }[]) {
export function localStore(key: string, initial: TodoType[]) {
function toString(value: TodoType[]) {
return JSON.stringify(value, null, 2);
}
const toObj = JSON.parse;
Expand All @@ -19,7 +17,7 @@ export function localStore(

return {
subscribe,
set(value: { id: number; name: string; completed: boolean }[]) {
set(value: TodoType[]) {
localStorage.setItem(key, toString(value));
return set(value);
},
Expand Down
5 changes: 5 additions & 0 deletions src/types/todo.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type TodoType = {
id: number;
name: string;
completed: boolean;
};

0 comments on commit ec94270

Please sign in to comment.