Loggest thine 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.

117 lines
3.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <script lang="ts">
  2. import { getStores } from "$app/stores";
  3. import { sl3 } from "$lib/clients/sl3";
  4. import Modal from "$lib/components/common/Modal.svelte";
  5. import ModalBody from "$lib/components/common/ModalBody.svelte";
  6. import { getModalContext } from "$lib/components/contexts/ModalContext.svelte";
  7. import { getScopeContext } from "$lib/components/contexts/ScopeContext.svelte";
  8. import Checkbox from "$lib/components/layout/Checkbox.svelte";
  9. import type { Requirement } from "$lib/models/project";
  10. import type Scope from "$lib/models/scope";
  11. import type { StatInput } from "$lib/models/stat";
  12. import type Stat from "$lib/models/stat";
  13. const {currentModal, closeModal} = getModalContext();
  14. const {scope, upsertStat} = getScopeContext();
  15. const {page} = getStores();
  16. let stat: StatInput
  17. let statId: number
  18. let op: string
  19. let error: string
  20. let loading: boolean
  21. let show: boolean
  22. $: switch ($currentModal.name) {
  23. case "stat.create":
  24. initCreate($scope)
  25. break;
  26. case "stat.edit":
  27. initEdit($currentModal.stat)
  28. break;
  29. default:
  30. loading = false;
  31. error = null;
  32. show = false;
  33. }
  34. function initCreate(scope: Scope, requirement?: Requirement) {
  35. let stats = requirement?.stats.map(s => ({statId: s.id, required: 0, acquired: 0}));
  36. if (stats == null) {
  37. stats = scope.stats.map(s => ({statId: s.id, required: 0, acquired: 0}))
  38. }
  39. stat = {
  40. name: "",
  41. weight: 1,
  42. primary: false,
  43. description: "",
  44. allowedAmounts: null,
  45. }
  46. op = "Create"
  47. show = true;
  48. }
  49. function initEdit(current: Stat) {
  50. stat = {
  51. name: current.name,
  52. weight: current.weight,
  53. primary: current.primary,
  54. description: current.description,
  55. allowedAmounts: current.allowedAmounts ? current.allowedAmounts.slice() : void(0),
  56. };
  57. statId = current.id;
  58. op = "Edit"
  59. show = true;
  60. }
  61. async function submit() {
  62. error = null;
  63. loading = true;
  64. try {
  65. switch (op) {
  66. case "Create":
  67. const createdStat = await sl3(fetch, $page.stuff.idToken).createStat($scope.id, stat);
  68. upsertStat(createdStat);
  69. break;
  70. case "Edit":
  71. const editedStat = await sl3(fetch, $page.stuff.idToken).updateStat($scope.id, statId, {
  72. ...stat,
  73. allowedAmounts: void(0),
  74. });
  75. upsertStat(editedStat);
  76. break;
  77. }
  78. closeModal();
  79. } catch(err) {
  80. if (err.statusCode != null) {
  81. error = err.statusMessage;
  82. } else {
  83. error = err
  84. }
  85. } finally {
  86. loading = false;
  87. }
  88. }
  89. </script>
  90. <form on:submit|preventDefault={submit}>
  91. <Modal closable show={show} verb={op} noun="Stat" disabled={loading} error={error}>
  92. <ModalBody>
  93. <label for="name">Name</label>
  94. <input name="name" type="text" bind:value={stat.name} />
  95. <label for="weight">Weight</label>
  96. <input name="weight" type="number" min={0} step={0.001} bind:value={stat.weight} />
  97. <label for="description">Description</label>
  98. <textarea name="description" bind:value={stat.description} />
  99. <Checkbox bind:checked={stat.primary} label="Primary" />
  100. </ModalBody>
  101. </Modal>
  102. </form>