Skip to content

Commit

Permalink
Minimal refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen committed Jan 1, 2024
1 parent a4c28d6 commit 34a1287
Show file tree
Hide file tree
Showing 5 changed files with 978 additions and 895 deletions.
24 changes: 13 additions & 11 deletions src/hooks/useWeather.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
import useSWR from 'swr';
import fetcher from '../lib/fetcher';

import * as recommendations from '../recommendations';
import * as weatherIcons from '../icons';
import recommendations from '../recommendations';
import weatherIcons from '../icons.js';

const iconPrefix = `wi wi-`;
const apiKey = import.meta.env.VITE_API_KEY;
const apiUrl = import.meta.env.VITE_API_URL;
const API_KEY = import.meta.env.VITE_API_KEY;
const BASE_URL = import.meta.env.VITE_API_URL;

if (!BASE_URL || !API_KEY) {
throw new Error('API URL or Key is not defined in environment variables');
}

export function useWeather(endpoint, location, units) {
const apiEndpoint = `?q=${location}&units=${units}&APPID=${apiKey}`;
const apiEndpoint = `?q=${location}&units=${units}&APPID=${API_KEY}`;

const { data, error, isLoading } = useSWR(
`${apiUrl}/${endpoint}/${apiEndpoint}`,
`${BASE_URL}/${endpoint}/${apiEndpoint}`,
fetcher,
);

Expand Down Expand Up @@ -58,8 +61,7 @@ function mapResponseProperties(data) {
// Add extra properties for the five day forecast: dt_txt, icon, min, max
if (data.dt_txt) {
mapped.dt_txt = data.dt_txt;
mapped.forecastIcon =
iconPrefix + weatherIcons.default['day'][mapped.icon_id].icon;
mapped.forecastIcon = iconPrefix + weatherIcons['day'][mapped.icon_id].icon;
}

if (mapped.sunset || mapped.sunrise) {
Expand All @@ -81,9 +83,9 @@ function mapResponseProperties(data) {
: false;
mapped.weatherIcon =
iconPrefix +
weatherIcons.default[mapped.isDay ? 'day' : 'night'][mapped.icon_id].icon;
weatherIcons[mapped.isDay ? 'day' : 'night'][mapped.icon_id].icon;
mapped.weatherRecommendation =
recommendations.default[mapped.isDay ? 'day' : 'night'][
recommendations[mapped.isDay ? 'day' : 'night'][
mapped.icon_id
].recommendation;
}
Expand Down
Loading

1 comment on commit 34a1287

@vercel
Copy link

@vercel vercel bot commented on 34a1287 Jan 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.