Skip to content

Commit

Permalink
Improve challenge navigation, query optimization, and fix delete goal
Browse files Browse the repository at this point in the history
  • Loading branch information
CXChristian committed Nov 19, 2024
1 parent 02609bb commit e28e53e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
14 changes: 8 additions & 6 deletions app/(tabs)/challenges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isBefore,
isAfter,
} from "date-fns";
import { Link } from "expo-router";
import { Link, router } from "expo-router";

const getChallengeStatus = (startDate: Date, endDate: Date) => {
const currentDate = new Date();
Expand Down Expand Up @@ -52,11 +52,13 @@ export default function ChallengesScreen() {
</Text>

{/* TODO: Allow only admin to create challenges */}
<Link href="/challenges/create" asChild>
<Button size="icon" className="h-14 w-14 rounded-full">
<Plus color="#fff" size={30} />
</Button>
</Link>
<Button
size="icon"
className="h-14 w-14 rounded-full"
onPress={() => router.push("/challenges/create")}
>
<Plus color="#fff" size={30} />
</Button>

<FlatList
contentContainerStyle={{
Expand Down
5 changes: 1 addition & 4 deletions app/challenges/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ export default function ChallengeScreen() {
const createGoal = useMutation(api.goals.createGoal);
const deleteGoalAndGoalLogs = useMutation(api.goals.deleteGoalAndGoalLogs);
const userGoals = useQuery(api.goals.listGoals);
const challengeGoals = useQuery(api.challengeGoals.listChallengeGoals);
const filteredChallengeGoals = challengeGoals?.filter(
(goal) => goal.challengeId === id
);
const filteredChallengeGoals = useQuery(api.challengeGoals.listChallengeGoalsById, { goalId: id });
const createGoalLogsFromGoal = useMutation(
api.goalLogs.createGoalLogsFromGoal
);
Expand Down
32 changes: 17 additions & 15 deletions app/challenges/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,25 @@ function CreateChallengeForm() {

<Button
onPress={async () => {
const newChallenge = {
name,
description,
repeat: ["daily"],
unitType: "General",
unitValue: 1,
unit: "times",
recurrence: "per day",
startDate: startDate.getTime(),
endDate: endDate.getTime(),
points: points ? parseInt(points) : 0,
};
const challengeId = await createChallenge(newChallenge);
if (!challengeId) {
try {
const newChallenge = {
name,
description,
repeat: ["daily"],
unitType: "General",
unitValue: 1,
unit: "times",
recurrence: "per day",
startDate: startDate.getTime(),
endDate: endDate.getTime(),
points: points ? parseInt(points) : 0,
};

await createChallenge(newChallenge);
router.dismiss();
} catch (error) {
throw new Error("Failed to create challenge");
}
router.navigate("/challenges");
}}
>
<Text>Add a sample challenge</Text>
Expand Down
2 changes: 1 addition & 1 deletion app/goals/[goalId]/[goalLogId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ export default function GoalScreen() {
{
text: "Yes",
onPress: async () => {
router.dismiss(); // Dismiss first, page will break if goal does not exist before exit. Read nonexistent data
await deleteGoalAndGoalLogs({ goalId });
router.dismiss();
},
style: "destructive",
},
Expand Down
16 changes: 16 additions & 0 deletions convex/challengeGoals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ export const listChallengeGoals = query({
return goals;
},
});

export const listChallengeGoalsById = query({
args: { goalId: v.id("challenges") },
handler: async (ctx, { goalId }) => {
const userId = await getAuthUserId(ctx);
if (userId === null) {
return null;
}
const goals = await ctx.db
.query("challengeGoals")
.filter((q) => q.eq(q.field("challengeId"), goalId))
.collect();

return goals;
},
});

0 comments on commit e28e53e

Please sign in to comment.