Skip to content

Commit

Permalink
feat: add first load control
Browse files Browse the repository at this point in the history
  • Loading branch information
alevidals committed Apr 7, 2024
1 parent b705189 commit 043189b
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/planets/add-planet-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { useToast } from "@/components/ui/use-toast";
import { usePlanets } from "@/lib/atoms";
import { insertPlanetSchema } from "@/lib/schemas";
import { usePlanets } from "@/lib/store";
import type { InsertPlanet, Planet } from "@/lib/types";
import { zodResolver } from "@hookform/resolvers/zod";
import { IconPlus, IconTrash } from "@tabler/icons-react";
Expand Down
2 changes: 1 addition & 1 deletion src/components/planets/delete-planet-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { toast } from "@/components/ui/use-toast";
import { usePlanets } from "@/lib/atoms";
import { usePlanets } from "@/lib/store";
import type { Planet } from "@/lib/types";

type Props = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/planets/edit-planet-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { useToast } from "@/components/ui/use-toast";
import { usePlanets } from "@/lib/atoms";
import { updatePlanetSchema } from "@/lib/schemas";
import { usePlanets } from "@/lib/store";
import type { Planet, UpdatePlanet } from "@/lib/types";
import { zodResolver } from "@hookform/resolvers/zod";
import { IconPlus, IconTrash } from "@tabler/icons-react";
Expand Down
2 changes: 1 addition & 1 deletion src/components/planets/planet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Heading } from "@/components/heading";
import { ResidentCard } from "@/components/resident-card";
import { useMediaQuery } from "@/hooks/use-media-query";
import { usePlanets } from "@/lib/atoms";
import { usePlanets } from "@/lib/store";
import Image from "next/image";
import { notFound } from "next/navigation";

Expand Down
7 changes: 4 additions & 3 deletions src/components/planets/planets-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { PageOutOfBound } from "@/components/planets/page-out-of-bound";
import { PlanetCard } from "@/components/planets/planet-card";
import { PlanetsNotFound } from "@/components/planets/planets-not-found";

import { usePlanets } from "@/lib/atoms";
import { ITEMS_PER_PAGE } from "@/lib/constants";
import { usePlanets } from "@/lib/store";
import type {
Order,
OrderByField,
Expand Down Expand Up @@ -98,16 +98,17 @@ export function PlanetsList(props: Props) {

const searchParams = useSearchParams();

const { planets, addPlanets } = usePlanets();
const { planets, addPlanets, setFirstLoadDone } = usePlanets();

useEffect(() => {

Check warning on line 103 in src/components/planets/planets-list.tsx

View workflow job for this annotation

GitHub Actions / biome

This hook does not specify all of its dependencies: addPlanets

Check warning on line 103 in src/components/planets/planets-list.tsx

View workflow job for this annotation

GitHub Actions / biome

This hook does not specify all of its dependencies: initialPlanets

Check warning on line 103 in src/components/planets/planets-list.tsx

View workflow job for this annotation

GitHub Actions / biome

This hook does not specify all of its dependencies: setFirstLoadDone
const storageItem = localStorage.getItem("planets");
const local = storageItem
? (JSON.parse(storageItem) as ZustandPlanetsStorage)
: undefined;

if (!local?.state.planets) {
if (!local?.state.planets || !local?.state.firstLoadDone) {
addPlanets(initialPlanets);
setFirstLoadDone(true);
}
}, []);

Expand Down
4 changes: 4 additions & 0 deletions src/lib/atoms.ts → src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { persist } from "zustand/middleware";

type PlanetsStore = {
planets: Planet[] | null;
firstLoadDone: boolean | null;
setFirstLoadDone: (value: boolean) => void;
addPlanet: (planet: Planet) => void;
addPlanets: (planets: Planet[]) => void;
updatePlanet: (planet: Planet) => void;
Expand All @@ -14,6 +16,8 @@ export const usePlanets = create<PlanetsStore>()(
persist(
(set) => ({
planets: null,
firstLoadDone: null,
setFirstLoadDone: (value: boolean) => set({ firstLoadDone: value }),
addPlanet: (planet: Planet) =>
set((state) => ({ planets: [...(state.planets ?? []), planet] })),
addPlanets: (planets: Planet[]) =>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export type Order = "asc" | "desc";

export type ZustandPlanetsStorage = {
state: {
planets: Planet[];
planets: Planet[] | null;
firstLoadDone: boolean | null;
};
version: number;
} | null;
Expand Down

0 comments on commit 043189b

Please sign in to comment.