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.

115 lines
3.1 KiB

4 years ago
  1. <script>
  2. import pluralize from "pluralize";
  3. import ModalFrame from "../components/ModalFrame";
  4. import SubGoalInput from "../components/SubGoalInput";
  5. import modal from "../stores/modal";
  6. import stufflog from "../stores/stufflog";
  7. export let period = {};
  8. let error = null;
  9. let activityId = "";
  10. let pointCount = "1000";
  11. let subGoals = [{name:"",multiplier:"1.0"}];
  12. function addPeriodGoal() {
  13. error = null;
  14. const parsedPointCount = parseInt(pointCount)
  15. if (Number.isNaN(parsedPointCount)) {
  16. error = "Point count must be a number.";
  17. return
  18. }
  19. if (parsedPointCount <= 0 || (parsedPointCount % 1000) !== 0) {
  20. error = "Point must be a positive non-zero multiple of 1000.";
  21. return
  22. }
  23. const parsedSubGoals = [];
  24. for (const subGoal of subGoals) {
  25. if (subGoal.name === "") {
  26. continue;
  27. }
  28. const parsedMultiplier = parseFloat(subGoal.multiplier);
  29. if (Number.isNaN(parsedMultiplier) || parsedMultiplier < 0) {
  30. error = `Sub goal ${subGoal.name} needs a numeric multiplier.`;
  31. return
  32. }
  33. parsedSubGoals.push({
  34. name: subGoal.name,
  35. multiplier: parsedMultiplier,
  36. })
  37. }
  38. const addGoal = {
  39. activityId: activityId,
  40. pointCount: parsedPointCount,
  41. subGoals: parsedSubGoals,
  42. }
  43. stufflog.updatePeriod(period.id, {addGoal}).then(() => {
  44. modal.close();
  45. }).catch(err => {
  46. error = err.message || err;
  47. console.warn(err);
  48. });
  49. }
  50. function updateSubGoalForm(subGoals) {
  51. let last = -1;
  52. for (let i = 0; i < subGoals.length; ++i) {
  53. if (subGoals[i].name != "") {
  54. last = i;
  55. }
  56. }
  57. if (last == subGoals.length - 1) {
  58. return [...subGoals, {name: "", value: "1.0"}];
  59. } else if (last + 2 < subGoals.length && subGoals.length > 1) {
  60. return subGoals.slice(0, last + 2);
  61. } else {
  62. return subGoals;
  63. }
  64. }
  65. $: subGoals = updateSubGoalForm(subGoals);
  66. $: if (activityId === "" && $stufflog.activities.length > 0) {
  67. activityId = $stufflog.activities[0].id;
  68. }
  69. </script>
  70. <ModalFrame title={`Add ${period.name} Goal`} error={error} closable on:close={() => modal.close()}>
  71. <form on:submit|preventDefault={() => addPeriodGoal()}>
  72. <label>Activity</label>
  73. <select bind:value={activityId}>
  74. {#each $stufflog.activities as activity (activity.id)}
  75. <option value={activity.id}>{activity.name}</option>
  76. {/each}
  77. </select>
  78. <label>Points</label>
  79. <input class="nolast" type="string" bind:value={pointCount} />
  80. <label>Subgoals</label>
  81. {#each subGoals as subGoal}
  82. <SubGoalInput bind:name={subGoal.name} bind:value={subGoal.multiplier} />
  83. {/each}
  84. <p>
  85. The amount of points must be in an increment of 1000, which should be
  86. equivalent to about an hour of baseline activity. Subgoal values are
  87. multipliers. 1.05 means that the 5% more points are added. 0.5 means
  88. the points are halved.
  89. </p>
  90. <hr />
  91. <button type="submit">Add Goal</button>
  92. </form>
  93. </ModalFrame>
  94. <style>
  95. p {
  96. margin-top: 1em;
  97. }
  98. </style>