Quellcode durchsuchen

Implemented modal for unauthorized popup

jnfrati vor 3 Jahren
Ursprung
Commit
ccb711eef4

+ 43 - 4
dashboard/src/shared/auth/UnauthorizedPopup.tsx

@@ -1,5 +1,6 @@
 import { Collapse, Snackbar } from "@material-ui/core";
 import { Collapse, Snackbar } from "@material-ui/core";
 import { Alert } from "@material-ui/lab";
 import { Alert } from "@material-ui/lab";
+import Modal from "main/home/modals/Modal";
 import React from "react";
 import React from "react";
 import styled from "styled-components";
 import styled from "styled-components";
 import { proxy, useSnapshot } from "valtio";
 import { proxy, useSnapshot } from "valtio";
@@ -17,6 +18,7 @@ const actions = {
   },
   },
   hideUnauthorizedPopup: () => {
   hideUnauthorizedPopup: () => {
     state.showUnauthorizedPopup = false;
     state.showUnauthorizedPopup = false;
+    state.expanded = false;
     state.message = "";
     state.message = "";
   },
   },
   toggleExpanded: () => {
   toggleExpanded: () => {
@@ -29,13 +31,25 @@ export const UnauthorizedPopupActions = actions;
 export const UnauthorizedPopup = () => {
 export const UnauthorizedPopup = () => {
   const { showUnauthorizedPopup, message, expanded } = useSnapshot(state);
   const { showUnauthorizedPopup, message, expanded } = useSnapshot(state);
 
 
-  if (expanded) {
-    // TODO: Implement expanded view. Should be a modal with the full message and contact information.
+  if (!showUnauthorizedPopup) {
     return null;
     return null;
   }
   }
 
 
-  if (!showUnauthorizedPopup) {
-    return null;
+  if (expanded) {
+    // TODO: Implement expanded view. Should be a modal with the full message and contact information.
+    return (
+      <Modal onRequestClose={actions.toggleExpanded} height="fit-content">
+        <ModalContentWrapper>
+          <ModalTitle>Unauthorized</ModalTitle>
+          <ModalMessage>{message}</ModalMessage>
+          <ModalContact>
+            Please contact your administrator. If you think this is a mistake,
+            please contact us at{" "}
+            <a href="mailto:contact@getporter.dev">contact@getporter.dev</a>
+          </ModalContact>
+        </ModalContentWrapper>
+      </Modal>
+    );
   }
   }
 
 
   return (
   return (
@@ -82,3 +96,28 @@ const ActionButton = styled.button`
     font-size: 18px;
     font-size: 18px;
   }
   }
 `;
 `;
+
+const ModalContentWrapper = styled.div`
+  display: flex;
+  flex-direction: column;
+  align-items: center;
+  justify-content: center;
+  padding: 20px;
+`;
+
+const ModalTitle = styled.div`
+  font-size: 18px;
+  font-weight: 500;
+  margin-bottom: 10px;
+`;
+
+const ModalMessage = styled.div`
+  font-size: 14px;
+  margin-bottom: 10px;
+`;
+
+const ModalContact = styled.div`
+  font-size: 14px;
+  margin-bottom: 10px;
+  text-align: center;
+`;

+ 6 - 2
dashboard/src/shared/auth/useAuth.ts

@@ -23,13 +23,17 @@ const useAuth = () => {
       scope: ScopeType,
       scope: ScopeType,
       resource: string | string[],
       resource: string | string[],
       verb: Verbs | Array<Verbs>
       verb: Verbs | Array<Verbs>
-    ) =>
+    ) => {
+      if (!authContext || !authContext.currentPolicy) {
+        return false;
+      }
       // We iterate over all the policies in search for at least one policy that will authorize
       // We iterate over all the policies in search for at least one policy that will authorize
       // the user to perform an action.
       // the user to perform an action.
       authContext.currentPolicy.reduce(
       authContext.currentPolicy.reduce(
         isAuthorizedReducer(scope, resource, verb),
         isAuthorizedReducer(scope, resource, verb),
         false
         false
-      ),
+      );
+    },
     [authContext.currentPolicy]
     [authContext.currentPolicy]
   );
   );
 
 

+ 3 - 2
dashboard/src/shared/baseApi.ts

@@ -88,9 +88,10 @@ const apiQueryBuilder = <ParamsType extends {}, PathParamsType = {}>(
   pathParams: PathParamsType
   pathParams: PathParamsType
 ) => {
 ) => {
   try {
   try {
-    return axios(
+    const res = ((await axios(
       buildAxiosConfig(method, endpoint, token, params, pathParams)
       buildAxiosConfig(method, endpoint, token, params, pathParams)
-    ) as AxiosPromise<ResponseType>;
+    )) as unknown) as AxiosPromise<ResponseType>;
+    return res;
   } catch (error) {
   } catch (error) {
     const axiosError = error as AxiosError;
     const axiosError = error as AxiosError;