import { Template } from 'meteor/templating';

import { Tasks } from '../api/tasks.js';

import './body.html';

Template.body.helpers({
  tasks() {
    return Tasks.find({});
  },
});

Template.body.events({
  'submit .new-task'(event) {
    event.preventDefault();

    // get form text
    const target = event.target;
    const text = target.text.value;

    // Insert task
    // insecure -- will be fixed later
    Tasks.insert({
      text,
      createdAt: new Date(),
    });

    // clear form text
    target.text.value = '';
  },
});

@@ -164,16 +164,45 @@
 s.find(%7B
+%7D, %7B sort: %7B createdAt: -1 %7D 
 %7D);%0A  %7D,

add sort to tasks; most recent first