Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use store instead of props to pass userUUID #19

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function DashboardPage() {
</div>
<div className="hidden items-start justify-center gap-6 rounded-lg p-8 md:grid lg:grid-cols-2 xl:grid-cols-3">
<DashboardContainer>
<SelectCoachingSession userUUID={userUUID} />
<SelectCoachingSession />
</DashboardContainer>
</div>
</>
Expand Down
20 changes: 6 additions & 14 deletions src/components/ui/dashboard/select-coaching-session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ import {
SelectValue,
} from "@/components/ui/select";
import { fetchOrganizationsByUserId } from "@/lib/api/organizations";
import { useAuthStore } from "@/lib/providers/auth-store-provider";
import { Organization, defaultOrganizations } from "@/types/organization";
import { useEffect, useState } from "react";

export interface CoachingSessionProps {
/** The current logged in user's UUID */
userUUID: string;
}

export function SelectCoachingSession({
userUUID,
...props
}: CoachingSessionProps) {
export function SelectCoachingSession() {
const [organizationSelection, setOrganizationSelection] =
useState<string>("");
const [relationshipSelection, setRelationshipSelection] =
Expand Down Expand Up @@ -58,19 +51,18 @@ export function SelectCoachingSession({
const [organizations, setOrganizations] = useState<Organization[]>(
defaultOrganizations()
);
useEffect(() => {
async function loadOrganizations() {
if (!userUUID) return;
const { userUUID } = useAuthStore((state) => state);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't prefer to break encapsulation of the SelectionCoachingSession component by directly using useAuthStore() instead of passing it as a property one level deep. Here's a good explanation of my thought process from Perplexity.ai summarizing other sites on this.

Here's my own thought process: I think it's a logical practice to retrieve state at the page level and pass user session state like userUUID down to components because components are reusable and pages are not. But a component like this one becomes less reusable when tightly coupling the AuthStore into the innards of the component.


await fetchOrganizationsByUserId(userUUID)
useEffect(() => {
if (userUUID) {
fetchOrganizationsByUserId(userUUID)
.then(([orgs]) => {
setOrganizations(orgs);
})
.catch(([err]) => {
console.error("Failed to fetch Organizations: " + err);
});
}
loadOrganizations();
}, [userUUID]);

return (
Expand Down
Loading