@dative-gpi/foundation-shared-services 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export * from "./useLanguageCode";
2
+ export * from "./useTimeZone";
@@ -0,0 +1,44 @@
1
+ import { onMounted, provide, ref, watch } from "vue";
2
+
3
+ import { LANGUAGE_CODE } from "../config/literals";
4
+
5
+ let initialized = false;
6
+
7
+ const languageCode = ref<string | null>("fr-FR");
8
+
9
+ export const useLanguageCode = () => {
10
+ const setLanguageCode = (payload: string) => {
11
+ languageCode.value = payload;
12
+ };
13
+
14
+ if (!initialized) {
15
+ provide(LANGUAGE_CODE, languageCode);
16
+
17
+ onMounted(() => {
18
+ if (languageCode.value) {
19
+ return;
20
+ }
21
+ });
22
+ }
23
+
24
+ initialized = true;
25
+
26
+ const ready = new Promise((resolve) => {
27
+ if (languageCode.value) {
28
+ resolve(languageCode.value);
29
+ }
30
+ else {
31
+ watch(languageCode, () => {
32
+ if (languageCode.value) {
33
+ resolve(languageCode.value);
34
+ }
35
+ });
36
+ }
37
+ });
38
+
39
+ return {
40
+ ready,
41
+ languageCode,
42
+ setLanguageCode
43
+ };
44
+ }
@@ -0,0 +1,109 @@
1
+ import { onMounted, provide, ref, watch } from "vue";
2
+
3
+ import { TimeZoneInfos } from "@dative-gpi/foundation-shared-domain/models";
4
+
5
+ import { TIME_ZONE } from "../config/literals";
6
+
7
+ let initialized = false;
8
+
9
+ const timeZone = ref<TimeZoneInfos | null>({
10
+ id: "UTC",
11
+ offset: "UTC +03:00:00"
12
+ });
13
+
14
+ export const useTimeZone = () => {
15
+ const setTimeZone = (payload: TimeZoneInfos) => {
16
+ timeZone.value = payload;
17
+ };
18
+
19
+ const getUserOffset = (): string => {
20
+ return timeZone?.value?.offset ?? "UTC +00:00:00";
21
+ };
22
+
23
+ const getUserOffsetMillis = (): number => {
24
+ const offset = timeZone?.value?.offset.slice(3) ?? "";
25
+ const matchData = offset.match(/([+-])(\d+)(?::(\d+))?/);
26
+ if (matchData) {
27
+ const [_, sign, hour, minute] = matchData;
28
+ return parseInt(sign + "1") * ((hour ? parseInt(hour) : 0) * 60 + (minute ? parseInt(minute) : 0)) * 60 * 1000;
29
+ }
30
+ return 0;
31
+ };
32
+
33
+ const getMachineOffset = (): string => {
34
+ const timeZoneName = Intl.DateTimeFormat("ia", {
35
+ timeZoneName: "short",
36
+ timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
37
+ }).formatToParts().find((i) => i.type === "timeZoneName")?.value ?? "";
38
+
39
+ const offset = timeZoneName.slice(3);
40
+ if (!offset) {
41
+ return "UTC +00:00:00";
42
+ }
43
+ const matchData = offset.match(/([+-])(\d+)(?::(\d+))?/);
44
+ if (matchData) {
45
+ const [_, sign, hour, minute] = matchData;
46
+ return `UTC ${sign}${hour.padStart(2, "0")}:${(minute ?? "").padStart(2, "0")}:00`;
47
+ }
48
+ return "UTC +00:00:00";
49
+ };
50
+
51
+ const getMachineOffsetMillis = (): number => {
52
+ const timeZoneName = Intl.DateTimeFormat("ia", {
53
+ timeZoneName: "short",
54
+ timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
55
+ }).formatToParts().find((i) => i.type === "timeZoneName")?.value ?? "";
56
+
57
+ const offset = timeZoneName.slice(3);
58
+ if (!offset) {
59
+ return 0;
60
+ }
61
+ const matchData = offset.match(/([+-])(\d+)(?::(\d+))?/);
62
+ if (matchData) {
63
+ const [_, sign, hour, minute] = matchData;
64
+ return parseInt(sign + "1") * ((hour ? parseInt(hour) : 0) * 60 + (minute ? parseInt(minute) : 0)) * 60 * 1000;
65
+ }
66
+ return 0;
67
+ };
68
+
69
+ if (!initialized) {
70
+ provide(TIME_ZONE, timeZone);
71
+
72
+ onMounted(() => {
73
+ if (timeZone.value) {
74
+ return;
75
+ }
76
+ else {
77
+ setTimeZone(new TimeZoneInfos({
78
+ id: Intl.DateTimeFormat().resolvedOptions().timeZone,
79
+ offset: getMachineOffset()
80
+ }));
81
+ }
82
+ });
83
+ }
84
+
85
+ initialized = true;
86
+
87
+ const ready = new Promise((resolve) => {
88
+ if (timeZone.value) {
89
+ resolve(timeZone.value);
90
+ }
91
+ else {
92
+ watch(timeZone, () => {
93
+ if (timeZone.value) {
94
+ resolve(timeZone.value);
95
+ }
96
+ });
97
+ }
98
+ });
99
+
100
+ return {
101
+ ready,
102
+ timeZone,
103
+ setTimeZone,
104
+ getUserOffset,
105
+ getMachineOffset,
106
+ getUserOffsetMillis,
107
+ getMachineOffsetMillis
108
+ };
109
+ }
@@ -0,0 +1 @@
1
+ export * from "./literals";
@@ -0,0 +1,2 @@
1
+ export const TIME_ZONE = "timeZone";
2
+ export const LANGUAGE_CODE = "languageCode";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dative-gpi/foundation-shared-services",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -10,10 +10,10 @@
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
12
  "@dative-gpi/bones-ui": "^0.0.50",
13
- "@dative-gpi/foundation-shared-domain": "0.0.7",
13
+ "@dative-gpi/foundation-shared-domain": "0.0.8",
14
14
  "@microsoft/signalr": "^8.0.0",
15
15
  "vue": "^3.2.0",
16
16
  "vue-router": "^4.2.5"
17
17
  },
18
- "gitHead": "970e56be5ba41712db154bd27ad98962dbeb802e"
18
+ "gitHead": "b3393ad0a66fda249e1b2d3582af86e961553453"
19
19
  }