diff --git a/src/app/closing/detail/page.tsx b/src/app/closing/detail/page.tsx
index 487533be..1b4ebc45 100644
--- a/src/app/closing/detail/page.tsx
+++ b/src/app/closing/detail/page.tsx
@@ -4,7 +4,6 @@ import { useRouter, useSearchParams } from 'next/navigation';
import useSWR from 'swr';
import ClosingDetail from '@/components/pages/closing/ClosingDetail';
-import SalesReportTable from '@/components/pages/closing/sale/SalesReportTable';
import { ClosingApi } from '@/services/api/closing';
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
@@ -20,9 +19,9 @@ const ClosingDetailPage = () => {
(id: number) => ClosingApi.getGeneralInfo(id)
);
- const { data: salesReport, isLoading: isLoadingSalesReport } = useSWR(
- closingId,
- (id: number) => ClosingApi.getPenjualan(id)
+ const { data: salesData, isLoading: isLoadingSales } = useSWR(
+ closingId ? `sales-${closingId}` : null,
+ () => ClosingApi.getPenjualan(Number(closingId))
);
if (!closingId) {
@@ -40,17 +39,18 @@ const ClosingDetailPage = () => {
return;
}
+ const isLoading = isLoadingClosing || isLoadingSales;
+
return (
- {isLoadingClosing && (
-
- )}
+ {isLoading && }
- {!isLoadingClosing && isResponseSuccess(closing) && (
-
- )}
- {!isLoadingSalesReport && isResponseSuccess(salesReport) && (
-
+ {!isLoading && isResponseSuccess(closing) && (
+
)}
);
diff --git a/src/components/helper/RequireAuth.tsx b/src/components/helper/RequireAuth.tsx
index 119d74cb..53853b96 100644
--- a/src/components/helper/RequireAuth.tsx
+++ b/src/components/helper/RequireAuth.tsx
@@ -1,54 +1,46 @@
'use client';
import { ReactNode, useEffect } from 'react';
-import { useRouter } from 'next/navigation';
-import useSWRImmutable from 'swr/immutable';
+import useSWR from 'swr';
import { useAuth } from '@/services/hooks/useAuth';
import { httpClientFetcher, SWRHttpKey } from '@/services/http/client';
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
import { BaseApiResponse, GetMeResponse } from '@/types/api/api-general';
import { AxiosError } from 'axios';
+import { redirectToSSO } from '@/lib/auth-helper';
interface RequireAuthProps {
children?: ReactNode;
}
const RequireAuth = ({ children }: RequireAuthProps) => {
- const router = useRouter();
const { setUser, setIsLoadingUser } = useAuth();
const {
data: userResponse,
isLoading: isLoadingUserResponse,
error: userErrorResponse,
- } = useSWRImmutable<
+ } = useSWR<
GetMeResponse & { ok?: boolean },
AxiosError,
SWRHttpKey
>('/sso/userinfo', httpClientFetcher, {
shouldRetryOnError: false,
- revalidateOnFocus: false,
- revalidateOnReconnect: false,
- refreshInterval: 0,
});
- useEffect(() => {
- setIsLoadingUser(isLoadingUserResponse);
- }, [isLoadingUserResponse, setIsLoadingUser]);
-
useEffect(() => {
if (isResponseSuccess(userResponse)) {
setUser(userResponse.data);
- } else if (
- isResponseError(userErrorResponse?.response?.data) &&
- typeof window !== 'undefined'
- ) {
- router.replace(
- `${process.env.NEXT_PUBLIC_SSO_LOGIN_URL as string}?redirect_url=${window.location.href}`
- );
}
- }, [userResponse, userErrorResponse, setIsLoadingUser, setUser]);
+ }, [userResponse, setUser]);
+
+ // Explicitly handle 401 redirect from the component level
+ useEffect(() => {
+ if (userErrorResponse?.response?.status === 401) {
+ redirectToSSO();
+ }
+ }, [userErrorResponse]);
if (isLoadingUserResponse && !userResponse && !userErrorResponse) {
return (
@@ -58,6 +50,24 @@ const RequireAuth = ({ children }: RequireAuthProps) => {
);
}
+ if (userErrorResponse) {
+ return (
+
+
Authentication Failed
+
+ Please try refreshing the page or contact support if the problem
+ persists.
+
+
+
+ );
+ }
+
return <>{isResponseSuccess(userResponse) && children}>;
};
diff --git a/src/components/pages/closing/ClosingDetail.tsx b/src/components/pages/closing/ClosingDetail.tsx
index 147b3fbd..11e28e32 100644
--- a/src/components/pages/closing/ClosingDetail.tsx
+++ b/src/components/pages/closing/ClosingDetail.tsx
@@ -7,15 +7,24 @@ import Button from '@/components/Button';
import Tabs from '@/components/Tabs';
import ClosingGeneralInformationTable from '@/components/pages/closing/ClosingGeneralInformationTable';
-import { ClosingGeneralInformation } from '@/types/api/closing';
+import {
+ ClosingGeneralInformation,
+ BaseClosingSales,
+} from '@/types/api/closing';
import ClosingSapronakTabContent from './ClosingSapronakTabContent';
+import SalesReportTable from './sale/SalesReportTable';
interface ClosingDetailProps {
id: number;
initialValue?: ClosingGeneralInformation;
+ salesData?: BaseClosingSales;
}
-const ClosingDetail: React.FC = ({ id, initialValue }) => {
+const ClosingDetail: React.FC = ({
+ id,
+ initialValue,
+ salesData,
+}) => {
const [activeTab, setActiveTab] = useState('sapronak');
const closingDetailTabs = useMemo(() => {
@@ -33,7 +42,7 @@ const ClosingDetail: React.FC = ({ id, initialValue }) => {
{
id: 'penjualan',
label: 'Penjualan',
- content: 'Penjualan',
+ content: ,
},
{
id: 'overhead',
diff --git a/src/components/pages/closing/sale/SalesReportTable.tsx b/src/components/pages/closing/sale/SalesReportTable.tsx
index e509eb7d..89cb6615 100644
--- a/src/components/pages/closing/sale/SalesReportTable.tsx
+++ b/src/components/pages/closing/sale/SalesReportTable.tsx
@@ -263,7 +263,7 @@ const SalesReportTable = ({
tableWrapperClassName: 'overflow-x-auto',
tableClassName: 'w-full table-auto text-sm',
headerColumnClassName:
- 'px-4 py-3 text-xs font-semibold text-gray-500 last:flex last:flex-row last:justify-end whitespace-nowrap border-l border-l-gray-200 border-r border-r-gray-200 border-t border-t-gray-200 border-gray-200 border-b-0',
+ 'px-4 py-3 text-xs font-semibold text-gray-500 whitespace-nowrap border-l border-l-gray-200 border-r border-r-gray-200 border-t border-t-gray-200 border-gray-200 border-b-0',
bodyRowClassName:
'hover:bg-gray-50 transition-colors border-b border-gray-200 first:border-t first:border-t-gray-200 border-l border-l-gray-200 border-r border-r-gray-200',
bodyColumnClassName:
diff --git a/src/lib/auth-helper.ts b/src/lib/auth-helper.ts
new file mode 100644
index 00000000..97d31a9f
--- /dev/null
+++ b/src/lib/auth-helper.ts
@@ -0,0 +1,25 @@
+/**
+ * Redirects the user to the SSO login page with loop protection.
+ *
+ * This function checks a session storage timestamp to ensure that redirects
+ * do not happen too frequently (blocking infinite redirect loops).
+ */
+export const redirectToSSO = () => {
+ if (typeof window === 'undefined') return;
+
+ const lastRedirect = sessionStorage.getItem('auth_redirect_timestamp');
+ const now = Date.now();
+
+ // Loop protection: allow redirect only if last one was > 2 seconds ago
+ // or if no redirect has happened yet.
+ if (!lastRedirect || now - parseInt(lastRedirect, 10) > 2000) {
+ sessionStorage.setItem('auth_redirect_timestamp', now.toString());
+ // const ssoLoginUrl = `${process.env.NEXT_PUBLIC_SSO_LOGIN_URL as string}?redirect_url=${window.location.href}`;
+
+ const ltiSsoStart = `${process.env.NEXT_PUBLIC_API_BASE_URL as string}/sso/start?client_id=${process.env.NEXT_PUBLIC_CLIENT_ID as string}&redirect_url=${window.location.href}`;
+ const ssoLoginUrl = `${process.env.NEXT_PUBLIC_SSO_LOGIN_URL as string}?redirect_url=${ltiSsoStart}`;
+ window.location.href = ssoLoginUrl;
+ } else {
+ console.error('Redirect loop detected. Aborting redirect.');
+ }
+};
diff --git a/src/services/api/closing.ts b/src/services/api/closing.ts
index fe2c2d50..6ce32995 100644
--- a/src/services/api/closing.ts
+++ b/src/services/api/closing.ts
@@ -20,10 +20,11 @@ export class ClosingApiService extends BaseApiService {
id: number
): Promise | undefined> {
try {
- const getPenjualanPath = `${id}/penjualan`;
- return await this.customRequest>(
- getPenjualanPath
- );
+ const getPenjualanPath = `${this.basePath}/${id}/penjualan`;
+ const getPenjualanRes =
+ await httpClient>(getPenjualanPath);
+
+ return getPenjualanRes;
} catch (error) {
if (axios.isAxiosError>(error)) {
return error.response?.data;
diff --git a/src/services/http/client.ts b/src/services/http/client.ts
index f9389a16..68b5282a 100644
--- a/src/services/http/client.ts
+++ b/src/services/http/client.ts
@@ -2,6 +2,8 @@ import axios from 'axios';
import type { AxiosError, AxiosRequestConfig } from 'axios';
import { RequestOptions } from '@/services/http/base';
+import { redirectToSSO } from '@/lib/auth-helper';
+
const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL ?? '';
const axiosClient = axios.create({ baseURL: BASE_URL, timeout: 10_000 });
@@ -9,8 +11,7 @@ axiosClient.interceptors.response.use(
(response) => response,
(error: AxiosError) => {
if (error.response?.status === 401) {
- const ssoLoginUrl = `${process.env.NEXT_PUBLIC_SSO_LOGIN_URL as string}?redirect_url=${window.location.href}`;
- window.location.href = ssoLoginUrl;
+ redirectToSSO();
}
return Promise.reject(error);