-
Notifications
You must be signed in to change notification settings - Fork 2
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 #37 from lrtlt/feature/channel-daily-questions
feature: channel daily questions
- Loading branch information
Showing
9 changed files
with
146 additions
and
47 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,50 @@ | ||
import React, {useEffect} from 'react'; | ||
import {StyleSheet, View} from 'react-native'; | ||
import useDailyQuestionData from './useDailyQuestionData'; | ||
import DailyQuestionComponent from './DailyQuestionComponent'; | ||
import {useSelector} from 'react-redux'; | ||
import {selectDailyQuestionChoice} from '../../redux/selectors'; | ||
|
||
interface DailyQuestionWrapperProps { | ||
id: string | number; | ||
} | ||
|
||
const VOTES_REFRESH_RATE = 1000 * 15; // 15 sec | ||
|
||
const DailyQuestionWrapper: React.FC<DailyQuestionWrapperProps> = ({id}) => { | ||
const {question, refresh} = useDailyQuestionData(id); | ||
const answer = useSelector(selectDailyQuestionChoice); | ||
|
||
useEffect(() => { | ||
let interval: NodeJS.Timeout; | ||
if (question) { | ||
const isAnswered = answer?.daily_question_id === question.data.id; | ||
const isQuestionOngoing = question.data.can_vote && !question.data.is_ended; | ||
|
||
if (isAnswered && isQuestionOngoing) { | ||
interval = setInterval(() => { | ||
refresh(question.data.id); | ||
}, VOTES_REFRESH_RATE); | ||
} | ||
} | ||
return () => clearInterval(interval); | ||
}, [question, answer]); | ||
|
||
if (question) { | ||
return ( | ||
<View style={styles.container}> | ||
<DailyQuestionComponent block={question} /> | ||
</View> | ||
); | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
export default DailyQuestionWrapper; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
}); |
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,36 @@ | ||
import {useCallback, useEffect, useState} from 'react'; | ||
import {fetchDailyQuestion} from '../../api'; | ||
import {HomeBlockDailyQuestion} from '../../api/Types'; | ||
|
||
type ReturnType = { | ||
question?: HomeBlockDailyQuestion; | ||
refresh: (id: string | number) => void; | ||
}; | ||
const useDailyQuestionData = (id: string | number): ReturnType => { | ||
const [question, setQuestion] = useState<HomeBlockDailyQuestion | undefined>(); | ||
|
||
const refresh = useCallback(async (id: string | number) => { | ||
try { | ||
const question = await fetchDailyQuestion(id); | ||
if (question?.choices?.length) { | ||
setQuestion({ | ||
type: 'daily_question', | ||
data: question, | ||
}); | ||
} | ||
} catch (e) { | ||
console.log('Error while fetching daily question', e); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
refresh(id); | ||
}, [id]); | ||
|
||
return { | ||
question, | ||
refresh, | ||
}; | ||
}; | ||
|
||
export default useDailyQuestionData; |
11 changes: 5 additions & 6 deletions
11
...ailyQuestion/useSetDailyQuestionChoise.ts → ...ailyQuestion/useSetDailyQuestionChoice.ts
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