123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- // Define a collection to hold our tasks.
- Tasks = new Mongo.Collection('tasks');
- if (Meteor.isClient) {
- // This code is executed on the client only.
- Accounts.ui.config({
- passwordSignupFields: "USERNAME_ONLY"
- });
- Meteor.subscribe("tasks");
- Meteor.startup(function () {
- // Use this branch to render the component after the page is ready.
- ReactDOM.render(<App />, document.getElementById('render-target'));
- });
- }
- if (Meteor.isServer) {
- Meteor.publish("tasks", function () {
- return Tasks.find({
- $or: [
- { private: { $ne: true }},
- { owner: this.userId }
- ]
- });
- });
- }
- Meteor.methods({
- addTask(text) {
- // Make sure user is logged before inserting a task.
- if (!Meteor.userId()) {
- throw new Meteor.error("not-authorized");
- }
- Tasks.insert({
- text: text,
- createdAt: new Date(),
- owner: Meteor.userId(),
- username: Meteor.user().username
- });
- },
- removeTask(taskId) {
- const task = Tasks.find(taskId);
- if (task.private && task.owner !== Meteor.userId) {
- throw new Meteor.Error("not-authorized");
- }
- Tasks.remove(taskId);
- },
- setChecked(taskId, setChecked) {
- const task = Tasks.find(taskId);
- if (task.private && task.owner != Meteor.userId) {
- throw new Meteor.Error("not-authorized");
- }
- Tasks.update(taskId, { $set: { checked: setChecked }});
- },
- setPrivate(taskId, setToPrivate) {
- const task = Tasks.findOne(taskId);
- // Make sure only the task owneer can make a task private.
- if (task.owner !== Meteor.userId()) {
- throw new Meteor.Error("not-authorized");
- }
- Tasks.update(taskId, { $set: { private: setToPrivate }});
- }
- });
|