Skip to content

Commit

Permalink
feat(weather): add custom weather icon
Browse files Browse the repository at this point in the history
  • Loading branch information
IdoBouskila committed Dec 1, 2024
1 parent 6370864 commit cc20eba
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 5 deletions.
Binary file added client/public/icons/cloudy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/icons/mist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/icons/night.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/icons/rain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/icons/snow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/icons/sunny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/icons/thunder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions client/src/components/current-weather-card.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getHours } from '@utils/utility-functions';
import WeatherDetails from '@components/shared/weather-details';
import WeatherIcon from '@components/shared/weather-icon/weather-icon';
import { PiEyeLight, PiSunLight, PiDropLight, PiWindLight } from 'react-icons/pi';

type Props = {
Expand All @@ -8,6 +9,8 @@ type Props = {
wind: number;
degrees: number;
humidity: number;
isNight: boolean;
iconCode: number;
localTime: string;
visibility: number;
description: string;
Expand All @@ -18,7 +21,9 @@ const CurrentWeatherCard: React.FC<Props> = ({
name,
wind,
degrees,
isNight,
humidity,
iconCode,
localTime,
visibility,
description,
Expand All @@ -33,10 +38,7 @@ const CurrentWeatherCard: React.FC<Props> = ({
</div>

<div className='weather-degrees-container'>
<img
src='/3d-weather-icon.png'
alt='weather-icon'
/>
<WeatherIcon isNight={ isNight } weatherCode={ iconCode } />

<div>
<span className='degrees'>{ degrees }</span>
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const Dashboard: React.FC<{
uv={ current.uv }
wind={ current.wind }
name={ location.name }
isNight={ ! current.is_day }
humidity={ current.humidity }
iconCode={ current.icon_code }
localTime={ location.localtime }
visibility={ current.visibility }
description={ current.description }
Expand Down
69 changes: 69 additions & 0 deletions client/src/components/shared/weather-icon/icon-mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export const icons = {
Mist: 'mist.png',
Rain: 'rain.png',
Snow: 'snow.png',
Sunny: 'sunny.png',
Night: 'night.png',
Cloudy: 'cloudy.png',
Thunder: 'thunder.png',
};

// Sorry for that, I know it's terrible, but `weatherapi.com` API provides ugly icons
// and I don't have enough icons to cover all the weather conditions...
export const weatherIconMapping = {
// Clear/Sunny
1000: icons.Sunny,

// Cloudy/Overcast
1003: icons.Cloudy,
1006: icons.Cloudy,
1009: icons.Cloudy,

// Mist/Fog
1030: icons.Mist,
1135: icons.Mist,
1147: icons.Mist,

// Rain
1063: icons.Rain,
1069: icons.Rain,
1072: icons.Rain,
1150: icons.Rain,
1153: icons.Rain,
1168: icons.Rain,
1171: icons.Rain,
1180: icons.Rain,
1183: icons.Rain,
1186: icons.Rain,
1189: icons.Rain,
1192: icons.Rain,
1195: icons.Rain,
1240: icons.Rain,
1243: icons.Rain,
1246: icons.Rain,

// Snow
1066: icons.Snow,
1067: icons.Snow,
1114: icons.Snow,
1117: icons.Snow,
1204: icons.Snow,
1207: icons.Snow,
1210: icons.Snow,
1213: icons.Snow,
1216: icons.Snow,
1219: icons.Snow,
1222: icons.Snow,
1225: icons.Snow,
1237: icons.Snow,
1255: icons.Snow,
1258: icons.Snow,
1261: icons.Snow,
1264: icons.Snow,

// Thunder
1273: icons.Thunder,
1276: icons.Thunder,
1279: icons.Thunder,
1282: icons.Thunder,
};
30 changes: 30 additions & 0 deletions client/src/components/shared/weather-icon/weather-icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { icons, weatherIconMapping } from './icon-mapping';

type IconCodes = keyof typeof weatherIconMapping;

const WeatherIcon: React.FC<{
isNight?: boolean;
weatherCode: number;
}> = ({ isNight, weatherCode }) => {
const icon = weatherIconMapping[weatherCode as IconCodes] || icons.Sunny;

if(isNight) {
return (
<img
alt='weather icon'
className='weather-icon'
src={ `/icons/${ icons.Night }` }
/>
)
}

return (
<img
alt='weather icon'
className='weather-icon'
src={ `/icons/${ icon }` }
/>
);
}

export default WeatherIcon;
4 changes: 3 additions & 1 deletion client/src/components/weekly-forecast.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import WeatherIcon from '@components/shared/weather-icon/weather-icon';

type Forecast = {
date: string;
icon_code: number;
Expand Down Expand Up @@ -25,7 +27,7 @@ const WeeklyForecast: React.FC<{

return (
<li key={ day.date }>
<img src='/3d-weather-icon.png' alt='weather-icon' />
<WeatherIcon weatherCode={ day.icon_code } />

<span>
{ Math.round(day.max_temp_c) }° / { Math.round(day.min_temp_c) }°
Expand Down
1 change: 1 addition & 0 deletions server/utils/fetchers/fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const fetchForecast = async (query: string) => {
uv: current.uv,
temp_c: current.temp_c,
wind: current.wind_kph,
is_day: current.is_day,
humidity: current.humidity,
visibility: current.vis_km,
icon_code: current.condition.code,
Expand Down

0 comments on commit cc20eba

Please sign in to comment.