Skip to content

Commit

Permalink
Fix achievements
Browse files Browse the repository at this point in the history
  • Loading branch information
aweell committed Nov 26, 2024
1 parent cab8afa commit f48ba00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
28 changes: 18 additions & 10 deletions src/pages/advent-calendar-2024/pages/progress-view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Snow from "../components/snow.tsx";
import {
ACHIEVEMENT_PREFIX,
achievementsConfig,
getAchievementFromLocalStorage,
} from "../utils/achievement-config";
import { TOTAL_CALENDAR_DAYS } from "../utils/constants";

Expand All @@ -38,20 +39,27 @@ const ProgressView = () => {
const { isTabletOrSmaller } = useScreenSize();

useEffect(() => {
// Retrieve and parse completed days
const storedDays = localStorage.getItem("completedDays");
const parsedDays = JSON.parse(storedDays);
const parsedDays = storedDays ? JSON.parse(storedDays) : [];
setCompletedDays(parsedDays);

const storedAchievements = localStorage.getItem("achievements");
const parsedAchievements = JSON.parse(storedAchievements);
const achievementsState = parsedAchievements.reduce((acc, id) => {
acc[id] = { isCompleted: true, isSecret: false };
return acc;
}, {});
setAchievements(achievementsState);

const scores = JSON.parse(localStorage.getItem("gameScores")) || {};
// Retrieve and parse game scores
const storedScores = localStorage.getItem("gameScores");
const scores = storedScores ? JSON.parse(storedScores) : {};
setGameScores(scores);

// Dynamically build the achievements state based on localStorage values
const achievementsState = {};
achievementsConfig.forEach(({ id, isSecret }) => {
// If an achievement is stored in localStorage and is completed, set it to true
const isCompleted = getAchievementFromLocalStorage(id); // If the achievement is in localStorage, it's considered completed
achievementsState[id] = {
isCompleted, // true if completed, false if not
isSecret,
};
});
setAchievements(achievementsState);
}, []);

const handleClearData = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const isChristmasDay = (date) => {
export const ACHIEVEMENT_PREFIX = "achievement_"; // Prefix for localStorage keys

// Function to get achievement status from localStorage
const getAchievementFromLocalStorage = (id) => {
export const getAchievementFromLocalStorage = (id) => {
return JSON.parse(localStorage.getItem(ACHIEVEMENT_PREFIX + id)) || false;
};

Expand Down
2 changes: 1 addition & 1 deletion src/pages/advent-calendar-2024/utils/content-config.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const contentByDate = {
"Can you guess which Christmas movie these emojis represent? 🎬.",
},
"2024-12-04": {
repeatable: false,
repeatable: true,
illustration: <Illustration04 />,
illustrationDimmed: <Illustration04 disabled />,
content: (
Expand Down

0 comments on commit f48ba00

Please sign in to comment.