simple-todos-react.jsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Define a collection to hold our tasks.
  2. Tasks = new Mongo.Collection('tasks');
  3. if (Meteor.isClient) {
  4. // This code is executed on the client only.
  5. Accounts.ui.config({
  6. passwordSignupFields: "USERNAME_ONLY"
  7. });
  8. Meteor.subscribe("tasks");
  9. Meteor.startup(function () {
  10. // Use this branch to render the component after the page is ready.
  11. ReactDOM.render(<App />, document.getElementById('render-target'));
  12. });
  13. }
  14. if (Meteor.isServer) {
  15. Meteor.publish("tasks", function () {
  16. return Tasks.find({
  17. $or: [
  18. { private: { $ne: true }},
  19. { owner: this.userId }
  20. ]
  21. });
  22. });
  23. }
  24. Meteor.methods({
  25. addTask(text) {
  26. // Make sure user is logged before inserting a task.
  27. if (!Meteor.userId()) {
  28. throw new Meteor.error("not-authorized");
  29. }
  30. Tasks.insert({
  31. text: text,
  32. createdAt: new Date(),
  33. owner: Meteor.userId(),
  34. username: Meteor.user().username
  35. });
  36. },
  37. removeTask(taskId) {
  38. const task = Tasks.find(taskId);
  39. if (task.private && task.owner !== Meteor.userId) {
  40. throw new Meteor.Error("not-authorized");
  41. }
  42. Tasks.remove(taskId);
  43. },
  44. setChecked(taskId, setChecked) {
  45. const task = Tasks.find(taskId);
  46. if (task.private && task.owner != Meteor.userId) {
  47. throw new Meteor.Error("not-authorized");
  48. }
  49. Tasks.update(taskId, { $set: { checked: setChecked }});
  50. },
  51. setPrivate(taskId, setToPrivate) {
  52. const task = Tasks.findOne(taskId);
  53. // Make sure only the task owneer can make a task private.
  54. if (task.owner !== Meteor.userId()) {
  55. throw new Meteor.Error("not-authorized");
  56. }
  57. Tasks.update(taskId, { $set: { private: setToPrivate }});
  58. }
  59. });