@vixoniccom/aqi 0.0.2-dev.0 → 0.0.2-dev.2
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/.github/workflows/sonarqube.yml +30 -0
- package/CHANGELOG.md +16 -0
- package/build/main.js +1 -1
- package/build.zip +0 -0
- package/package.json +1 -1
- package/src/App.tsx +4 -3
- package/src/components/FormattedText.tsx +7 -7
- package/src/services/index.ts +9 -12
package/build.zip
CHANGED
|
Binary file
|
package/package.json
CHANGED
package/src/App.tsx
CHANGED
|
@@ -15,6 +15,7 @@ export const App: React.FunctionComponent<Props> = ({ data, start }) => {
|
|
|
15
15
|
const backgroundImageState = parameters.backgroundImage ? `url('${downloadsPath}/${parameters.backgroundImage.filename}')` : ''
|
|
16
16
|
const [formattedData, setFormattedData] = useState<AqiData>()
|
|
17
17
|
const cityInput = parameters?.cityInput || "santiago"
|
|
18
|
+
const msj0 = parameters?.msj0 || 'No hay datos para mostrar'
|
|
18
19
|
const updateTime = 600000
|
|
19
20
|
|
|
20
21
|
useEffect(() => {
|
|
@@ -22,10 +23,10 @@ export const App: React.FunctionComponent<Props> = ({ data, start }) => {
|
|
|
22
23
|
|
|
23
24
|
const fetchData = async () => {
|
|
24
25
|
try {
|
|
25
|
-
const data = await getData(cityInput, updateTime)
|
|
26
|
+
const data = await getData(cityInput, updateTime, msj0)
|
|
26
27
|
setFormattedData(data)
|
|
27
28
|
} catch (error) {
|
|
28
|
-
console.error('Error
|
|
29
|
+
console.error('Error fetching data:', error)
|
|
29
30
|
setFormattedData(undefined)
|
|
30
31
|
}
|
|
31
32
|
}
|
|
@@ -77,7 +78,7 @@ export const App: React.FunctionComponent<Props> = ({ data, start }) => {
|
|
|
77
78
|
}}>
|
|
78
79
|
<FontLoader paths={['formatMjs.font']} parameters={parameters} downloadsPath={downloadsPath} />
|
|
79
80
|
<div style={{ display: 'flex', position: 'relative', flex: '1 1 0%', flexDirection: 'column' }}>
|
|
80
|
-
<FormattedText text={
|
|
81
|
+
<FormattedText text={msj0} format={parameters?.formatMjs} />
|
|
81
82
|
</div>
|
|
82
83
|
</div>
|
|
83
84
|
)}
|
|
@@ -15,7 +15,7 @@ interface Props {
|
|
|
15
15
|
format?: any
|
|
16
16
|
lineHeight?: number
|
|
17
17
|
maxChar?: number
|
|
18
|
-
style?:
|
|
18
|
+
style?: React.CSSProperties
|
|
19
19
|
text: string
|
|
20
20
|
unit?: string
|
|
21
21
|
paddingBottom?: string
|
|
@@ -25,15 +25,14 @@ interface Props {
|
|
|
25
25
|
export const FormattedText: React.FunctionComponent<Props> = ({ text, format, maxChar, lineHeight, style, unit, paddingBottom, paddingTop }) => {
|
|
26
26
|
const trimText = (text: string, maxChar?: number) => {
|
|
27
27
|
const isValid = maxChar && maxChar >= 3
|
|
28
|
-
if (isValid &&
|
|
28
|
+
if (isValid && text.length > maxChar) {
|
|
29
29
|
const returnText = text.substring(0, maxChar - 3)
|
|
30
|
-
returnText.substr(-1, 3)
|
|
31
30
|
return `${returnText.trim()}...`
|
|
32
31
|
}
|
|
33
32
|
return text
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
const checkNested = (obj: any, path:
|
|
35
|
+
const checkNested = (obj: any, path: string): any => {
|
|
37
36
|
const arr = path.split('.')
|
|
38
37
|
if (arr.length > 0) {
|
|
39
38
|
if (obj.hasOwnProperty(arr[0])) {
|
|
@@ -51,10 +50,11 @@ export const FormattedText: React.FunctionComponent<Props> = ({ text, format, ma
|
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
const renderText = maxChar ? trimText(text, maxChar) : text
|
|
54
|
-
let containerStyle =
|
|
53
|
+
let containerStyle = {
|
|
55
54
|
display: 'inline-flex',
|
|
56
|
-
justifyContent: getHorizontalAlignment(format.alignment)
|
|
57
|
-
|
|
55
|
+
justifyContent: getHorizontalAlignment(format.alignment),
|
|
56
|
+
...style
|
|
57
|
+
}
|
|
58
58
|
|
|
59
59
|
return (
|
|
60
60
|
<div style={containerStyle}>
|
package/src/services/index.ts
CHANGED
|
@@ -3,7 +3,8 @@ import localforage from 'localforage'
|
|
|
3
3
|
import { AirQuality, AqiData, StorageData } from '../types'
|
|
4
4
|
import { API_RESPONSE_STATUS } from '../utils'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
if (!process.env.AQI_TOKEN) console.warn('Env variable not found. Skipping API call.')
|
|
7
|
+
const TOKEN = process.env.AQI_TOKEN
|
|
7
8
|
|
|
8
9
|
const refetchData = (referenceDate: Date, updateTime: number): boolean => {
|
|
9
10
|
const now = new Date()
|
|
@@ -13,17 +14,13 @@ const refetchData = (referenceDate: Date, updateTime: number): boolean => {
|
|
|
13
14
|
const sameDay = referenceDate.getDate() === now.getDate()
|
|
14
15
|
|
|
15
16
|
if (sameYear && sameMonth && sameDay) {
|
|
16
|
-
const
|
|
17
|
-
return
|
|
17
|
+
const differenceMilliseconds = Math.abs(now.getTime() - referenceDate.getTime())
|
|
18
|
+
return differenceMilliseconds >= updateTime
|
|
18
19
|
}
|
|
19
20
|
return true
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
const requestData = async (cityInput: string): Promise<AirQuality | null> => {
|
|
23
|
-
if (!TOKEN) {
|
|
24
|
-
console.warn('VITE_AQI_TOKEN no está definido en el entorno. Se omitirá la llamada a la API.')
|
|
25
|
-
return null
|
|
26
|
-
}
|
|
27
24
|
const URL = `https://api.waqi.info/feed/${cityInput}/?token=${TOKEN}`
|
|
28
25
|
try {
|
|
29
26
|
const response = await axios.get<AirQuality>(URL)
|
|
@@ -32,13 +29,13 @@ const requestData = async (cityInput: string): Promise<AirQuality | null> => {
|
|
|
32
29
|
}
|
|
33
30
|
return null
|
|
34
31
|
} catch (error) {
|
|
35
|
-
console.error('Error
|
|
32
|
+
console.error('Error fetching AQI data:', error)
|
|
36
33
|
return null
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
|
|
40
|
-
const processData = (response: AirQuality | null, cityInput: string): AqiData => {
|
|
41
|
-
let data: AqiData = { city: cityInput, station:
|
|
37
|
+
const processData = (response: AirQuality | null, cityInput: string, defaultMsg: string): AqiData => {
|
|
38
|
+
let data: AqiData = { city: cityInput, station: defaultMsg }
|
|
42
39
|
if (response?.status === API_RESPONSE_STATUS.OK) {
|
|
43
40
|
data = {
|
|
44
41
|
aqi: response.data.aqi,
|
|
@@ -49,12 +46,12 @@ const processData = (response: AirQuality | null, cityInput: string): AqiData =>
|
|
|
49
46
|
return data
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
export const getData = async (cityInput: string, updateTime: number): Promise<AqiData> => {
|
|
49
|
+
export const getData = async (cityInput: string, updateTime: number, defaultMsg: string): Promise<AqiData> => {
|
|
53
50
|
const storageData: StorageData | null = await localforage.getItem('aqi')
|
|
54
51
|
let data
|
|
55
52
|
if (!storageData?.item || storageData?.item.city !== cityInput || !storageData?.date || refetchData(new Date(storageData.date), updateTime)) {
|
|
56
53
|
data = await requestData(cityInput)
|
|
57
|
-
const processedData = processData(data, cityInput)
|
|
54
|
+
const processedData = processData(data, cityInput, defaultMsg)
|
|
58
55
|
await localforage.setItem('aqi', { item: processedData, date: new Date() })
|
|
59
56
|
return processedData
|
|
60
57
|
} else {
|