Skip to content

Commit

Permalink
Remove params generation
Browse files Browse the repository at this point in the history
  • Loading branch information
aweell committed Nov 26, 2024
1 parent 32c778d commit 4ab4df1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 102 deletions.
16 changes: 0 additions & 16 deletions src/pages/advent-calendar-2024/components/corner-layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ const CornerLayout = () => {
};
}, []);

const handleViewProgress = () => {
const completedDays =
JSON.parse(localStorage.getItem("completedDays")) || [];

// Encode completed days as a Base64 string
const encodedDays = base64Encode(completedDays.join(","));
const params = new URLSearchParams({
completedDays: encodedDays,
});

navigate(`/advent-calendar-2024/progress-view?${params.toString()}`);
};

const copyToClipboard = () => {
const url = window.location.href; // Get the current URL

Expand All @@ -60,9 +47,6 @@ const CornerLayout = () => {
.catch((err) => console.error("Failed to copy: ", err));
};

// Helper function to determine if the link should be underlined
const isCurrentPage = (path) => location.pathname === path;

return (
<ResponsiveLayout>
{isTabletOrBigger && (
Expand Down
33 changes: 6 additions & 27 deletions src/pages/advent-calendar-2024/components/navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { useLocation, useNavigate } from "react-router-dom";
import { base64Encode } from "../utils/url-encoder";
import RotatingSVG from "./label-rotate";

const SheetView = ({ isOpen, onClose, handleViewProgress }) => {
const SheetView = ({ isOpen, onClose }) => {
return (
isOpen && (
<Sheet onClose={onClose}>
Expand Down Expand Up @@ -59,7 +59,7 @@ const SheetView = ({ isOpen, onClose, handleViewProgress }) => {
</Circle>
<p>
In{" "}
<TextLink onPress={handleViewProgress}>
<TextLink to={`/advent-calendar-2024/progress-view`}>
My Progress
</TextLink>{" "}
page, you can see the days you've completed, your
Expand Down Expand Up @@ -120,19 +120,6 @@ const NavBar = () => {
};
}, []);

const handleViewProgress = () => {
const completedDays =
JSON.parse(localStorage.getItem("completedDays")) || [];

// Encode completed days as a Base64 string
const encodedDays = base64Encode(completedDays.join(","));
const params = new URLSearchParams({
completedDays: encodedDays,
});

navigate(`/advent-calendar-2024/progress-view?${params.toString()}`);
};

// Helper function to determine if the link should be underlined
const isCurrentPage = (path) => location.pathname === path;

Expand All @@ -152,7 +139,7 @@ const NavBar = () => {
},
{
title: "My Progress",
onPress: handleViewProgress,
to: "/advent-calendar-2024/progress-view",
decoration: isCurrentPage("/advent-calendar-2024/progress-view")
? "underline"
: "none",
Expand All @@ -169,11 +156,7 @@ const NavBar = () => {
},
]}
/>
<SheetView
isOpen={isSheetOpen}
onClose={() => setIsSheetOpen(false)}
handleViewProgress={handleViewProgress}
/>
<SheetView isOpen={isSheetOpen} onClose={() => setIsSheetOpen(false)} />
</>
) : (
<ResponsiveLayout>
Expand Down Expand Up @@ -205,7 +188,7 @@ const NavBar = () => {
>
<TextLink
style={{ color: skinVars.colors.textPrimary }}
onPress={handleViewProgress}
to={"/advent-calendar-2024/progress-view"}
>
My progress
</TextLink>
Expand All @@ -232,11 +215,7 @@ const NavBar = () => {
</Inline>
</Inline>
</Box>
<SheetView
isOpen={isSheetOpen}
onClose={() => setIsSheetOpen(false)}
handleViewProgress={handleViewProgress}
/>
<SheetView isOpen={isSheetOpen} onClose={() => setIsSheetOpen(false)} />
</ResponsiveLayout>
);
};
Expand Down
38 changes: 7 additions & 31 deletions src/pages/advent-calendar-2024/pages/calendar-view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ import { updateCompletedDays } from "../utils/state-manager";
import { base64Encode } from "../utils/url-encoder.jsx";

const CalendarView = () => {
const location = useLocation();
const navigate = useNavigate();
const { isMobile } = useScreenSize();

// Load completed days from local storage on initial mount
Expand Down Expand Up @@ -90,28 +88,21 @@ const CalendarView = () => {
const newCompletedDays = [...completedDays, date];
// Update completed days state and local storage
localStorage.setItem("completedDays", JSON.stringify(newCompletedDays));
updateCompletedDays(
newCompletedDays,
setCompletedDays,
navigate,
location
);
updateCompletedDays(newCompletedDays, setCompletedDays);

// Check for achievements
checkAndUnlockAchievements(
newCompletedDays,
achievements,
setAchievements,
navigate,
location,
handleShowToast
);
}
};

const clearLocalStorage = () => {
localStorage.removeItem("completedDays"); // Clear from local storage
updateCompletedDays([], setCompletedDays, navigate, location); // Clear state
updateCompletedDays([], setCompletedDays); // Clear state
achievementsConfig.forEach(({ id }) => {
localStorage.removeItem(ACHIEVEMENT_PREFIX + id);
});
Expand Down Expand Up @@ -154,20 +145,7 @@ const CalendarView = () => {
}, [completedDays, availableDays, calendarDays]);
const [isSheetOpen, setIsSheetOpen] = useState(false);

const handleViewProgress = () => {
const completedDays =
JSON.parse(localStorage.getItem("completedDays")) || [];

// Encode completed days as a Base64 string
const encodedDays = base64Encode(completedDays.join(","));
const params = new URLSearchParams({
completedDays: encodedDays,
});

navigate(`/advent-calendar-2024/progress-view?${params.toString()}`);
};

const SheetView = ({ isOpen, onClose, handleViewProgress }) => {
const SheetView = ({ isOpen, onClose }) => {
return (
isOpen && (
<Sheet onClose={onClose}>
Expand Down Expand Up @@ -205,7 +183,9 @@ const CalendarView = () => {
</Circle>
<p>
In{" "}
<TextLink onPress={handleViewProgress}>
<TextLink
to={`/advent-calendar-2024/progress-view`}
>
My Progress
</TextLink>{" "}
page, you can see the days you've completed, your
Expand Down Expand Up @@ -332,11 +312,7 @@ const CalendarView = () => {
</Stack>
</Box>
</ResponsiveLayout>
<SheetView
isOpen={isSheetOpen}
onClose={() => setIsSheetOpen(false)}
handleViewProgress={handleViewProgress}
/>
<SheetView isOpen={isSheetOpen} onClose={() => setIsSheetOpen(false)} />
<ToastWrapper toasts={toasts} removeToast={removeToast} />
</>
);
Expand Down
6 changes: 1 addition & 5 deletions src/pages/advent-calendar-2024/utils/achievement-config.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ export const checkAndUnlockAchievements = (
newCompletedDays,
achievements,
setAchievements,
navigate,
location,
showToast
) => {
achievementsConfig.forEach(({ id, check, name, message, icon, isSecret }) => {
Expand All @@ -160,9 +158,7 @@ export const checkAndUnlockAchievements = (
Object.keys(achievements)
.filter((key) => achievements[key].isCompleted)
.map((key) => key),
setAchievements,
navigate,
location
setAchievements
);
}
}
Expand Down
25 changes: 2 additions & 23 deletions src/pages/advent-calendar-2024/utils/state-manager.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
// utils/stateManager.js
import { base64Encode } from "./url-encoder";

export const updateCompletedDays = (
days,
setCompletedDays,
navigate,
location
) => {
export const updateCompletedDays = (days, setCompletedDays) => {
setCompletedDays(days);
localStorage.setItem("completedDays", JSON.stringify(days));
const params = new URLSearchParams(location.search);
params.set("completedDays", base64Encode(days.join(",")));
navigate({ search: params.toString() }, { replace: true });
};

export const updateAchievements = (
newAchievements,
setAchievements,
navigate,
location
) => {
export const updateAchievements = (newAchievements, setAchievements) => {
// Get existing achievements from local storage
const existingAchievements =
JSON.parse(localStorage.getItem("achievements")) || [];
Expand All @@ -32,9 +16,4 @@ export const updateAchievements = (
// Update state and local storage
setAchievements(updatedAchievements);
localStorage.setItem("achievements", JSON.stringify(updatedAchievements));

// Update the URL parameters
const params = new URLSearchParams(location.search);
params.set("achievements", base64Encode(updatedAchievements.join(",")));
navigate({ search: params.toString() }, { replace: true });
};

0 comments on commit 4ab4df1

Please sign in to comment.