@c-rex/contexts 0.1.3 → 0.1.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c-rex/contexts",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -1,10 +1,10 @@
1
1
  "use client"
2
2
 
3
- import { CONTENT_LANG_KEY, UI_LANG_KEY, UI_LANG_OPTIONS } from '@c-rex/constants'
4
- import { ConfigInterface, LanguageAndCountries } from '@c-rex/interfaces'
5
- import { call, getCookie, manageToken, setCookie } from '@c-rex/utils'
3
+ import { CONTENT_LANG_KEY, CREX_TOKEN_HEADER_KEY, UI_LANG_KEY, UI_LANG_OPTIONS } from '@c-rex/constants'
4
+ import { ConfigInterface, LanguageAndCountries, SidebarAvailableVersionsInterface } from '@c-rex/interfaces'
5
+ import { call, getCookie, getFromCookieString, setCookie } from '@c-rex/utils'
6
6
  import { getConfigs } from '@c-rex/utils/next-cookies'
7
- import React, { createContext, useContext, useEffect, useRef, useState } from 'react'
7
+ import React, { createContext, useContext, useEffect, useState } from 'react'
8
8
 
9
9
  type AppConfigContextType = {
10
10
  configs: ConfigInterface
@@ -13,8 +13,10 @@ type AppConfigContextType = {
13
13
  error: Error | null
14
14
  uiLang: string | null
15
15
  contentLang: string | null
16
+ availableVersions: SidebarAvailableVersionsInterface[] | null
16
17
  setUiLang: (lang: string | null) => void
17
18
  setContentLang: (lang: string | null) => void
19
+ setAvailableVersions: (versions: SidebarAvailableVersionsInterface[] | null) => void
18
20
  }
19
21
 
20
22
  const AppConfigContext = createContext<AppConfigContextType | undefined>(undefined)
@@ -22,16 +24,15 @@ const AppConfigContext = createContext<AppConfigContextType | undefined>(undefin
22
24
  export const AppConfigProvider = ({ children }: { children: React.ReactNode }) => {
23
25
  const [configs, setConfigs] = useState<ConfigInterface | null>(null)
24
26
  const [availableLanguagesAndCountries, setAvailableLanguagesAndCountries] = useState<LanguageAndCountries[] | null>(null)
27
+ const [availableVersions, setAvailableVersions] = useState<SidebarAvailableVersionsInterface[] | null>(null)
25
28
  const [loading, setLoading] = useState(true)
26
29
  const [uiLang, setUiLang] = useState<string | null>(null)
27
30
  const [contentLang, setContentLang] = useState<string | null>(null)
28
31
  const [error, setError] = useState<Error | null>(null)
29
- const hasRun = useRef(false)
30
32
 
31
33
  const manageUILanguage = async (configs: ConfigInterface): Promise<void> => {
32
- const hasUILangCookie = await getCookie(UI_LANG_KEY);
33
- let locale = hasUILangCookie.value
34
- if (hasUILangCookie.value === null) {
34
+ let locale = getFromCookieString(document.cookie, UI_LANG_KEY)
35
+ if (locale.length === 0) {
35
36
  const browserLang = navigator.language;
36
37
 
37
38
  locale = UI_LANG_OPTIONS.includes(browserLang.toLowerCase())
@@ -44,9 +45,8 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
44
45
  }
45
46
 
46
47
  const manageContentLanguage = async (configs: ConfigInterface, availableLanguages: LanguageAndCountries[]): Promise<void> => {
47
- const hasContentLangCookie = await getCookie(CONTENT_LANG_KEY);
48
- let locale = hasContentLangCookie.value
49
- if (hasContentLangCookie.value === null) {
48
+ let locale = getFromCookieString(document.cookie, CONTENT_LANG_KEY)
49
+ if (locale.length === 0) {
50
50
  const browserLang = navigator.language;
51
51
  const hasLang = availableLanguages.some((item) => item.value === browserLang);
52
52
 
@@ -60,18 +60,36 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
60
60
  setContentLang(locale)
61
61
  }
62
62
 
63
+ const manageToken = async (): Promise<void> => {
64
+ try {
65
+ const hasToken = await getCookie(CREX_TOKEN_HEADER_KEY);
66
+
67
+ console.log(hasToken)
68
+
69
+ if (hasToken.value === null) {
70
+ await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/token`, {
71
+ method: 'POST',
72
+ credentials: 'include',
73
+ });
74
+ }
75
+ } catch (error) {
76
+ call("CrexLogger.log", {
77
+ level: "error",
78
+ message: `config-provider.manageToken error: ${error}`
79
+ });
80
+ }
81
+ }
82
+
63
83
  async function interval(delayMs: number) {
64
84
  while (true) {
65
- await manageToken()
66
85
  await new Promise(resolve => setTimeout(resolve, delayMs));
86
+ await manageToken()
67
87
  }
68
88
  }
69
89
 
70
90
  useEffect(() => {
71
- if (hasRun.current) return
72
- hasRun.current = true
73
-
74
91
  const load = async () => {
92
+ console.log('load')
75
93
  try {
76
94
  setLoading(true)
77
95
 
@@ -115,6 +133,8 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
115
133
  contentLang,
116
134
  setUiLang,
117
135
  setContentLang,
136
+ availableVersions,
137
+ setAvailableVersions,
118
138
  }}
119
139
  >
120
140
  {loading ? loadingComp : children}