Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions components/challenge/attempt/attempt-carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const AttemptCarousel = () => {
currentQuestionIndex,
} = useAttemptCarousel();
const { questions, answers } = useAttempt();
console.log({ questions });
const isCompleted = useMemo(
() => isQuestionCompleted(questions[currentQuestionIndex], answers),
[answers, currentQuestionIndex, questions],
Expand All @@ -38,7 +37,7 @@ const AttemptCarousel = () => {
<FlatList
data={questions}
renderItem={renderItem}
keyExtractor={(item) => item._id}
keyExtractor={(item) => item.id}
scrollEnabled={false}
horizontal
pagingEnabled
Expand Down
4 changes: 2 additions & 2 deletions components/challenge/attempt/attempt-slide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ const AttemptSlide = ({ style, currentQuestion }: Props) => {
data={currentQuestion.options}
ItemSeparatorComponent={Separator}
renderItem={({ item, index }) => {
const isChosen = answers.includes(item._id);
const isChosen = answers.includes(item.id);
const isCorrect = item.isCorrect;
return (
<AttemptQuestion
option={item}
delay={index * 1000}
onPress={() => {
if (mode === 'view') return;
completeQuestion(item._id, currentQuestion);
completeQuestion(item.id, currentQuestion);
}}
type={getAttemptType(mode, isChosen, isCorrect)}
/>
Expand Down
8 changes: 3 additions & 5 deletions components/shared/challenge-list/challenge-attempts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Feather } from '@expo/vector-icons';
import { BottomSheetFlatList } from '@gorhom/bottom-sheet';
import { format } from 'date-fns';
import { useCallback } from 'react';
import { Platform, Pressable, View } from 'react-native';
import { createStyleSheet, useStyles } from 'react-native-unistyles';
Expand All @@ -10,6 +9,7 @@ import Text from '@/components/ui/text';
import { useAttemptByChallengeId } from '@/data/fetchers/attempt.fetcher';
import { useGetChallengeById } from '@/data/fetchers/challenge.fetcher';
import { useAttempt } from '@/hooks/attempt/useAttempt';
import { formatDate } from '@/utils/date.utils';
import { hex2rgba } from '@/utils/ui.utils';

interface Props {
Expand Down Expand Up @@ -51,7 +51,7 @@ const ChallengeAttempts = ({ goBack, challengeId }: Props) => {
<BottomSheetFlatList
style={styles.container}
data={attempts}
keyExtractor={(item) => item._id}
keyExtractor={(item) => item.id}
ListHeaderComponent={header}
contentContainerStyle={styles.content}
ListFooterComponent={footer}
Expand All @@ -64,9 +64,7 @@ const ChallengeAttempts = ({ goBack, challengeId }: Props) => {
<Text weight="800" size={19}>
{`${item.score}%`}
</Text>
<Text size={14}>
{format(new Date(item.createdAt), 'dd/MM/yyyy')}
</Text>
<Text size={14}>{formatDate(item.createdAt)}</Text>
</View>
<Feather name="chevron-right" size={24} color="#FFFFFF" />
</Pressable>
Expand Down
4 changes: 2 additions & 2 deletions components/shared/challenge-list/challenge-list.item.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Feather } from '@expo/vector-icons';
import { format } from 'date-fns';
import React from 'react';
import { Pressable, View } from 'react-native';
import { createStyleSheet, useStyles } from 'react-native-unistyles';

import Spinner from '@/components/ui/spinner';
import Text from '@/components/ui/text';
import type { MinChallenge } from '@/types/challenge';
import { formatDate } from '@/utils/date.utils';
interface Props {
startAttempt: () => void;
item: MinChallenge;
Expand All @@ -21,7 +21,7 @@ const ChallengeListItem = ({ item, startAttempt, isLoading }: Props) => {
{item.title}
</Text>
<Text size={13} weight="300">
{`${item.difficulty} ⭐️ - ${format(new Date(item.createdAt), 'dd/MM/yyyy')}`}
{`${item.difficulty} ⭐️ - ${formatDate(item.createdAt)}`}
</Text>
</View>
<View style={styles.cardActions}>
Expand Down
6 changes: 3 additions & 3 deletions components/shared/challenge-list/challenge-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const ChallengeList = () => {
return (
<ChallengeListItem
item={item}
startAttempt={() => present(item._id)}
isLoading={isPending && variables === item._id}
startAttempt={() => present(item.id)}
isLoading={isPending && variables === item.id}
/>
);
},
Expand Down Expand Up @@ -72,7 +72,7 @@ const ChallengeList = () => {
showsVerticalScrollIndicator={false}
ListHeaderComponent={header}
data={data?.data}
keyExtractor={(item) => item._id}
keyExtractor={(item) => item.id}
renderItem={renderItem}
contentContainerStyle={styles.contentContainer}
ItemSeparatorComponent={separator}
Expand Down
2 changes: 1 addition & 1 deletion components/upload/upload-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const UploadHeader = () => {
const { styles } = useStyles(stylesheet);
return (
<View style={styles.container}>
<Text weight={800} style={styles.title}>
<Text weight={'800'} style={styles.title}>
Añade archivo
</Text>
</View>
Expand Down
5 changes: 4 additions & 1 deletion data/api/attempt.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ type AttemptCreationBody = {
};

export const createAttempt = async (body: AttemptCreationBody) => {
console.log('Creating attempt...');
console.log(JSON.stringify(body, null, 2));
const { data } = await privateApi.post<Attempt>('/attempt', body);
return data;
};

export const getAttemptByChallengeId = async (challengeId: string) => {
const { data } = await privateApi.get<Attempt[]>(
`/attempt/${challengeId}/all`,
`/attempt/challenge/${challengeId}`,
);
console.log(JSON.stringify(data));
return data;
};
2 changes: 1 addition & 1 deletion data/api/auth.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const refreshToken = async () => {
export const getUserProfile = async () => {
const { data } = await privateApi.get<User>('/auth/profile');
try {
Analytics.identifyUser(data._id, {
Analytics.identifyUser(data.id, {
name: data.name,
email: data.email,
avatar: data.picture,
Expand Down
4 changes: 2 additions & 2 deletions data/fetchers/attempt.fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { getResults } from '@/utils/challenge.utils';
import { createAttempt, getAttemptByChallengeId } from '../api/attempt.api';

export const useCreateAttempt = () => {
const { answers, questions, challengeId } = useAttempt();
const { answers, questions, challenge } = useAttempt();

return useMutation({
mutationFn: () =>
createAttempt({
answers: Object.values(answers),
challenge: challengeId!,
challenge: challenge?.id!,
score: parseInt(
getResults(questions, answers).percentage.toString(),
10,
Expand Down
8 changes: 4 additions & 4 deletions data/state/attempt.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ const storeApi: StateCreator<AttemptState, [['zustand/immer', never]]> = (
setChallenge: (challenge) => {
set((state) => {
state.questions = challenge.questions;
state.challengeId = challenge._id;
state.challengeId = challenge.id;
state.challenge = challenge;
});
},
completeQuestion: (answerId, question) => {
set((state) => {
const alreadyCompleted = question.options.find((o) =>
state.answers.includes(o._id),
state.answers.includes(o.id),
);
if (alreadyCompleted) {
state.answers = state.answers.filter((a) => a !== alreadyCompleted._id);
state.answers = state.answers.filter((a) => a !== alreadyCompleted.id);
state.answers.push(answerId);
} else {
state.answers.push(answerId);
Expand All @@ -63,7 +63,7 @@ const storeApi: StateCreator<AttemptState, [['zustand/immer', never]]> = (
setMode: ({ mode, challenge, answers }) => {
set((state) => {
state.mode = mode;
state.challengeId = challenge?._id;
state.challengeId = challenge?.id;
state.questions = challenge?.questions || [];
if (answers) state.answers = answers;
});
Expand Down
5 changes: 2 additions & 3 deletions types/attempt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export interface Attempt {
challenge: string;
score: number;
answers: string[];
_id: string;
createdAt: Date;
__v: number;
id: string;
createdAt: number;
}
31 changes: 17 additions & 14 deletions types/challenge.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
export interface MinChallenge {
title: string;
description: string;
difficulty: string;
createdAt: number;
id: string;
}

export interface Challenge {
questions: Question[];
id: string;
title: string;
description: string;
provider: string;
totalTokens: number;
difficulty: string;
_id: string;
title: string;
description: string;
createdAt: string;
ownerId: string;
language: string;
createdAt: number;
questions: Question[];
}

export interface Question {
id: string;
question: string;
options: Option[];
_id: string;
}

export interface Option {
id: string;
description: string;
_id: string;
isCorrect: boolean;
}
export interface MinChallenge {
_id: string;
title: string;
description: string;
createdAt: string;
difficulty: number;
}
17 changes: 5 additions & 12 deletions types/user.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
export interface User {
_id: string;
thirdPartyAuthId?: string;
id: string;
thirdPartyAuthId: string;
email: string;
emailVerified: boolean;
name: string;
picture?: string;
picture: string;
locale: string;
createdAt: number;
updatedAt: null;
isActive: boolean;
attemptsMade: number;
challengesMade: number;
actions: Actions;
createdAt: Date;
updatedAt: Date;
}

export interface Actions {
ocr: number;
textExtraction: number;
geminiCredits: number;
}
6 changes: 3 additions & 3 deletions utils/challenge.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const getCorrectAnswers = (
return questions.reduce((acc, question) => {
const correctOptionId = question.options.find(
(option) => option.isCorrect,
)?._id;
)?.id;
return correctOptionId && answers.includes(correctOptionId) ? acc + 1 : acc;
}, 0);
};
Expand All @@ -34,9 +34,9 @@ export const getResults = (questions: Question[], answers: string[]) => {

export const isCorrectAnswer = (question: Question, answer: string) => {
const correctOption = question.options.find((option) => option.isCorrect);
return correctOption?._id === answer;
return correctOption?.id === answer;
};

export const isQuestionCompleted = (question: Question, answers: string[]) => {
return question?.options?.some((option) => answers.includes(option._id));
return question?.options?.some((option) => answers.includes(option.id));
};
6 changes: 6 additions & 0 deletions utils/date.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { format } from 'date-fns';

export const formatDate = (date: number) => {
const finalDate = new Date(date * 1000);
return format(finalDate, 'dd/MM/yyyy');
};
Loading