-
-
Notifications
You must be signed in to change notification settings - Fork 864
/
Copy pathLeftDrawerOrg.tsx
211 lines (198 loc) · 6.97 KB
/
LeftDrawerOrg.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { useQuery } from '@apollo/client';
import { WarningAmberOutlined } from '@mui/icons-material';
import { ORGANIZATIONS_LIST } from 'GraphQl/Queries/Queries';
import CollapsibleDropdown from 'components/CollapsibleDropdown/CollapsibleDropdown';
import IconComponent from 'components/IconComponent/IconComponent';
import React, { useEffect, useMemo, useState } from 'react';
import Button from 'react-bootstrap/Button';
import { useTranslation } from 'react-i18next';
import { NavLink, useLocation } from 'react-router-dom';
import type { TargetsType } from 'state/reducers/routesReducer';
import type { InterfaceQueryOrganizationsListObject } from 'utils/interfaces';
import AngleRightIcon from 'assets/svgs/angleRight.svg?react';
import TalawaLogo from 'assets/svgs/talawa.svg?react';
import styles from './../../style/app.module.css'; // Import the global CSS file
import Avatar from 'components/Avatar/Avatar';
import useLocalStorage from 'utils/useLocalstorage';
export interface InterfaceLeftDrawerProps {
orgId: string;
targets: TargetsType[];
hideDrawer: boolean | null;
setHideDrawer: React.Dispatch<React.SetStateAction<boolean | null>>;
}
/**
* LeftDrawerOrg component for displaying organization details and navigation options.
*
* @param orgId - ID of the current organization.
* @param targets - List of navigation targets.
* @param hideDrawer - Determines if the drawer should be hidden or shown.
* @param setHideDrawer - Function to update the visibility state of the drawer.
* @returns JSX element for the left navigation drawer with organization details.
*/
const leftDrawerOrg = ({
targets,
orgId,
hideDrawer,
setHideDrawer,
}: InterfaceLeftDrawerProps): JSX.Element => {
const { t: tCommon } = useTranslation('common');
const { t: tErrors } = useTranslation('errors');
const location = useLocation();
const { getItem } = useLocalStorage();
const userId = getItem('id');
const getIdFromPath = (pathname: string): string => {
if (!pathname) return '';
const segments = pathname.split('/');
// Index 2 (third segment) represents the ID in paths like /member/{userId}
return segments.length > 2 ? segments[2] : '';
};
const [isProfilePage, setIsProfilePage] = useState(false);
const [showDropdown, setShowDropdown] = useState(false);
const [organization, setOrganization] = useState<
InterfaceQueryOrganizationsListObject | undefined
>(undefined);
const {
data,
loading,
}: {
data:
| { organizations: InterfaceQueryOrganizationsListObject[] }
| undefined;
loading: boolean;
} = useQuery(ORGANIZATIONS_LIST, {
variables: { id: orgId },
});
// Get the ID from the current path
const pathId = useMemo(
() => getIdFromPath(location.pathname),
[location.pathname],
);
// Check if the current page is admin profile page
useEffect(() => {
// if param id is equal to userId, then it is a profile page
setIsProfilePage(pathId === userId);
}, [location, userId]);
// Set organization data when query data is available
useEffect(() => {
let isMounted = true;
if (data && isMounted) {
setOrganization(data?.organizations[0]);
} else {
setOrganization(undefined);
}
return () => {
isMounted = false;
};
}, [data]);
/**
* Handles link click to hide the drawer on smaller screens.
*/
const handleLinkClick = (): void => {
if (window.innerWidth <= 820) {
setHideDrawer(true);
}
};
return (
<>
<div
className={`${styles.leftDrawer} ${
hideDrawer === null
? styles.hideElemByDefault
: hideDrawer
? styles.inactiveDrawer
: styles.activeDrawer
}`}
data-testid="leftDrawerContainer"
>
{/* Branding Section */}
<div className={styles.brandingContainer}>
<TalawaLogo className={styles.talawaLogo} />
<span className={styles.talawaText}>
{tCommon('talawaAdminPortal')}
</span>
</div>
{/* Organization Section */}
<div className={`${styles.organizationContainer} pe-3`}>
{loading ? (
<button
className={`${styles.profileContainer} shimmer`}
data-testid="orgBtn"
/>
) : organization == undefined ? (
!isProfilePage && (
<button
className={`${styles.profileContainer} ${styles.bgDanger} text-start text-white`}
disabled
>
<div className="px-3">
<WarningAmberOutlined />
</div>
{tErrors('errorLoading', { entity: 'Organization' })}
</button>
)
) : (
<button className={styles.profileContainer} data-testid="OrgBtn">
<div className={styles.imageContainer}>
{organization.image ? (
<img src={organization.image} alt={`profile picture`} />
) : (
<Avatar
name={organization.name}
containerStyle={styles.avatarContainer}
alt={'Dummy Organization Picture'}
/>
)}
</div>
<div className={styles.profileText}>
<span className={styles.primaryText}>{organization.name}</span>
<span className={styles.secondaryText}>
{organization.address.city}
</span>
</div>
<AngleRightIcon fill={'var(--bs-secondary)'} />
</button>
)}
</div>
{/* Options List */}
<h5 className={`${styles.titleHeader} text-secondary`}>
{tCommon('menu')}
</h5>
<div className={styles.optionList}>
{targets.map(({ name, url }, index) => {
return url ? (
<NavLink to={url} key={name} onClick={handleLinkClick}>
{({ isActive }) => (
<Button
key={name}
variant={isActive ? 'success' : ''}
className={
isActive ? styles.activeButton : styles.inactiveButton
}
>
<div className={styles.iconWrapper}>
<IconComponent
name={name == 'Membership Requests' ? 'Requests' : name}
fill={
isActive ? 'var(--bs-black)' : 'var(--bs-secondary)'
}
/>
</div>
{tCommon(name)}
</Button>
)}
</NavLink>
) : (
<CollapsibleDropdown
key={name}
target={targets[index]}
showDropdown={showDropdown}
setShowDropdown={setShowDropdown}
/>
);
})}
</div>
</div>
</>
);
};
export default leftDrawerOrg;