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.

127 lines
4.2 KiB

  1. <script lang="ts">
  2. import stuffLogClient from "../clients/stufflog";
  3. import Checkbox from "../components/Checkbox.svelte";
  4. import ItemSelect from "../components/ItemSelect.svelte";
  5. import Modal from "../components/Modal.svelte";
  6. import type { LogResult } from "../models/log";
  7. import markStale from "../stores/markStale";
  8. import modalStore from "../stores/modal";
  9. import { formatFormTime } from "../utils/time";
  10. export let deletion = false;
  11. export let creation = false;
  12. const md = $modalStore;
  13. let log: LogResult = {
  14. id: "",
  15. taskId: "",
  16. itemId: "",
  17. loggedTime: new Date().toISOString(),
  18. description: "",
  19. itemAmount: 1,
  20. secondaryItemAmount: 0,
  21. secondaryItemId: null,
  22. task: null,
  23. item: null,
  24. secondaryItem: null,
  25. }
  26. let defaultMarkInactive = false;
  27. let verb = "Add";
  28. if (md.name === "log.edit" || md.name === "log.delete") {
  29. log = md.log;
  30. verb = (md.name === "log.edit") ? "Edit" : "Delete";
  31. } else if (md.name === "log.add") {
  32. defaultMarkInactive = md.task.completedAmount >= (md.task.itemAmount - 1);
  33. log.task = md.task;
  34. } else {
  35. throw new Error("Wrong form")
  36. }
  37. let loggedTime = formatFormTime(log.loggedTime);
  38. let description = log.description;
  39. let itemAmount = log.itemAmount;
  40. let secondaryItemId = log.secondaryItemId || "";
  41. let secondaryItemAmount = log.secondaryItemAmount;
  42. let markInactive = defaultMarkInactive;
  43. let error = null;
  44. let loading = false;
  45. function onSubmit() {
  46. loading = true;
  47. error = null;
  48. if (creation) {
  49. stuffLogClient.createLog({
  50. taskId: log.task.id,
  51. loggedTime: new Date(loggedTime).toISOString(),
  52. secondaryItemId: secondaryItemId || null,
  53. description, itemAmount, secondaryItemAmount,
  54. }).then(() => {
  55. markStale("project", "task", "goal", "log");
  56. if (markInactive) {
  57. return stuffLogClient.updateTask(log.task.id, {active: false, statusTag: "completed"});
  58. }
  59. }).then(() => {
  60. markStale("project", "task");
  61. modalStore.close();
  62. }).catch(err => {
  63. error = err.message ? err.message : err.toString();
  64. }).finally(() => {
  65. loading = false;
  66. })
  67. } else if (deletion) {
  68. stuffLogClient.deleteLog(log.id).then(() => {
  69. markStale("project", "task", "goal", "log");
  70. modalStore.close();
  71. }).catch(err => {
  72. error = err.message ? err.message : err.toString();
  73. }).finally(() => {
  74. loading = false;
  75. })
  76. } else {
  77. stuffLogClient.updateLog(log.id, {
  78. loggedTime: new Date(loggedTime).toISOString(),
  79. secondaryItemId: secondaryItemId || null,
  80. clearSecondaryItemId: !secondaryItemId,
  81. description, itemAmount, secondaryItemAmount
  82. }).then(() => {
  83. markStale("project", "task", "goal", "log");
  84. modalStore.close();
  85. }).catch(err => {
  86. error = err.message ? err.message : err.toString();
  87. }).finally(() => {
  88. loading = false;
  89. })
  90. }
  91. }
  92. function onClose() {
  93. modalStore.close();
  94. }
  95. </script>
  96. <Modal show title="{verb} Log" error={error} closable on:close={onClose}>
  97. <form on:submit|preventDefault={onSubmit}>
  98. <label for="taskName">Task</label>
  99. <input disabled name="taskName" type="text" value={log.task.name} />
  100. <label for="loggedTime">Logged Time</label>
  101. <input disabled={deletion} name="loggedTime" type="datetime-local" bind:value={loggedTime} />
  102. <label for="itemAmount">Item Amount</label>
  103. <input disabled={deletion} name="itemAmount" type="number" bind:value={itemAmount} />
  104. <label for="description">Description</label>
  105. <textarea disabled={deletion} name="description" bind:value={description} />
  106. <label for="secondaryItemId">Secondary Item</label>
  107. <ItemSelect name="secondaryItemId" disabled={deletion} optional bind:value={secondaryItemId} />
  108. {#if secondaryItemId != ""}
  109. <label for="secondaryItemAmount">Secondary Item Amount</label>
  110. <input disabled={deletion} name="secondaryItemAmount" type="number" bind:value={secondaryItemAmount} />
  111. {/if}
  112. <Checkbox disabled={!creation} bind:checked={markInactive} label="Mark task inactive/completed." />
  113. <hr />
  114. <button disabled={loading} type="submit">{verb} Log</button>
  115. </form>
  116. </Modal>