disable auto language detection;

disable languuage switch for now;
Tweak some error logs and messages
This commit is contained in:
Dobromir Popov
2024-04-30 02:51:37 +03:00
parent aa766f4e1e
commit c98b018bb1
7 changed files with 66 additions and 32 deletions

View File

@@ -39,7 +39,7 @@ const LanguageSwitcher = () => {
open={Boolean(anchorEl)}
onClose={handleClose}
>
{locales.map((lng) => {
{locales?.map((lng) => {
if (lng === locale) return null;
return (
<MenuItem key={lng} onClick={() => changeLanguage(lng)}>

View File

@@ -10,9 +10,10 @@ interface ProtectedRouteProps {
allowedRoles: UserRole[];
deniedMessage?: string;
bypass?: boolean;
autoRedirect?: boolean;
}
const ProtectedRoute = ({ children, allowedRoles, deniedMessage, bypass = false }: ProtectedRouteProps) => {
const ProtectedRoute = ({ children, allowedRoles, deniedMessage, bypass = false, autoRedirect = false }: ProtectedRouteProps) => {
const { data: session, status } = useSession()
const router = useRouter();
@@ -20,10 +21,9 @@ const ProtectedRoute = ({ children, allowedRoles, deniedMessage, bypass = false
console.log("session.role:" + session?.user?.role);
if (!status || status === "unauthenticated") {
// Redirect to the sign-in page
if (!bypass) {
if (autoRedirect) {
signIn();
}
return null;
}
else {
console.log("session.role:" + session?.user?.role);
@@ -41,14 +41,27 @@ const ProtectedRoute = ({ children, allowedRoles, deniedMessage, bypass = false
if (deniedMessage !== undefined) {
return <div>{deniedMessage}</div>;
}
return <div>Нямате достъп до тази страница. Ако мислите, че това е грешка, моля, свържете се с администраторите</div>;
return (
<>
<div className="flex items-center justify-center min-h-screen">
<div className="text-center">
<h1 className="text-2xl font-bold mb-4 text-blue-500">{session?.user?.email},</h1>
<p className="mb-6">{`Нямате достъп до тази страница. Ако мислите, че това е грешка, моля, свържете се с администраторите`}</p>
</div>
</div>
</>);
}
if (status === "loading") {
return <div>Зареждане...</div>;
}
if (!session) return <a href="/api/auth/signin">Защитено съдържание. Впишете се.. </a>
return children;
if (!session) {
if (deniedMessage !== undefined) {
return <div>{deniedMessage}</div>;
}
return <a href="/api/auth/signin">Защитено съдържание. Впишете се.. </a>
}
};
export default ProtectedRoute;

View File

@@ -105,19 +105,22 @@ export default function Sidebar({ isSidebarOpen, toggleSidebar }) {
useEffect(() => {
const fetchLocations = async () => {
try {
const response = await axiosInstance.get('/api/data/locations'); // Adjust the API endpoint as needed
const locationsData = response.data
.filter(location => location.isActive === true)
.map(location => ({
text: location.name,
url: `/cart/locations/${location.id}`,
}));
// Find the "Locations" menu item and populate its children with locationsData
const menuIndex = sidemenu.findIndex(item => item.id === "locations");
if (menuIndex !== -1) {
sidemenu[menuIndex].children = locationsData;
if (session) { // Don't fetch locations if the user is not authenticated
const response = await axiosInstance.get('/api/data/locations'); // Adjust the API endpoint as needed
const locationsData = response.data
.filter(location => location.isActive === true)
.map(location => ({
text: location.name,
url: `/cart/locations/${location.id}`,
}));
// Find the "Locations" menu item and populate its children with locationsData
const menuIndex = sidemenu.findIndex(item => item.id === "locations");
if (menuIndex !== -1) {
sidemenu[menuIndex].children = locationsData;
}
//setLocations(locationsData); // Optional, if you need to use locations elsewhere
}
//setLocations(locationsData); // Optional, if you need to use locations elsewhere
} catch (error) {
console.error("Error fetching locations:", error);
}
@@ -174,16 +177,17 @@ function UserSection({ session }) {
}
function SignInButton() {
const t = useTranslations('common');
// const t = useTranslations('common');
return (
<div className="items-center py-2 font-bold" onClick={() => signIn()}>
<button>{t('login')}</button>
<button>вход</button>
{/* <button>{t('login')}</button> */}
</div>
);
}
function UserDetails({ session }) {
const t = useTranslations('common');
// const t = useTranslations('common');
return (
<>
<hr className="m-0" />
@@ -194,7 +198,10 @@ function UserDetails({ session }) {
<div className="ml-3 overflow-hidden">
<p className="mx-1 mt-1 text-sm font-medium text-gray-800 dark:text-gray-200">{session.user.name}</p>
<p className="mx-1 mt-1 text-sm font-medium text-gray-600 dark:text-gray-400">{session.user.role}</p>
<a href="/api/auth/signout" className={styles.button} onClick={(e) => { e.preventDefault(); signOut(); }}>{t('logout')}</a>
<a href="/api/auth/signout" className={styles.button} onClick={(e) => { e.preventDefault(); signOut(); }}>
{/* {t('logout')} */}
изход
</a>
</div>
</div>
</>