useDatepicker.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Module-scoped reactive ref for the panel's "Calendar Type" setting.
  2. // Loaded from /panel/setting/defaultSettings on first use, so any
  3. // component (modals, inbound forms, future pages) can read the same
  4. // value without prop-drilling and without re-fetching.
  5. //
  6. // useInbounds (which already reads defaultSettings for its own state)
  7. // calls setDatepicker() after its fetch so we don't issue a second
  8. // HTTP round-trip on the inbounds page.
  9. import { readonly, ref } from 'vue';
  10. import { HttpUtil } from '@/utils';
  11. const datepicker = ref('gregorian');
  12. let fetched = false;
  13. let pending = null;
  14. async function loadOnce() {
  15. if (fetched) return;
  16. if (pending) {
  17. await pending;
  18. return;
  19. }
  20. pending = (async () => {
  21. try {
  22. const msg = await HttpUtil.post('/panel/setting/defaultSettings');
  23. if (msg?.success) {
  24. datepicker.value = msg.obj?.datepicker || 'gregorian';
  25. }
  26. } finally {
  27. fetched = true;
  28. pending = null;
  29. }
  30. })();
  31. await pending;
  32. }
  33. export function setDatepicker(value) {
  34. fetched = true;
  35. datepicker.value = value || 'gregorian';
  36. }
  37. export function useDatepicker() {
  38. loadOnce();
  39. return { datepicker: readonly(datepicker) };
  40. }