Skip to content

Commit

Permalink
add loggedInOrWorkingOffline variable, make authenticated / workingOf…
Browse files Browse the repository at this point in the history
…fline mutually exclusive
  • Loading branch information
LocalNewsTV authored and plasticviking committed Dec 12, 2024
1 parent 557d10c commit 72386cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 4 additions & 8 deletions app/src/UI/Map2/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export const Map = ({ children }) => {

const [draw, setDraw] = useState(null);
const [mapReady, setMapReady] = useState(false);
const [loggedIn, setLoggedIn] = useState<boolean>(false);
const mapContainer: React.MutableRefObject<HTMLDivElement | null> = useRef<HTMLDivElement>(null);
const map: React.MutableRefObject<MapLibre | null> = useRef<MapLibre>(null);

Expand All @@ -62,7 +61,7 @@ export const Map = ({ children }) => {

// Avoid remounting map to avoid unnecesssary tile fetches or bad umounts:
const authInitiated = useSelector((state) => state.Auth.initialized);
const { authenticated, workingOffline } = useSelector((state) => state.Auth);
const { authenticated, loggedInOrWorkingOffline } = useSelector((state) => state.Auth);
const connectedToNetwork = useSelector((state) => state.Network.connected);

// RecordSet Layers
Expand Down Expand Up @@ -127,9 +126,6 @@ export const Map = ({ children }) => {

const PUBLIC_MAP_URL = useSelector((state) => state.Configuration.current.PUBLIC_MAP_URL);

useEffect(() => {
setLoggedIn(authenticated || workingOffline);
}, [workingOffline, authenticated]);
useEffect(() => {
if (!map.current || mapReady) return;

Expand Down Expand Up @@ -206,7 +202,7 @@ export const Map = ({ children }) => {
refreshColoursOnColourUpdate(storeLayers, map.current);
refreshVisibilityOnToggleUpdate(storeLayers, map.current);
removeDeletedRecordSetLayersOnRecordSetDelete(storeLayers, map.current);
}, [storeLayers, map.current, mapReady, connectedToNetwork, workingOffline]);
}, [storeLayers, map.current, mapReady, connectedToNetwork, loggedInOrWorkingOffline]);

// Layer picker:
useEffect(() => {
Expand Down Expand Up @@ -432,13 +428,13 @@ export const Map = ({ children }) => {
// toggle public map pmtile layer
useEffect(() => {
if (!mapReady) return;
if (loggedIn) {
if (loggedInOrWorkingOffline) {
toggleLayerOnBool(map.current, 'invasivesbc-pmtile-vector', false);
toggleLayerOnBool(map.current, 'iapp-pmtile-vector', false);
toggleLayerOnBool(map.current, 'invasivesbc-pmtile-vector-label', false);
toggleLayerOnBool(map.current, 'iapp-pmtile-vector-label', false);
}
}, [loggedIn, map.current, mapReady]);
}, [loggedInOrWorkingOffline, map.current, mapReady]);

useEffect(() => {
refreshWhatsHereFeature(map.current, { whatsHereFeature });
Expand Down
14 changes: 11 additions & 3 deletions app/src/state/reducers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface AuthState {
offlineUserDialogOpen: boolean;
offlineUsers: OfflineUserState[];
workingOffline: boolean;
loggedInOrWorkingOffline: boolean;

roles: { role_id: number; role_name: string }[];
accessRoles: { role_id: number; role_name: string }[];
Expand Down Expand Up @@ -130,6 +131,7 @@ const initialState: AuthState = {
offlineUserDialogOpen: false,
offlineUsers: [],
workingOffline: false,
loggedInOrWorkingOffline: false,
extendedInfo: {
account_status: 0,
activation_status: 0,
Expand All @@ -152,16 +154,17 @@ const initialState: AuthState = {

function loadCurrentStateFromIdToken(idToken): object {
let displayName = 'User';
let username = null;
let username: string | null = null;
let email = '';
let bceid_userid = '';
let idir_userid = '';
let bceid_user_guid = '';
let idir_user_guid = '';

let loggedInOrWorkingOffline = false;
const authenticated = !!idToken;

if (authenticated) {
loggedInOrWorkingOffline = true;
try {
const parsedToken = JSON.parse(
decodeURIComponent(
Expand Down Expand Up @@ -203,7 +206,8 @@ function loadCurrentStateFromIdToken(idToken): object {
idir_userid,
bceid_userid,
idir_user_guid,
bceid_user_guid
bceid_user_guid,
loggedInOrWorkingOffline
};
}

Expand All @@ -222,6 +226,7 @@ function createAuthReducer(configuration: AppConfig): (AuthState, AnyAction) =>
draftState.displayName = null;
draftState.email = null;
draftState.username = 'loggedOut';
draftState.loggedInOrWorkingOffline = false;
break;
}
case AUTH_SAVE_CURRENT_TO_OFFLINE: {
Expand Down Expand Up @@ -281,6 +286,7 @@ function createAuthReducer(configuration: AppConfig): (AuthState, AnyAction) =>
draftState.username = found.username;
draftState.displayName = found.displayName;
draftState.workingOffline = true;
draftState.loggedInOrWorkingOffline = true;
break;
}
case AUTH_INITIALIZE_COMPLETE: {
Expand All @@ -290,6 +296,8 @@ function createAuthReducer(configuration: AppConfig): (AuthState, AnyAction) =>
Object.keys(loadCurrentStateFromIdToken(currentIdToken)).forEach((key) => {
draftState[key] = loadCurrentStateFromIdToken(currentIdToken)[key];
});
draftState.workingOffline = draftState.authenticated ? false : draftState.workingOffline;

break;
}
case AUTH_REQUEST_COMPLETE: {
Expand Down

0 comments on commit 72386cc

Please sign in to comment.