-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from alhabibhasan/master
Hadith of the day
- Loading branch information
Showing
11 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.HadithOfTheDayWrapper { | ||
margin-top: 100px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { Component } from 'react'; | ||
import './View6.css'; | ||
import Logo from '../_components/logo/logo'; | ||
import Clock from '../_components/clock/clock'; | ||
import BuildNumber from '../_components/build-number/build-number'; | ||
import Branding from '../_components/branding/branding'; | ||
import HadithOfTheDay from '../_components/hadith-of-the-day/hadith-of-the-day'; | ||
|
||
class View6 extends Component { | ||
render() { | ||
return ( | ||
<div className="View6"> | ||
<div className="row"> | ||
<Logo /> | ||
</div> | ||
<div className="row"> | ||
<div className="col-12 col-md-12"> | ||
<div className="row"> | ||
<Clock /> | ||
</div> | ||
</div> | ||
</div> | ||
<div className="row"> | ||
<HadithOfTheDay /> | ||
</div> | ||
<BuildNumber /> | ||
<Branding /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default View6; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import View6 from './View6'; | ||
|
||
it('renders without crashing', () => { | ||
const div = document.createElement('div'); | ||
ReactDOM.render(<View6 />, div); | ||
ReactDOM.unmountComponentAtNode(div); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import moment from 'moment'; | ||
import config from '../../config.json'; | ||
import csvtojson from 'csvtojson'; | ||
import request from 'request'; | ||
|
||
export default class HadithData { | ||
constructor() { | ||
this.updateData(); | ||
} | ||
|
||
getHadithSpreadsheetUrl() { | ||
return ( | ||
process.env.REACT_APP_HADITH_DATA_SPREADSHEET_URL || | ||
config.googleSheets.hadithData | ||
); | ||
} | ||
|
||
getHadithFromGoogleSheets() { | ||
let hadithSpreadsheetUrl = this.getHadithSpreadsheetUrl(); | ||
|
||
if (!hadithSpreadsheetUrl) { | ||
alert('Hadith CSV not set'); | ||
} | ||
|
||
return csvtojson() | ||
.fromStream( | ||
request.get(`${hadithSpreadsheetUrl}&_cacheBust=${Math.random()}`) | ||
) | ||
.then(json => { | ||
this.storeHadithData(json); | ||
return json; | ||
}); | ||
} | ||
|
||
getAllHadithData() { | ||
let _hadithData = window.localStorage.getItem('hadithData'); | ||
return _hadithData ? JSON.parse(_hadithData) : null; | ||
} | ||
|
||
getLastUpdatedTime() { | ||
return JSON.parse(window.localStorage.getItem('hadithData_lastUpdated')); | ||
} | ||
|
||
getLastChangedTime() { | ||
return JSON.parse( | ||
window.localStorage.getItem('hadithData_lastChangedHadith') | ||
); | ||
} | ||
|
||
getHadithCurrentIndex() { | ||
return JSON.parse(window.localStorage.getItem('hadithData_currentIndex')); | ||
} | ||
|
||
getCurrentDayHadith() { | ||
let lastHadithChanged = this.getLastChangedTime(); | ||
if (!lastHadithChanged) { | ||
// If last change wasn't set then set it and set current index to 0, as this is the first (re)run. | ||
lastHadithChanged = moment().unix(); | ||
let currentHadithIndex = 0; | ||
this._updateHadithMetadata(lastHadithChanged, currentHadithIndex); | ||
} | ||
|
||
let daysPassed = (moment().unix() - lastHadithChanged) / 60 / 60 / 24; | ||
let oneDayHasPassed = daysPassed > 1 ? true : false; | ||
|
||
let currentHadithIndex = this.getHadithCurrentIndex(); | ||
let hadith = this.getAllHadithData(); | ||
|
||
let currentHadith = { | ||
hadith: | ||
"If you can see this, then the hadith haven't been loaded properly. Try refreshing.", | ||
source: | ||
'Or check if the hadith source URL is correct in your ENV variables or config file.' | ||
}; | ||
|
||
if (oneDayHasPassed) { | ||
currentHadithIndex + 1 >= Object.keys(hadith).length | ||
? (currentHadithIndex = 0) | ||
: ++currentHadithIndex; | ||
|
||
this._updateHadithMetadata(moment().unix(), currentHadithIndex); | ||
} | ||
|
||
if (hadith) { | ||
currentHadith = hadith[currentHadithIndex]; | ||
} | ||
|
||
return currentHadith; | ||
} | ||
|
||
_updateHadithMetadata(lastChanged, currentIndex) { | ||
if (lastChanged && currentIndex >= 0) { | ||
window.localStorage.setItem('hadithData_lastChangedHadith', lastChanged); | ||
window.localStorage.setItem('hadithData_currentIndex', currentIndex); | ||
} | ||
} | ||
|
||
storeHadithData(hadithData = []) { | ||
window.localStorage.setItem('hadithData', JSON.stringify(hadithData)); | ||
window.localStorage.setItem('hadithData_lastUpdated', moment().unix()); | ||
} | ||
|
||
updateData() { | ||
var lastHadithUpdateDiff = moment().unix() - this.getLastUpdatedTime(); | ||
var alreadyHasHadithData = this.getAllHadithData() ? true : false; | ||
if ( | ||
lastHadithUpdateDiff > config.googleSheets.refreshRate * 60 || | ||
!alreadyHasHadithData | ||
) { | ||
this.getHadithFromGoogleSheets(); | ||
console.info('Updating Hadith Data....'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.HadithOfTheDayWrapper { | ||
width: auto; | ||
display: block; | ||
position: fixed; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
text-align: center; | ||
} | ||
|
||
.Hadith { | ||
font-size: 30px; | ||
} | ||
|
||
.HadithSource { | ||
font-size: 22px; | ||
margin: inherit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { Component } from 'react'; | ||
import HadithData from '../hadith-data/hadith-data'; | ||
import './hadith-of-the-day.css'; | ||
|
||
export default class HadithOfTheDay extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.hadithData = new HadithData(); | ||
this.state = { | ||
hadith: this.hadithData.getCurrentDayHadith() | ||
}; | ||
} | ||
render() { | ||
return ( | ||
<div className="HadithOfTheDayWrapper"> | ||
<div className="HadithOfTheDayTitle"> | ||
<h1>Hadith of the day</h1> | ||
</div> | ||
|
||
<blockquote className="blockquote"> | ||
<p className="mb-0 Hadith">{this.state.hadith.hadith}</p> | ||
<footer className="blockquote-footer HadithSource"> | ||
<cite title="Source Title">{this.state.hadith.source}</cite> | ||
</footer> | ||
</blockquote> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters