Skip to content

Commit

Permalink
Merge pull request #87 from communitycenter/75-STA-135
Browse files Browse the repository at this point in the history
75-STA-135
  • Loading branch information
jacc authored Apr 16, 2024
2 parents 5034abe + 7c0ca6e commit 3309d4d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
15 changes: 13 additions & 2 deletions src/components/dialogs/deletion-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const DeletionDialog = ({ open, setOpen, playerID, type }: Props) => {
const { players } = useContext(PlayersContext);

const selectedPlayer = players?.filter(
(player) => player._id === playerID
(player) => player._id === playerID,
)[0];

const [verify, setVerify] = useState("");
Expand Down Expand Up @@ -74,7 +74,18 @@ export const DeletionDialog = ({ open, setOpen, playerID, type }: Props) => {
? "localhost"
: "stardew.app",
});
window.localStorage.removeItem("player_id");
}

// delete the saved player id if player requested to delete character and it was their saved player
// if player requested to delete all saved data, also remove saved player_id
if (
type &&
type === "player" &&
window.localStorage.getItem("player_id") === _body._id
) {
window.localStorage.removeItem("player_id");
} else if (!type) window.localStorage.removeItem("player_id");
window.location.reload();
}
};
Expand Down Expand Up @@ -144,7 +155,7 @@ export const DeletionDialog = ({ open, setOpen, playerID, type }: Props) => {
<DialogDescription asChild>
<span>
The following farmhands will be deleted:
<ul className="list-disc list-inside">
<ul className="list-inside list-disc">
{playerID ? (
<li>
{`${selectedPlayer?.general?.name} - ${selectedPlayer?.general?.farmInfo}`}
Expand Down
16 changes: 15 additions & 1 deletion src/contexts/players-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,15 @@ export const PlayersProvider = ({ children }: { children: ReactNode }) => {

useEffect(() => {
if (!activePlayerId && players.length > 0) {
setActivePlayerId(players[0]._id);
// first lets check if local storage contains the last set player
if (typeof window !== "undefined") {
const stored = window.localStorage.getItem("player_id");

// also check if the player_id is still in the players array
if (stored && players.some((player) => player._id === stored)) {
setActivePlayerId(stored);
} else setActivePlayerId(players[0]._id);
}
}
}, [activePlayerId, players]);

Expand Down Expand Up @@ -136,7 +144,13 @@ export const PlayersProvider = ({ children }: { children: ReactNode }) => {
setActivePlayerId(undefined);
return;
}

setActivePlayerId(player._id);

if (typeof window !== "undefined") {
console.log(`Setting player_id to '${player._id}'`);
window.localStorage.setItem("player_id", player._id);
}
}, []);

return (
Expand Down

0 comments on commit 3309d4d

Please sign in to comment.