Plan stuff. Log stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <script>
  2. import pluralize from "pluralize";
  3. import capitalize from "capitalize";
  4. import ModalFrame from "../components/ModalFrame";
  5. import SubGoalInput from "../components/SubGoalInput";
  6. import modal from "../stores/modal";
  7. import stufflog from "../stores/stufflog";
  8. import dateStr from "../utils/dateStr";
  9. export let period = {};
  10. let error = null;
  11. let goal = null;
  12. let activity = null;
  13. let subActivity = null;
  14. let dateInput = dateStr(new Date);
  15. let goalId = "";
  16. let subGoalId = "";
  17. let subActivityId = "";
  18. let amount = "200";
  19. let description = "";
  20. let dateInPeriod = false;
  21. function addPeriodLog() {
  22. error = null;
  23. const addLog = {
  24. date: new Date(`${dateInput} 12:00:00.000`),
  25. goalId: goalId,
  26. subGoalId: subGoalId,
  27. subActivityId: subActivityId,
  28. amount: parseInt(amount),
  29. description: description,
  30. }
  31. stufflog.updatePeriod(period.id, {addLog}).then(() => {
  32. modal.close();
  33. }).catch(err => {
  34. error = err.message || err;
  35. console.warn(err);
  36. });
  37. }
  38. $: goal = period.goals.find(g => g.id === goalId) || null;
  39. $: activity = $stufflog.activities.find(a => a.id === goal.activityId) || null;
  40. $: subActivity = activity.subActivities.find(sg => sg.id === subActivityId) || null;
  41. $: if (period.goals.length > 0 && goalId === "") {
  42. goalId = period.goals[0].id;
  43. }
  44. $: if (activity != null && !activity.subActivities.find(s => s.id === subActivityId)) {
  45. if (activity.subActivities.length > 0) {
  46. subActivityId = activity.subActivities[0].id;
  47. } else {
  48. subActivityId = "";
  49. }
  50. }
  51. $: dateInPeriod = Date.parse(dateInput) > Date.parse(period.from) && Date.parse(dateInput) < Date.parse(period.to)
  52. </script>
  53. <ModalFrame title={`Add ${period.name} Log`} error={error} closable on:close={() => modal.close()}>
  54. <form on:submit|preventDefault={() => addPeriodLog()}>
  55. <label>Date</label>
  56. <input type="date" bind:value={dateInput} />
  57. {#if (!dateInPeriod)}
  58. <p class="warning">Date is outside period!</p>
  59. {/if}
  60. <label>Goal</label>
  61. <select bind:value={goalId}>
  62. {#each period.goals as goal (goal.id)}
  63. <option value={goal.id}>{$stufflog.activities.find(a => a.id === goal.activityId).name}</option>
  64. {/each}
  65. </select>
  66. {#if (activity != null)}
  67. <label>Sub-Activity</label>
  68. <select bind:value={subActivityId}>
  69. {#each activity.subActivities as subActivity (subActivity.id)}
  70. <option value={subActivity.id}>{subActivity.name}</option>
  71. {/each}
  72. </select>
  73. {/if}
  74. {#if (subActivity != null)}
  75. <label>{pluralize(capitalize(subActivity.unitName))}</label>
  76. <input type="number" bind:value={amount} />
  77. {/if}
  78. <label>Description</label>
  79. <input type="text" bind:value={description} />
  80. <hr />
  81. <button type="submit">Add Log</button>
  82. </form>
  83. </ModalFrame>
  84. <style>
  85. p.warning {
  86. color: #FB1;
  87. margin: 0;
  88. text-align: center;
  89. }
  90. </style>