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.

110 lines
3.1 KiB

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