Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //import { todos } from './database.js';
- import { todoView } from './views.js';
- import { statics, todosContainer } from './common.js';
- import { deleteTodo, addTodo, toggleTodo } from './events.js';
- //let todos=[] // използва се за кеширани данни
- const populateTodos = (todos, container) => {
- const $div = $(container);
- $div.empty();
- fetch('http://localhost:3000/todos')
- .then((res)=>res.json())
- .then((res)=>{res.forEach((todo) => todoView(todo, container));
- })
- };
- (() => {
- let todos=[]
- // Initial populate
- populateTodos(todos, todosContainer);
- // Attach events here
- deleteTodo((ev) => {
- const $target = $(ev.target);
- const todosList = todos.filter((todo) => todo.id !== +$target.attr('data-id'));
- let $targetid=+$target.attr('data-id');
- fetch(`http://localhost:3000/todos`+'/'+$targetid, {
- method: 'DELETE',
- }).then((res) => {
- populateTodos(todosList, todosContainer);
- // What you should do here?
- }).catch((err) => alert(err.message));
- });
- addTodo((ev) => {
- const $text = $('#todo-text')
- const $date = $('#todo-date')
- if ($($date).val()) {
- statics.id++;
- fetch(`http://localhost:3000/todos`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({ id:statics.id, name:$text.val() ,due: $date.val(),isDone:false, isDeleted:false })
- }).then((res) => {
- populateTodos(todos, todosContainer);
- $text.val('');
- $date.val('');
- res.json()
- }).catch((err) => alert(err.message))
- } else {
- alert('Invalid date');
- }
- });
- toggleTodo((ev) => {
- const $target = $(ev.target);
- const todosList = todos.map((todo) => {
- if (todo.id === +$target.attr('data-check-id')) {
- todo.isDone = !$target.attr('checked');
- }
- return todo;
- });
- populateTodos(todosList, todosContainer);
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement