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.

106 lines
2.7 KiB

  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 { getScopeListContext } from "$lib/components/contexts/ScopeListContext.svelte";
  9. import type { ScopeInput } from "$lib/models/scope";
  10. import type Scope from "$lib/models/scope";
  11. const {currentModal, closeModal} = getModalContext();
  12. const {updateScope} = getScopeContext();
  13. const {reloadScopeList} = getScopeListContext();
  14. const {page} = getStores();
  15. let scope: ScopeInput
  16. let scopeId: number
  17. let op: string
  18. let error: string
  19. let loading: boolean
  20. let show: boolean
  21. $: switch ($currentModal.name) {
  22. case "scope.create":
  23. initCreate()
  24. break;
  25. case "scope.edit":
  26. initEdit($currentModal.scope)
  27. break;
  28. default:
  29. loading = false;
  30. error = null;
  31. show = false;
  32. }
  33. function initCreate() {
  34. scope = {
  35. name: "",
  36. abbreviation: "",
  37. ownerName: "",
  38. customLabels: {},
  39. }
  40. op = "Create"
  41. show = true;
  42. }
  43. function initEdit(current: Scope) {
  44. scope = {
  45. name: current.name,
  46. abbreviation: current.abbreviation,
  47. ownerName: "",
  48. customLabels: current.customLabels,
  49. };
  50. scopeId = current.id;
  51. op = "Edit"
  52. show = true;
  53. }
  54. async function submit() {
  55. error = null;
  56. loading = true;
  57. try {
  58. switch (op) {
  59. case "Create":
  60. await sl3(fetch, $page.stuff.idToken).createScope(scope);
  61. await reloadScopeList();
  62. break;
  63. case "Edit":
  64. const res = await sl3(fetch, $page.stuff.idToken).updateScope(scopeId, scope);
  65. updateScope(res);
  66. break;
  67. }
  68. closeModal();
  69. } catch(err) {
  70. if (err.statusCode != null) {
  71. error = err.statusMessage;
  72. } else {
  73. error = err
  74. }
  75. } finally {
  76. loading = false;
  77. }
  78. }
  79. </script>
  80. <form on:submit|preventDefault={submit}>
  81. <Modal closable show={show} verb={op} noun="scope" disabled={loading} error={error}>
  82. <ModalBody>
  83. <label for="name">Name</label>
  84. <input name="name" type="text" bind:value={scope.name} />
  85. <label for="abbreviation">Abbreviation</label>
  86. <input name="abbreviation" type="text" bind:value={scope.abbreviation} />
  87. {#if op === "Create"}
  88. <label for="ownerName">Owner Name</label>
  89. <input name="ownerName" type="text" bind:value={scope.ownerName} />
  90. {/if}
  91. </ModalBody>
  92. </Modal>
  93. </form>