Skip to content

Commit

Permalink
add prayer times
Browse files Browse the repository at this point in the history
  • Loading branch information
gamingumar committed Apr 22, 2023
1 parent e779e2f commit 00b5093
Show file tree
Hide file tree
Showing 8 changed files with 35,554 additions and 7,884 deletions.
27,449 changes: 27,449 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "watcher",
"version": "0.2.1",
"version": "0.3.0",
"private": false,
"homepage": "https://gamingumar.com/watcher",
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useEffect, useState } from "react";
import { create } from "apisauce";
import { GpuBlock } from "./Components/GpuBlock";
import { Sunrise } from "./Components/Sunrise";
import { PrayerTimes } from "./Components/PrayerTimes";

const card1 = "http://192.168.1.56:4067/summary";
const card3 = "http://192.168.1.56:4069/summary";
Expand Down Expand Up @@ -111,6 +112,8 @@ function App() {
)}
<Sunrise />

<PrayerTimes />

{/* <GpuBlock gpu={gpu1} title="RTX 3080-OC" />
<GpuBlock gpu={gpu3} title="RTX 3070-OC" />
<GpuBlock gpu={gpu2} title="RTX 3070-EG" /> */}
Expand Down
69 changes: 69 additions & 0 deletions src/Components/PrayerTimes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* File: PrayerTimes.js
* Project: watcher
* File Created: Saturday, 22nd April 2023 7:41:41 pm
* Author: Umar Aamer ([email protected])
* -----
* Last Modified: Saturday, 22nd April 2023 7:41:42 pm
* -----
* Copyright 2020 - 2023 While Geek, https://umar.tech
*/
import React, { useState, useEffect } from "react";
import useLocation from "../hooks/useLocation";

export const PrayerTimes = () => {
const [prayerTimes, setPrayerTimes] = useState({});
const [isLoading, setIsLoading] = useState(true);

const { latitude, longitude } = useLocation();

const today = new Date();

const year = today.getFullYear();

const month = today.getMonth() + 1;

const date = today.getDate();

useEffect(() => {
const fetchPrayerTimes = async () => {
try {
const response = await fetch(
`https://api.aladhan.com/v1/calendar/${year}/${month}?latitude=${latitude}&longitude=${longitude}&method=1`
);

const data = await response.json();

const todayPrayerData = data.data[date - 1].timings;


const formattedData = [];

for (let key in todayPrayerData) {
formattedData.push({
key,
value: todayPrayerData[key],
});
}

setPrayerTimes(formattedData);
setIsLoading(false);
} catch (error) {
console.error(error);
}
};

fetchPrayerTimes();
}, []);

if (isLoading) {
return <div>Loading prayer times...</div>;
}

return (
<div>
<h2>
{prayerTimes[0].key} - {prayerTimes[0].value}</h2>
</div>
);
};
37 changes: 4 additions & 33 deletions src/Components/Sunrise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,32 @@
import { create } from "apisauce";
import moment from "moment";
import React, { useEffect, useState } from "react";
import useLocation from "../hooks/useLocation";

const api = create({
baseURL: "",
headers: { Accept: "application/vnd.github.v3+json" },
});

export const Sunrise = () => {
const [loading, setLoading] = useState(true);

const [data, setData] = useState(null);

const [coords, setCoords] = useState({
lat: "33.6461432",
lng: "73.0523224",
});

const _onSuccess = (position) => {
const lat = position.coords.latitude;
const lng = position.coords.longitude;

setCoords({
lat,
lng,
});

setLoading(false);
};

const _onError = () => {
setLoading(false);
};
const location = useLocation()

// fetch sunrise / sunset
const _fetchData = async () => {
let res = await api.get(
`https://api.sunrise-sunset.org/json?lat=${coords.lat}&lng=${coords.lng}&date=today`
`https://api.sunrise-sunset.org/json?lat=${location.latitude}&lng=${location.longitude}&date=today`
);

if (res.ok) {
setData(res.data.results);
}
};

useEffect(() => {
if (!navigator.geolocation) {
// status.textContent = 'Geolocation is not supported by your browser';
} else {
// status.textContent = 'Locating…';
navigator.geolocation.getCurrentPosition(_onSuccess, _onError);
}
}, []);

useEffect(() => {
_fetchData();
}, [coords]);
}, [location]);

if (!data) {
return null;
Expand Down
16 changes: 16 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* File: config.js
* Project: watcher
* File Created: Saturday, 22nd April 2023 9:28:19 pm
* Author: Umar Aamer ([email protected])
* -----
* Last Modified: Saturday, 22nd April 2023 9:28:20 pm
* -----
* Copyright <<year>> - 2023 WhileGeek, https://umar.tech
*/
export const config = {
coordinates: {
lat: "33.6461432",
lng: "73.0523224",
}
}
41 changes: 41 additions & 0 deletions src/hooks/useLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* File: useLocation.js
* Project: watcher
* File Created: Saturday, 22nd April 2023 9:29:15 pm
* Author: Umar Aamer ([email protected])
* -----
* Last Modified: Saturday, 22nd April 2023 9:29:15 pm
* -----
* Copyright <<year>> - 2023 WhileGeek, https://umar.tech
*/
import { useState, useEffect } from 'react';
import { config } from '../config';

const useLocation = () => {
const [location, setLocation] = useState({
latitude: config.coordinates.lat,
longitude: config.coordinates.lng,
});

useEffect(() => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
setLocation({
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
},
error => {
console.error(error);
}
);
} else {
console.error('Geolocation is not supported by this browser.');
}
}, []);

return location;
};

export default useLocation;
Loading

0 comments on commit 00b5093

Please sign in to comment.