Skip to content

Commit

Permalink
Auth put in context. (#65)
Browse files Browse the repository at this point in the history
* Auth put in context.

* Move scripts folder out of src.

* Consolidate hooks folders.

* Delete some deprecated file. Remove unneccessary import.

* Rename user geter.

* Add loggedIn status to authContext. Some minor cleanup.

* Remove unneccessary clientProviders wrapper.

* Fix import typo.

* Fix auth check, via token first.

* rename utils directory to lib

* move auth-context to lib

* move crags context  to lib folder

* reorganize log-dialog

* rename server-actions to lib and rename tsx files to ts

* fix some imports

---------

Co-authored-by: Anze Demsar <[email protected]>
  • Loading branch information
salamca and demshy authored Jan 19, 2025
1 parent 0dfda96 commit 3942e23
Show file tree
Hide file tree
Showing 85 changed files with 236 additions and 244 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "next start -p 3003",
"lint": "next lint",
"codegen": "graphql-codegen",
"gengradsys": "node ./src/scripts/generate-grading-systems.mjs"
"gengradsys": "node ./scripts/generate-grading-systems.mjs"
},
"dependencies": {
"@dnd-kit/core": "^6.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ loadEnvConfig(process.cwd());

const fullFilePath = path.join(
process.cwd(),
"src/utils",
"src/lib",
"grading-systems.ts"
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
import { Activity } from "@/graphql/generated";
import displayDate from "@/utils/display-date";
import displayDate from "../../../../lib/display-date";
import CragLink from "@/components/crag-link";
import Grade from "@/components/grade";
import RouteLink from "@/components/route-link";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
import { Activity, DifficultyVote } from "@/graphql/generated";
import displayDate from "@/utils/display-date";
import displayDate from "../../../../lib/display-date";
import CragLink from "@/components/crag-link";
import Grade from "@/components/grade";
import RouteLink from "@/components/route-link";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import { useState } from "react";
import Button from "@/components/ui/button";
import TextArea from "@/components/ui/text-area";
import { Radio, RadioGroup } from "@/components/ui/radio-group";
import createCommentAction from "./server-actions/create-comment-action";
import createCommentAction from "./lib/create-comment-action";
import { useRouter } from "next/navigation";
import { User } from "@/graphql/generated";
import { useAuthContext } from "@/lib/auth/auth-context";

interface Props {
cragId: string;
currentUser: User | null | undefined;
}

enum CommentType {
COMMENT = "comment",
WARNING = "warning",
}

function AddCommentForm({ cragId, currentUser }: Props) {
function AddCommentForm({ cragId }: Props) {
const router = useRouter();
const { currentUser } = useAuthContext();

const [commentType, setCommentType] = useState<CommentType>(
CommentType.COMMENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { useRouter } from "next/navigation";
import Dialog, { DialogSize } from "@/components/ui/dialog";
import Link from "@/components/ui/link";
import updateCommentAction from "./server-actions/update-comment-action";
import deleteCommentAction from "./server-actions/delete-comment-action";
import updateCommentAction from "./lib/update-comment-action";
import deleteCommentAction from "./lib/delete-comment-action";
import TextArea from "@/components/ui/text-area";
import { Radio, RadioGroup } from "@/components/ui/radio-group";
import { useState } from "react";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"use client";

import { User } from "@/graphql/generated";
import { useAuthContext } from "@/lib/auth/auth-context";
import CommentActions from "./comment-actions";

interface CommentProps {
Expand All @@ -8,7 +11,6 @@ interface CommentProps {
content: string | null | undefined; // TODO: fix type when BE marks this field as non nullable
type: CommentType;
author: User | null | undefined; // TODO: fix type when BE marks this field as non nullable
currentUser: User | undefined;
}

enum CommentType {
Expand All @@ -23,8 +25,9 @@ function Comment({
content,
type,
author,
currentUser,
}: CommentProps) {
const { currentUser } = useAuthContext();

return (
<div
className={`${
Expand Down
5 changes: 1 addition & 4 deletions src/app/[lang]/crag/[cragSlug]/(crag)/comments/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import urqlServer from "@/graphql/urql-server";
import { Crag, CragCommentsDocument } from "@/graphql/generated";
import Comment, { CommentType } from "./components/comment";
import AddCommentForm from "./components/add-comment-form";
import authStatus from "@/utils/auth/auth-status";

interface Params {
cragSlug: string;
Expand All @@ -14,12 +13,11 @@ async function CragComments({ params }: { params: Params }) {
crag: params.cragSlug,
});
const crag = data.cragBySlug as Crag;
const currentUser = (await authStatus())?.user;

return (
<div className="mt-18 px-4 xs:px-8">
<div className="mx-auto max-w-lg">
<AddCommentForm cragId={crag.id} currentUser={currentUser} />
<AddCommentForm cragId={crag.id} />

<div className="mt-18">
{crag.comments.map((comment) => (
Expand All @@ -34,7 +32,6 @@ async function CragComments({ params }: { params: Params }) {
content={comment.content}
type={comment.type as CommentType}
author={comment.user}
currentUser={currentUser}
/>
</div>
))}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { Crag } from "@/graphql/generated";
import { useI18nPathname } from "@/utils/hooks/use-i18n-pathname";
import { useI18nPathname } from "@/hooks/use-i18n-pathname";
import IconComment from "@/components/ui/icons/comment";
import IconGallery from "@/components/ui/icons/gallery";
import IconInfo from "@/components/ui/icons/info";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import useResizeObserver from "@/hooks/useResizeObserver";
import {
LogRoutesProvider,
TLogRoute,
} from "@/components/log-dialog/log-routes-context";
} from "@/components/log-dialog/lib/log-routes-context";
import LogRoutesPopover from "./log-routes-popover";
import dayjs from "dayjs";
import Toast from "@/components/ui/toast";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import IconStarFull from "@/components/ui/icons/star-full";
import IconComment from "@/components/ui/icons/comment";
import IconCheck from "@/components/ui/icons/check";
import { IconSize } from "@/components/ui/icons/icon-size";
import { filterEntitiesBySearchTerm } from "@/utils/search-helpers";
import { filterEntitiesBySearchTerm } from "../../../../../../../lib/search-helpers";

interface Props {
crag: Crag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import IconStarEmpty from "@/components/ui/icons/star-empty";
import IconStarFull from "@/components/ui/icons/star-full";
import Link from "@/components/ui/link";
import { CragRoutesContext } from "../../crag-routes";
import { pluralizeNoun } from "@/utils/text-helpers";
import { pluralizeNoun } from "../../../../../../../../lib/text-helpers";
import RouteGrade from "./crag-route/route-grade";
import Checkbox from "@/components/ui/checkbox";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DifficultyVote, Route } from "@/graphql/generated";
import displayDate from "@/utils/display-date";
import displayDate from "../../../../../../../../../lib/display-date";
import Grade, { diffToGrade } from "@/components/grade";
import { pluralizeNoun } from "@/utils/text-helpers";
import { pluralizeNoun } from "../../../../../../../../../lib/text-helpers";

interface Props {
route: Route;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Crag, DifficultyVote, Route } from "@/graphql/generated";
import Grade from "@/components/grade";
import useIsVisible from "@/hooks/useIsVisible";
import { useEffect, useRef, useState } from "react";
import difficultyVotesAction from "./server-actions/difficulty-votes-action";
import difficultyVotesAction from "./lib/difficulty-votes-action";
import Button from "@/components/ui/button";

interface RouteGradeProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext, useRef } from "react";
import { CragRoutesContext } from "./crag-routes";
import useIsVisible from "@/hooks/useIsVisible";
import { pluralizeNoun } from "@/utils/text-helpers";
import { pluralizeNoun } from "../../../../../../lib/text-helpers";
import Button from "@/components/ui/button";
import LogDialog from "@/components/log-dialog/log-dialog";

Expand Down
49 changes: 28 additions & 21 deletions src/app/[lang]/crag/[cragSlug]/(crag)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {
Crag,
CragSectorsDocument,
MyCragSummaryDocument,
User,
} from "@/graphql/generated";
import urqlServer from "@/graphql/urql-server";
import CragRoutes from "./components/crag-routes";
import authStatus from "@/utils/auth/auth-status";
import tickAscentTypes from "@/utils/constants/tick-ascent-types";
import trTickAscentTypes from "@/utils/constants/tr-tick-ascent-types";
import tickAscentTypes from "../../../../../lib/constants/tick-ascent-types";
import trTickAscentTypes from "../../../../../lib/constants/tr-tick-ascent-types";
import getCurrentUser from "../../../../../lib/auth/get-current-user";

type Params = {
cragSlug: string;
Expand All @@ -19,22 +20,23 @@ type Props = {
params: Params;
};

async function getCragBySlug(crag: string): Promise<Crag> {
const { user: loggedInUser } = await authStatus();

const firstTryArInput = !!loggedInUser
async function getCragBySlug(
crag: string,
currentUser: User | null
): Promise<Crag> {
const firstTryArInput = !!currentUser
? {
pageSize: 1,
pageNumber: 1,
orderBy: {
field: "date",
direction: "ASC",
},
userId: loggedInUser.id,
userId: currentUser.id,
}
: null;

const firstTickArInput = !!loggedInUser
const firstTickArInput = !!currentUser
? {
ascentType: tickAscentTypes,
pageSize: 1,
Expand All @@ -43,11 +45,11 @@ async function getCragBySlug(crag: string): Promise<Crag> {
field: "date",
direction: "ASC",
},
userId: loggedInUser.id,
userId: currentUser.id,
}
: null;

const firstTrTickArInput = !!loggedInUser
const firstTrTickArInput = !!currentUser
? {
ascentType: trTickAscentTypes,
pageSize: 1,
Expand All @@ -56,16 +58,16 @@ async function getCragBySlug(crag: string): Promise<Crag> {
field: "date",
direction: "ASC",
},
userId: loggedInUser.id,
userId: currentUser.id,
}
: null;

const difficultyVotesInput = !!loggedInUser
? { userId: loggedInUser.id }
const difficultyVotesInput = !!currentUser
? { userId: currentUser.id }
: null;

const starRatingVotesInput = !!loggedInUser
? { userId: loggedInUser.id }
const starRatingVotesInput = !!currentUser
? { userId: currentUser.id }
: null;

const {
Expand All @@ -77,14 +79,17 @@ async function getCragBySlug(crag: string): Promise<Crag> {
firstTrTickArInput,
difficultyVotesInput,
starRatingVotesInput,
loggedIn: !!loggedInUser,
loggedIn: !!currentUser,
});

return cragBySlug;
}

async function getMySummary(crag: string): Promise<ActivityRoute[]> {
const { loggedIn } = await authStatus();
async function getMySummary(
crag: string,
currentUser: User | null
): Promise<ActivityRoute[]> {
const loggedIn = !!currentUser;

if (!loggedIn) {
return [];
Expand All @@ -100,9 +105,11 @@ async function getMySummary(crag: string): Promise<ActivityRoute[]> {
}

async function CragPage({ params: { cragSlug } }: Props) {
const currentUser = await getCurrentUser();

const [cragBySlug, myCragSummary] = await Promise.all([
getCragBySlug(cragSlug),
getMySummary(cragSlug),
getCragBySlug(cragSlug, currentUser),
getMySummary(cragSlug, currentUser),
]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ShowMap from "./show-map";
import ToggleFiltersPane from "./toggle-filters-pane";
import SelectColumns from "./select-columns";
import Sort from "./sort";
import { useCragsContext } from "../crags-context";
import { useCragsContext } from "../../lib/crags-context";
import { IconSize } from "@/components/ui/icons/icon-size";

function ActionsRow() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Button from "@/components/ui/button";
import IconColumns from "@/components/ui/icons/columns";
import { Select, Option } from "@/components/ui/select";
import { useCragsContext } from "../crags-context";
import { useCragsContext } from "../../lib/crags-context";

function SelectColumns() {
const { columns } = useCragsContext();
Expand Down
2 changes: 1 addition & 1 deletion src/app/[lang]/crags/components/actions-row/sort.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Button from "@/components/ui/button";
import IconSort from "@/components/ui/icons/sort";
import { Select, Option } from "@/components/ui/select";
import { useCragsContext } from "../crags-context";
import { useCragsContext } from "../../lib/crags-context";

function Sort() {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Button from "@/components/ui/button";
import IconFilter from "@/components/ui/icons/filter";
import { useCragsContext } from "../crags-context";
import { useCragsContext } from "../../lib/crags-context";

function ToggleFiltersPane() {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCragsContext } from "../../crags-context";
import { useCragsContext } from "../../../lib/crags-context";
import FilterChip from "./filter-chip";

function ActiveFilters() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Crag } from "@/graphql/generated";
import CragCard from "./single-crag/crag-card";
import { useCragsContext } from "../crags-context";
import { useCragsContext } from "../../lib/crags-context";

function CragListCards() {
const { crags } = useCragsContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Crag } from "@/graphql/generated";
import CragRow from "./single-crag/crag-row";
import { useCragsContext } from "../crags-context";
import { useCragsContext } from "../../lib/crags-context";

function CragListTable() {
const { crags, columns } = useCragsContext();
Expand Down
2 changes: 1 addition & 1 deletion src/app/[lang]/crags/components/crag-list/crag-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ActiveFilters from "./active-filters-row/active-filters";
import CragListCards from "./crag-list-cards";
import CragListTable from "./crag-list-table";
import Link from "@/components/ui/link";
import { useCragsContext } from "../crags-context";
import { useCragsContext } from "../../lib/crags-context";

function CragList() {
const { crags, columns } = useCragsContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import WallAngles from "./wall-angles";
import GradeFromTo from "./grade-from-to";
import Orientations from "./orientations";
import NrRoutes from "./nr-routes";
import { useCragsContext } from "../../crags-context";
import { useCragsContext } from "../../../lib/crags-context";

type TCragCardProps = {
crag: Crag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import WallAngles from "./wall-angles";
import GradeFromTo from "./grade-from-to";
import Orientations from "./orientations";
import Link from "@/components/ui/link";
import { useCragsContext } from "../../crags-context";
import { useCragsContext } from "../../../lib/crags-context";

type TCragRowProps = {
crag: Crag;
Expand Down
Loading

0 comments on commit 3942e23

Please sign in to comment.