From 7c0ca6e3730853b9875b1806140c102d43944fdb Mon Sep 17 00:00:00 2001 From: Clemente Solorio Date: Wed, 10 Apr 2024 19:51:40 -0700 Subject: [PATCH] fix #75: persist farmhand across page refreshes --- bun.lockb | Bin 210543 -> 210543 bytes src/components/dialogs/deletion-dialog.tsx | 15 +++++++++++++-- src/contexts/players-context.tsx | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/bun.lockb b/bun.lockb index efd148c694592dc2b83d0ceb60f42074dd4b2240..a1353a0b94292ea25dd998f642f3e00df00207a4 100755 GIT binary patch delta 3352 zcmYk-N6?&f9LDi%jM!dDY}iqwVn<`a62*=s5sigdu``nyha=wh`;8MA7&6JmX9*&*%UD?(VRco!zm0ZpZeyO&hmNCXXju)-P_4 zb)J-SiXE`-Q&KYQi1kV-IaYaE&OWxls?SIXuq9U8Dka2LSp8Wk5w^w}&q%1r>#SU2aB`Fzp#Ck7F$+5~Sa`v$WR((}UfGx4wf|L+j zVfELfMA#Z@)KaF{25Y`9Wrl6B)*Dh{Y=^bql#*b3tn-$X6gyzux20s*5$nApCC4i7 z%Gt*jShbN7U`wp_o|F(_(k9B^Ll41v}`>T`;J7T@xq~us-kh701 zuGoG5d+`J^D~zK8)SiKZfkq$2uq#E8_ySb;bt4Kepa zoROAT`yBVosEUaq~$*)I$&hQWEV@#E?`(KMXM< zH8BoHoRWr^MUyPWLo;a5vreq-QrHC0BiFX-d zPL#`W^GQL}D-Z)x675RFkW@r>5F=6(<0`}{X^43>;*7M!x&|>O9kH)POh`|hHHax0 zh`SasBO~#yL(GZd;^vcrsMjL~q$Ju6h#{$nz78=WH8E~PoRWr^HzCeQORSp_W6}}( z7Q}?~#PJYQG7xt?Vn!DKF6Ec*-3oG|+=iP^3ZmYQ7?6@^cOZtOBKn<(5vhrB7vhvO z#Pktoq$Sqfh%xDieGg(ndg9!Rn393G_aSCvB;NgqIZ+A4CjDNwfg*v2YU1 guXt#(Y<|-tlNFnHK0MjB^wEV4lg$eomOioYAM+v4EdT%j delta 3350 zcmYk-N37gr7=__GA((b?LNnb!0MiYjm=ZAElmtU4zEsnVA|Wq_-K@~CvxHE5citd* zX=UXs0)|jbyO?geX~DGMP)!LK<~v77Y|iul@65=v8O@IE^E;ooR!W9dzLT?$&9UlsDFL>?YTruQl@*4QCsf~~RU4^pPs25bE&CBn8?`zI+ew!=C?YQIYfu_adjL&_LiVU0hfOt3Z9+$m*>ZLro~QX*`Nwf~k9V>_%fNJ+3g z*8N9HiXCJ(dL!9Pc2OAtpX5Ych8U27XuBYWq$K)s#4)Leu`A+))WlQ}r=%g)Zio?S ziM=~wOgiH1ftZkl=3`t4!6^LU}5o0Cdgw({` z2XRUoV(p64|#)VoC<$X^0t74#dqTIZ+Qn3`jw=gAqeg68#Xw zF{y}gDB^_F#5@dfN*ZDvju?@a*g9fNI^rCGn2?^hMSiG3zwOgiG6g_w|@xE5kc2I8HKm=Wb1+ zE;$%%RyVn7O_U5*%%lIRZNm{i2L0&zlWVqS?jB@MBzLX1dD?5h!D(h+AZ zVnTZ2u0u@8K)h=ZGorY-`6MUmwTJ;Jh;|)fNJ^rwM;w!i7}q0CNKMQe5T~Re){Te} zX^DLkVoW;Xc!&w2b=Nlw)J5d%^XEf_5< lJ{pc5SX}wwX!+u%hes0cqn%{u@9 diff --git a/src/components/dialogs/deletion-dialog.tsx b/src/components/dialogs/deletion-dialog.tsx index eae361e9..69362920 100644 --- a/src/components/dialogs/deletion-dialog.tsx +++ b/src/components/dialogs/deletion-dialog.tsx @@ -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(""); @@ -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(); } }; @@ -144,7 +155,7 @@ export const DeletionDialog = ({ open, setOpen, playerID, type }: Props) => { The following farmhands will be deleted: -
    +
      {playerID ? (
    • {`${selectedPlayer?.general?.name} - ${selectedPlayer?.general?.farmInfo}`} diff --git a/src/contexts/players-context.tsx b/src/contexts/players-context.tsx index def9012c..a0fd1e02 100644 --- a/src/contexts/players-context.tsx +++ b/src/contexts/players-context.tsx @@ -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]); @@ -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 (