Skip to content

Commit

Permalink
Rearrange weather block
Browse files Browse the repository at this point in the history
  • Loading branch information
arcangelini committed May 30, 2024
1 parent 86eedcb commit a3b0c90
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 94 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"npm": "^10.8.0"
},
"dependencies": {
"@tanstack/react-query": "^5.40.0",
"@types/wordpress__block-editor": "^11.5.14",
"@wordpress/block-editor": "^12.26.0",
"@wordpress/components": "^27.6.0",
Expand Down
77 changes: 40 additions & 37 deletions src/weather/edit.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
import { useBlockProps, InspectorControls } from "@wordpress/block-editor";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { PanelBody } from "@wordpress/components";

import { useWeatherData } from "./utils";
import { Waves, Wind, Temperature } from "./icons";
import { Waves, Wind } from "./icons";
import "./editor.scss";
import React, { useEffect } from "react";
import React from "react";

export default function Edit({ attributes, setAttributes }) {
const queryClient = new QueryClient();

function TheEdit({ attributes, setAttributes }) {
const blockProps = useBlockProps();
const { data, isLoading } = useWeatherData();
const { windData, waveData } = useWeatherData();
const { data: windDataData, isLoading: windDataIsLoading } = windData;
const { data: waveDataData, isLoading: waveDataIsLoading } = waveData;

return (
<>
<div {...blockProps}>
<div className="wp-weather-content">
<div className="weather-data heading">
<h2>Current Weather Conditions</h2>
</div>
<div className="weather-data wind">
<Wind />
<div className="wind-speed">
<p>Wind<br />Speed</p>
{isLoading ? (
<p>Loading...</p>
) : (
<p>{data.wind.windspeed} m</p>
)}
</div>
{windDataIsLoading ? (
<h2>Loading...</h2>
) : (
<div className="data">
<p>Speed <span className="meters">{windDataData?.windspeed}</span></p>
<p>Temperature <span className="degrees">{windDataData?.temperature}</span></p>
<p>Direction <span className="degrees">{windDataData?.winddir}</span></p>
</div>
)}
</div>
<div className="weather-data waves">
<div className="weather-data water">
<Waves />
<div className="wave-height">
<p>Wave<br />Height</p>
{isLoading ? (
<p>Loading...</p>
) : (
<p>{data.buoy.hm0} m</p>
)}
</div>
</div>
<div className="weather-data temperature">
<Temperature />
<div className="water-temperature">
<p>Water<br />Temperature</p>
{isLoading ? (
<p>Loading...</p>
) : (
<p>{data.buoy.watertemp} m</p>
)}
</div>
{waveDataIsLoading ? (
<h2>Loading...</h2>
) : (
<div className="data">
<p>Height <span className="meters">{waveDataData?.hm0}</span></p>
<p>Temperature <span className="degrees">{waveDataData?.watertemp}</span></p>
<p>Direction <span className="degrees">{waveDataData?.meandir}</span></p>
</div>
)}
</div>
</div>
</div>
<InspectorControls>
<PanelBody
title={"Block Settings"}
initialOpen={true}
></PanelBody>
<PanelBody title='Block Settings' initialOpen={true}></PanelBody>
</InspectorControls>
</>
);
}

export default function Edit({ attributes, setAttributes }) {
return (
<QueryClientProvider client={queryClient}>
<TheEdit attributes={attributes} setAttributes={setAttributes} />
</QueryClientProvider>
);
}
6 changes: 3 additions & 3 deletions src/weather/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ export const BlockIcon = () => (
);

export const Waves = () => (
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512" width={24} height={24}>
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512" width={45} height={45}>
<Path d="M269.5 69.9c11.1-7.9 25.9-7.9 37 0C329 85.4 356.5 96 384 96c26.9 0 55.4-10.8 77.4-26.1l0 0c11.9-8.5 28.1-7.8 39.2 1.7c14.4 11.9 32.5 21 50.6 25.2c17.2 4 27.9 21.2 23.9 38.4s-21.2 27.9-38.4 23.9c-24.5-5.7-44.9-16.5-58.2-25C449.5 149.7 417 160 384 160c-31.9 0-60.6-9.9-80.4-18.9c-5.8-2.7-11.1-5.3-15.6-7.7c-4.5 2.4-9.7 5.1-15.6 7.7c-19.8 9-48.5 18.9-80.4 18.9c-33 0-65.5-10.3-94.5-25.8c-13.4 8.4-33.7 19.3-58.2 25c-17.2 4-34.4-6.7-38.4-23.9s6.7-34.4 23.9-38.4C42.8 92.6 61 83.5 75.3 71.6c11.1-9.5 27.3-10.1 39.2-1.7l0 0C136.7 85.2 165.1 96 192 96c27.5 0 55-10.6 77.5-26.1zm37 288C329 373.4 356.5 384 384 384c26.9 0 55.4-10.8 77.4-26.1l0 0c11.9-8.5 28.1-7.8 39.2 1.7c14.4 11.9 32.5 21 50.6 25.2c17.2 4 27.9 21.2 23.9 38.4s-21.2 27.9-38.4 23.9c-24.5-5.7-44.9-16.5-58.2-25C449.5 437.7 417 448 384 448c-31.9 0-60.6-9.9-80.4-18.9c-5.8-2.7-11.1-5.3-15.6-7.7c-4.5 2.4-9.7 5.1-15.6 7.7c-19.8 9-48.5 18.9-80.4 18.9c-33 0-65.5-10.3-94.5-25.8c-13.4 8.4-33.7 19.3-58.2 25c-17.2 4-34.4-6.7-38.4-23.9s6.7-34.4 23.9-38.4c18.1-4.2 36.2-13.3 50.6-25.2c11.1-9.4 27.3-10.1 39.2-1.7l0 0C136.7 373.2 165.1 384 192 384c27.5 0 55-10.6 77.5-26.1c11.1-7.9 25.9-7.9 37 0zm0-144C329 229.4 356.5 240 384 240c26.9 0 55.4-10.8 77.4-26.1l0 0c11.9-8.5 28.1-7.8 39.2 1.7c14.4 11.9 32.5 21 50.6 25.2c17.2 4 27.9 21.2 23.9 38.4s-21.2 27.9-38.4 23.9c-24.5-5.7-44.9-16.5-58.2-25C449.5 293.7 417 304 384 304c-31.9 0-60.6-9.9-80.4-18.9c-5.8-2.7-11.1-5.3-15.6-7.7c-4.5 2.4-9.7 5.1-15.6 7.7c-19.8 9-48.5 18.9-80.4 18.9c-33 0-65.5-10.3-94.5-25.8c-13.4 8.4-33.7 19.3-58.2 25c-17.2 4-34.4-6.7-38.4-23.9s6.7-34.4 23.9-38.4c18.1-4.2 36.2-13.3 50.6-25.2c11.1-9.5 27.3-10.1 39.2-1.7l0 0C136.7 229.2 165.1 240 192 240c27.5 0 55-10.6 77.5-26.1c11.1-7.9 25.9-7.9 37 0z" />
</SVG>
);

export const Wind = () => (
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width={24} height={24}>
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width={45} height={45}>
<Path d="M288 32c0 17.7 14.3 32 32 32h32c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32 14.3-32 32s14.3 32 32 32H352c53 0 96-43 96-96s-43-96-96-96H320c-17.7 0-32 14.3-32 32zm64 352c0 17.7 14.3 32 32 32h32c53 0 96-43 96-96s-43-96-96-96H32c-17.7 0-32 14.3-32 32s14.3 32 32 32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H384c-17.7 0-32 14.3-32 32zM128 512h32c53 0 96-43 96-96s-43-96-96-96H32c-17.7 0-32 14.3-32 32s14.3 32 32 32H160c17.7 0 32 14.3 32 32s-14.3 32-32 32H128c-17.7 0-32 14.3-32 32s14.3 32 32 32z" />
</SVG>
);

export const Temperature = () => (
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width={24} height={24}>
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width={45} height={45}>
<Path d="M448 96a32 32 0 1 0 -64 0 32 32 0 1 0 64 0zM320 96a96 96 0 1 1 192 0A96 96 0 1 1 320 96zM144 64c-26.5 0-48 21.5-48 48V276.5c0 17.3-7.1 31.9-15.3 42.5C70.2 332.6 64 349.5 64 368c0 44.2 35.8 80 80 80s80-35.8 80-80c0-18.5-6.2-35.4-16.7-48.9c-8.2-10.6-15.3-25.2-15.3-42.5V112c0-26.5-21.5-48-48-48zM32 112C32 50.2 82.1 0 144 0s112 50.1 112 112V276.5c0 .1 .1 .3 .2 .6c.2 .6 .8 1.6 1.7 2.8c18.9 24.4 30.1 55 30.1 88.1c0 79.5-64.5 144-144 144S0 447.5 0 368c0-33.2 11.2-63.8 30.1-88.1c.9-1.2 1.5-2.2 1.7-2.8c.1-.3 .2-.5 .2-.6V112zM192 368c0 26.5-21.5 48-48 48s-48-21.5-48-48c0-20.9 13.4-38.7 32-45.3V272c0-8.8 7.2-16 16-16s16 7.2 16 16v50.7c18.6 6.6 32 24.4 32 45.3z" />
</SVG>
);
43 changes: 30 additions & 13 deletions src/weather/style.scss
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
.wp-block-spearfishing-stuff-weather {
background-color: #f3f3f3;
margin-top: 20px;
width: 100%;
height: 200px;
margin-top: 20px auto;
max-width: 1440px;

.wp-weather-content {
display: flex;
height: 100%;
gap: 5px;
gap: 20px;

.weather-data {
color: #fff;
background-color: #000;
opacity: 0.7;
background-color: rgb(0 3 55 / 50%);
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
border-radius: 5px;
padding: 20px;

h2.heading {
margin: 5px auto;
font-weight: 100;
}

svg {
fill: #fff;
margin: 0 auto;
margin: 0 50px;
}

div {
display: flex;
flex-direction: column;
width: 75%;
display: grid;
grid-template-columns: 1fr auto;
align-items: center;

p {
margin: 0;
margin: 0 10px 0 0;
justify-self: end;
}

span {
&.meters:after {
content: 'm';
font-size: 10px;
}

&.degrees:after {
content: '°';
position: absolute;
font-size: 10px;
}
}
}
}
Expand Down
85 changes: 44 additions & 41 deletions src/weather/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { useState, useEffect } from "react";
import { useQuery } from "@tanstack/react-query";

// Get the current UTC date in the format YYYYMMDD@HH00
export const getCurrentDate = () => {
const date = new Date();
const year = date.getUTCFullYear();
const month = date.getUTCMonth() + 1;
const day = date.getUTCDate();
const hour = date.getUTCHours();
// Get the current date in the format YYYYMMDD
const getTimestamps = () => {
const now = new Date();
const year = now.getUTCFullYear();
const month = (now.getUTCMonth() + 1).toString().padStart(2, "0");
const day = Number(now.getUTCDate().toString().padStart(2, "0"));

return "20240520@1000";
return `${year}${month.toString().padStart(2, "0")}${day
.toString()
.padStart(2, "0")}@${hour.toString().padStart(2, "0")}00`;
return `${year}${month}${day - 1}@0000&to=${year}${month}${day + 1}@0000`;
};

type BuoyData = {
Expand All @@ -22,11 +18,21 @@ type BuoyData = {
tm02: number;
tp: number;
meandir: number;
windspeed: number;
};

const getBuoyData = (data): BuoyData => {
type WindData = {
temperature: number;
windspeed: number;
windspeedmax: number;
winddir: number;
winddirmax: number;
}

const getLatestData = (data): BuoyData | WindData => {
const keys = data.content[0];
const values = data.content[1][0];
const length = data.content[1].length;
const values = data.content[1][length - 1];

const transformedData = values.reduce((acc, value, index) => {
const theKey = keys[index].toLowerCase().split("(")[0];
Expand All @@ -38,35 +44,32 @@ const getBuoyData = (data): BuoyData => {
return transformedData;
};

export const useWeatherData = () => {
const [data, setData] = useState<any>();
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState();

const fetchData = async () => {
try {
// Get the water data
const buoyResponse = await fetch(
`https://movil.puertos.es/cma2/app/CMA/adhoc/station_data?station=1731&params=WaterTemp,Hm0,Hmax,Tm02,Tp,MeanDir&from=${getCurrentDate()}`,
const getWindData = () => {
return useQuery({
queryKey: ["windData"],
queryFn: async () => {
const response = await fetch(
`https://movil.puertos.es/cma2/app/CMA/adhoc/station_data?station=4753&params=WindSpeed,WindSpeedMax,WindDir,WindDirMax&from=${getTimestamps()}`,
);
const buoyData = await buoyResponse.json();
const data = await response.json();
return getLatestData(data) as WindData;
},
});
};

// Get the wind data
const windResponse = await fetch(
`https://movil.puertos.es/cma2/app/CMA/adhoc/station_data?station=4753&params=WindSpeed,WindSpeedMax,WindDir,WindDirMax&from=${getCurrentDate()}`,
const getWaveData = () => {
return useQuery({
queryKey: ["waveData"],
queryFn: async () => {
const response = await fetch(
`https://movil.puertos.es/cma2/app/CMA/adhoc/station_data?station=1731&params=WaterTemp,Hm0,Hmax,Tm02,Tp,MeanDir&from=${getTimestamps()}`,
);
const windData = await windResponse.json();

setData({ buoy: getBuoyData(buoyData), wind: getBuoyData(windData)});
setIsLoading(false);
} catch (error) {
setError(error);
}
};

useEffect(() => {
fetchData();
}, []);
const data = await response.json();
return getLatestData(data) as BuoyData;
},
});
};

return { data, isLoading, error };
export const useWeatherData = () => {
return { windData: getWindData(), waveData: getWaveData() };
};

0 comments on commit a3b0c90

Please sign in to comment.