@c-rex/contexts 0.1.11 → 0.1.12
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.
- package/package.json +1 -1
- package/src/config-provider.tsx +10 -10
package/package.json
CHANGED
package/src/config-provider.tsx
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
3
|
import { CREX_TOKEN_HEADER_KEY, UI_LANG_OPTIONS } from '@c-rex/constants'
|
|
4
|
-
import {
|
|
4
|
+
import { CookiesConfigs, LanguageAndCountries, AvailableVersionsInterface } from '@c-rex/interfaces'
|
|
5
5
|
import { call } from '@c-rex/utils'
|
|
6
|
-
import {
|
|
6
|
+
import { getClientConfigs } from '@c-rex/utils/next-cookies'
|
|
7
7
|
import React, { createContext, useContext, useEffect, useState } from 'react'
|
|
8
8
|
import { useLanguageStore } from '@c-rex/components/language-store'
|
|
9
9
|
|
|
10
10
|
type AppConfigContextType = {
|
|
11
11
|
error: Error | null
|
|
12
|
-
configs:
|
|
12
|
+
configs: CookiesConfigs
|
|
13
13
|
loading: boolean
|
|
14
14
|
packageID: string | null
|
|
15
15
|
articleLang: string | null
|
|
@@ -23,14 +23,14 @@ const AppConfigContext = createContext<AppConfigContextType | undefined>(undefin
|
|
|
23
23
|
|
|
24
24
|
export const AppConfigProvider = ({ children }: { children: React.ReactNode }) => {
|
|
25
25
|
const [error, setError] = useState<Error | null>(null)
|
|
26
|
-
const [configs, setConfigs] = useState<
|
|
26
|
+
const [configs, setConfigs] = useState<CookiesConfigs | null>(null)
|
|
27
27
|
const [loading, setLoading] = useState(true)
|
|
28
28
|
const [packageID, setPackageID] = useState<string | null>(null)
|
|
29
29
|
const [articleLang, setArticleLang] = useState<string | null>(null)
|
|
30
30
|
const [availableVersions, setAvailableVersions] = useState<AvailableVersionsInterface[] | null>(null)
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
const manageUILanguage = async (configs:
|
|
33
|
+
const manageUILanguage = async (configs: CookiesConfigs): Promise<void> => {
|
|
34
34
|
const uiLang = useLanguageStore.getState().uiLang;
|
|
35
35
|
const setUiLang = useLanguageStore.getState().setUiLang;
|
|
36
36
|
let locale = uiLang
|
|
@@ -45,7 +45,7 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
|
|
|
45
45
|
setUiLang(locale)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
const manageContentLanguage = async (configs:
|
|
48
|
+
const manageContentLanguage = async (configs: CookiesConfigs): Promise<void> => {
|
|
49
49
|
const contentLang = useLanguageStore.getState().contentLang;
|
|
50
50
|
const setContentLang = useLanguageStore.getState().setContentLang;
|
|
51
51
|
const availableLanguages = useLanguageStore.getState().availableLanguages;
|
|
@@ -63,7 +63,7 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
|
|
|
63
63
|
setContentLang(locale)
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
const manageToken = async (configs:
|
|
66
|
+
const manageToken = async (configs: CookiesConfigs): Promise<void> => {
|
|
67
67
|
try {
|
|
68
68
|
const url = configs.publicNextApiUrl
|
|
69
69
|
const res = await fetch(`${url}/api/cookies?key=${CREX_TOKEN_HEADER_KEY}`);
|
|
@@ -86,7 +86,7 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
const requestToken = async (configs:
|
|
89
|
+
const requestToken = async (configs: CookiesConfigs): Promise<void> => {
|
|
90
90
|
try {
|
|
91
91
|
await fetch(`${configs.publicNextApiUrl}/api/token`, {
|
|
92
92
|
method: 'POST',
|
|
@@ -100,7 +100,7 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
|
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
const interval = async (delayMs: number, configs:
|
|
103
|
+
const interval = async (delayMs: number, configs: CookiesConfigs) => {
|
|
104
104
|
while (true) {
|
|
105
105
|
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
106
106
|
await manageToken(configs);
|
|
@@ -121,7 +121,7 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
|
|
|
121
121
|
try {
|
|
122
122
|
setLoading(true)
|
|
123
123
|
|
|
124
|
-
const configsResult = await
|
|
124
|
+
const configsResult = await getClientConfigs()
|
|
125
125
|
if (!configsResult) {
|
|
126
126
|
throw new Error("Config cookie not available");
|
|
127
127
|
}
|