Skip to content

Commit

Permalink
fix language passed as access token
Browse files Browse the repository at this point in the history
  • Loading branch information
kguzek committed Jan 5, 2025
1 parent 93bd71a commit 7b68d0b
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/app/admin/users/[uuid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ErrorComponent } from "@/components/error-component";
import type { User } from "@/lib/types";
import { getTitle } from "@/lib/util";
import { serverToApi } from "@/lib/backend/server";
import { getAccessToken, serverToApi } from "@/lib/backend/server";
import { useTranslations } from "@/providers/translation-provider";
import { UserEditor } from "./user-editor";
import { ErrorCode } from "@/lib/enums";

interface Props {
params: Promise<{ uuid: string }>;
Expand All @@ -16,11 +17,21 @@ export async function generateMetadata(): Promise<{ title: string }> {

export default async function UserPage({ params }: Props) {
const { userLanguage } = await useTranslations();
const accessToken = await getAccessToken();
if (!accessToken) {
return <ErrorComponent errorCode={ErrorCode.Unauthorized} />;
}
const { uuid } = await params;
const result = await serverToApi<User>(`auth/users/${uuid}`);
if (!result.ok) {
return <ErrorComponent errorResult={result} />;
}

return <UserEditor user={result.data} userLanguage={userLanguage} />;
return (
<UserEditor
user={result.data}
userLanguage={userLanguage}
accessToken={accessToken}
/>
);
}
8 changes: 6 additions & 2 deletions src/app/admin/users/[uuid]/user-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import { clientToApi } from "@/lib/backend/client";
export function UserEditor({
user: originalUser,
userLanguage,
accessToken,
}: {
user: User;
userLanguage: Language;
accessToken: string;
}) {
const [user, setUser] = useState(originalUser);
const [username, setUsername] = useState(user.username);
const [email, setEmail] = useState(user.email);
const [admin, setAdmin] = useState(user.admin);
const [loading, setLoading] = useState(false);
const [password, setPassword] = useState("");
const { setModalInfo } = useModals();
const { setModalError, setModalInfo } = useModals();
const data = TRANSLATIONS[userLanguage];

const haveDetailsChanged = () =>
Expand All @@ -49,10 +51,12 @@ export function UserEditor({
setLoading(true);
const result = await clientToApi(
`auth/users/${user.uuid}/${section}`,
userLanguage,
accessToken,
{
method: "PUT",
body,
userLanguage,
setModalError,
},
);
if (result.ok) {
Expand Down
8 changes: 7 additions & 1 deletion src/app/admin/users/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Metadata } from "next";
import type { User } from "@/lib/types";
import { useTranslations } from "@/providers/translation-provider";
import { serverToApi } from "@/lib/backend/server";
import { getAccessToken, serverToApi } from "@/lib/backend/server";
import { ErrorComponent } from "@/components/error-component";
import { ErrorCode } from "@/lib/enums";
import { UserCard } from "./user-card";
Expand All @@ -15,6 +15,11 @@ export async function generateMetadata(): Promise<Metadata> {

export default async function Users() {
const { data, userLanguage } = await useTranslations();
const accessToken = await getAccessToken();

if (!accessToken) {
return <ErrorComponent errorCode={ErrorCode.Unauthorized} />;
}

const usersResult = await serverToApi<User[]>("auth/users");
if (!usersResult.ok) {
Expand All @@ -31,6 +36,7 @@ export default async function Users() {
key={`user-${user.uuid || key}`}
user={user}
userLanguage={userLanguage}
accessToken={accessToken}
/>
))}
</div>
Expand Down
8 changes: 6 additions & 2 deletions src/app/admin/users/user-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { useState } from "react";
export function UserCard({
user,
userLanguage,
accessToken,
}: {
user: User;
userLanguage: Language;
accessToken: string;
}) {
const { setModalChoice, setModalInfo } = useModals();
const { setModalChoice, setModalError, setModalInfo } = useModals();
const [isDeleted, setIsDeleted] = useState(false);

const data = TRANSLATIONS[userLanguage];
Expand All @@ -27,8 +29,10 @@ export function UserCard({
}

async function deleteUser(user: User) {
const res = await clientToApi(`auth/users/${user.uuid}`, userLanguage, {
const res = await clientToApi(`auth/users/${user.uuid}`, accessToken, {
method: "DELETE",
userLanguage,
setModalError,
});
if (res.ok) {
setModalInfo(data.admin.users.deleted(user.username));
Expand Down
2 changes: 2 additions & 0 deletions src/app/liveseries/tv-show/[permalink]/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export function ShowDetails({
{
method: "PUT",
body: episodeNumbers,
userLanguage,
setModalError,
},
);
if (!result.ok) {
Expand Down
2 changes: 2 additions & 0 deletions src/app/login/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function LogInForm({ userLanguage }: { userLanguage: Language }) {
result = await clientToApi("auth/tokens", "", {
method: "POST",
body,
userLanguage,
setModalError,
});
setLoading(false);
if (result.ok) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/liveseries/downloads-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function DownloadsWidget({
(episode) => episode.status === DownloadStatus.PENDING,
) == null,
);
const { setModalInfo, setModalChoice } = useModals();
const { setModalError, setModalChoice, setModalInfo } = useModals();
const data = TRANSLATIONS[userLanguage];
const serialise = data.liveSeries.episodes.serialise;

Expand All @@ -39,7 +39,7 @@ export default function DownloadsWidget({
const result = await clientToApi(
`liveseries/downloaded-episodes/${episode.showName}/${episode.season}/${episode.episode}`,
accessToken,
{ method: "DELETE", user },
{ method: "DELETE", user, userLanguage, setModalError },
);
if (result.ok) {
setModalInfo(data.liveSeries.episodes.deleted(episodeString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export function EpisodeDownloadIndicator({
season: episode.season,
},
user,
userLanguage,
setModalError,
},
);
if (result.ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export function EpisodeWatchedIndicator({
{
method: "PUT",
body: episodes,
userLanguage,
setModalError,
},
);
if (result.ok) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/liveseries/tv-show-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export function TvShowPreview({
accessToken,
{
method: isLiked ? "DELETE" : "POST",
userLanguage,
setModalError,
},
);
if (!result.ok) {
Expand Down
8 changes: 5 additions & 3 deletions src/lib/backend/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import type { Language } from "../enums";
import type { User } from "../types";
import { TRANSLATIONS } from "../translations";

type FetchOptionsExtension =
| { userLanguage: Language; setModalError: (error: string) => void }
| { userLanguage?: never; setModalError?: never };
type FetchOptionsExtension = {
userLanguage: Language;
setModalError: (error: string) => void;
};
// | { userLanguage?: never; setModalError?: never };

/** Makes a client-to-server API call using the provided access token.
*
Expand Down

0 comments on commit 7b68d0b

Please sign in to comment.