Skip to content

Commit

Permalink
Cache changed answers in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
duogenesis committed Dec 19, 2023
1 parent 131cf2a commit 20dea5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
20 changes: 8 additions & 12 deletions components/in-depth-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,14 @@ import { useFocusEffect } from '@react-navigation/native';
import { api } from '../api/api';
import { StatusBarSpacer } from './status-bar-spacer';
import { FloatingBackButton } from './prospect-profile-screen';
import { CardState } from './quiz-card';

const sideMargins: StyleProp<ViewStyle> = {
marginLeft: 10,
marginRight: 10,
};

const AnsweredQuizCard_ = (props: {
children: any,
questionNumber: number
topic: string,
user1: string,
answer1: boolean | null,
user2: string,
answer2: boolean | null,
answer2Publicly: boolean,
}) => <AnsweredQuizCard {...props}/>;

const AnsweredQuizCardMemo = memo(AnsweredQuizCard_);
const AnsweredQuizCardMemo = memo(AnsweredQuizCard);

const Subtitle = ({children}) => {
return (
Expand Down Expand Up @@ -232,6 +222,11 @@ const InDepthScreen = (navigationRef) => ({navigation, route}) => {
const [idx4, setIdx4] = useState(0);

const renderItem = useCallback(({item}) => {
const onStateChange = (state: CardState) => {
item.item.person_answer = state.answer;
item.item.person_public_ = state.public_;
};

switch (item.kind) {
case 'answer':
return <AnsweredQuizCardMemo
Expand All @@ -242,6 +237,7 @@ const InDepthScreen = (navigationRef) => ({navigation, route}) => {
user2="You"
answer2={item.item.person_answer}
answer2Publicly={item.item.person_public_ ?? true}
onStateChange={onStateChange}
>
{item.item.question}
</AnsweredQuizCardMemo>;
Expand Down
28 changes: 22 additions & 6 deletions components/quiz-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const cardPadding = {
paddingBottom: 10,
};

type CardState = {
answer: boolean | null,
public_: boolean,
}

const LeftComponent = ({percentage}) => {
return (
<View
Expand Down Expand Up @@ -537,12 +542,18 @@ const AnsweredQuizCard = ({
user2,
answer2,
answer2Publicly,
onStateChange,
}: {
children: any,
questionNumber: number
topic: string,
user1: string,
answer1: boolean | null,
user2: string,
answer2: boolean | null,
answer2Publicly: boolean,
onStateChange: (state: CardState) => void,
}) => {
type CardState = {
answer: boolean | null,
public_: boolean,
}

const [state, setState] = useState<CardState>({
answer: answer2,
public_: answer2Publicly
Expand Down Expand Up @@ -576,10 +587,14 @@ const AnsweredQuizCard = ({
markTraitDataDirty();
});

return {
const nextState = {
...state,
answer: nextAnswer_,
};

onStateChange(nextState);

return nextState;
});
}, [setState]);

Expand Down Expand Up @@ -905,6 +920,7 @@ const NoMoreCards = () => {

export {
AnsweredQuizCard,
CardState,
NoMoreCards,
QuizCard,
SearchQuizCard,
Expand Down

0 comments on commit 20dea5f

Please sign in to comment.