|
| 1 | +import { |
| 2 | + useState, useEffect, useRef, useMemo, |
| 3 | +} from 'react'; |
| 4 | +import { |
| 5 | + Form, Spinner, Dropdown, Icon, Badge, |
| 6 | +} from '@openedx/paragon'; |
| 7 | +import { |
| 8 | + Search, FilterList, ExpandLess, ExpandMore, |
| 9 | +} from '@openedx/paragon/icons'; |
| 10 | +import { useScopes, useOrganizations } from '../data/hooks'; |
| 11 | +import { courseRolesMetadata, libraryRolesMetadata } from '../constants'; |
| 12 | +import { ScopeItem } from '../data/api'; |
| 13 | + |
| 14 | +const allRolesMetadata = [...courseRolesMetadata, ...libraryRolesMetadata]; |
| 15 | + |
| 16 | +function getContextType(role: string | null): string | undefined { |
| 17 | + if (!role) { return undefined; } |
| 18 | + return allRolesMetadata.find((r) => r.role === role)?.contextType; |
| 19 | +} |
| 20 | + |
| 21 | +function getContextLabel(contextType: string | undefined): string { |
| 22 | + if (contextType === 'course') { return 'Courses'; } |
| 23 | + if (contextType === 'library') { return 'Libraries'; } |
| 24 | + return 'Items'; |
| 25 | +} |
| 26 | + |
| 27 | +interface ScopeCheckboxItemProps { |
| 28 | + scope: ScopeItem; |
| 29 | + checked: boolean; |
| 30 | + onToggle: (scopeId: string) => void; |
| 31 | +} |
| 32 | + |
| 33 | +const ScopeCheckboxItem = ({ scope, checked, onToggle }: ScopeCheckboxItemProps) => ( |
| 34 | + <div className="d-flex align-items-start mb-2" style={{ gap: '8px' }}> |
| 35 | + <input |
| 36 | + type="checkbox" |
| 37 | + id={`scope-${scope.id}`} |
| 38 | + checked={checked} |
| 39 | + onChange={() => onToggle(scope.id)} |
| 40 | + style={{ |
| 41 | + width: '16px', height: '16px', marginTop: '2px', flexShrink: 0, cursor: 'pointer', |
| 42 | + }} |
| 43 | + /> |
| 44 | + <label htmlFor={`scope-${scope.id}`} className="mb-0" style={{ cursor: 'pointer' }}> |
| 45 | + <span>{scope.name}</span> |
| 46 | + {scope.description && ( |
| 47 | + <small className="d-block text-muted">{scope.description}</small> |
| 48 | + )} |
| 49 | + </label> |
| 50 | + </div> |
| 51 | +); |
| 52 | + |
| 53 | +interface OrgSectionProps { |
| 54 | + org: string; |
| 55 | + orgName: string; |
| 56 | + scopes: ScopeItem[]; |
| 57 | + selectedScopes: Set<string>; |
| 58 | + onScopeToggle: (scopeId: string) => void; |
| 59 | +} |
| 60 | + |
| 61 | +const OrgSection = ({ |
| 62 | + orgName, scopes, selectedScopes, onScopeToggle, |
| 63 | +}: OrgSectionProps) => { |
| 64 | + const [collapsed, setCollapsed] = useState(false); |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className="scope-org-group mb-3"> |
| 68 | + <button |
| 69 | + type="button" |
| 70 | + className="d-flex align-items-center bg-transparent border-0 p-0 mb-2 text-primary font-weight-bold" |
| 71 | + style={{ gap: '4px' }} |
| 72 | + onClick={() => setCollapsed((prev) => !prev)} |
| 73 | + > |
| 74 | + <span>Org: {orgName}</span> |
| 75 | + <Icon src={collapsed ? ExpandMore : ExpandLess} className="text-primary" /> |
| 76 | + </button> |
| 77 | + |
| 78 | + {!collapsed && ( |
| 79 | + <div className="pl-2"> |
| 80 | + {scopes.map((scope) => ( |
| 81 | + <ScopeCheckboxItem |
| 82 | + key={scope.id} |
| 83 | + scope={scope} |
| 84 | + checked={selectedScopes.has(scope.id)} |
| 85 | + onToggle={onScopeToggle} |
| 86 | + /> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
1 | 94 | interface DefineApplicationScopeStepProps { |
2 | 95 | selectedRole: string | null; |
3 | 96 | selectedScopes: Set<string>; |
4 | 97 | onScopeToggle: (scopeId: string) => void; |
5 | 98 | } |
6 | 99 |
|
7 | | -const DefineApplicationScopeStep = ( |
8 | | - { selectedRole, selectedScopes, onScopeToggle }: DefineApplicationScopeStepProps, |
9 | | -) => { |
10 | | - // Placeholder for Step 2 - "Define Application Scope" |
11 | | - // This will be implemented in future iterations |
12 | | - console.log(selectedRole, selectedScopes, onScopeToggle); |
| 100 | +const DefineApplicationScopeStep = ({ |
| 101 | + selectedRole, |
| 102 | + selectedScopes, |
| 103 | + onScopeToggle, |
| 104 | +}: DefineApplicationScopeStepProps) => { |
| 105 | + const [search, setSearch] = useState(''); |
| 106 | + const [debouncedSearch, setDebouncedSearch] = useState(''); |
| 107 | + const [selectedOrg, setSelectedOrg] = useState<string>(''); |
| 108 | + const loadMoreRef = useRef<HTMLDivElement>(null); |
| 109 | + |
| 110 | + useEffect(() => { |
| 111 | + const timer = setTimeout(() => setDebouncedSearch(search), 300); |
| 112 | + return () => clearTimeout(timer); |
| 113 | + }, [search]); |
| 114 | + |
| 115 | + const contextType = getContextType(selectedRole); |
| 116 | + const contextLabel = getContextLabel(contextType); |
| 117 | + |
| 118 | + const { |
| 119 | + data: scopesData, |
| 120 | + fetchNextPage, |
| 121 | + hasNextPage, |
| 122 | + isFetchingNextPage, |
| 123 | + isLoading, |
| 124 | + } = useScopes({ |
| 125 | + contextType, |
| 126 | + search: debouncedSearch || undefined, |
| 127 | + org: selectedOrg || undefined, |
| 128 | + }); |
| 129 | + |
| 130 | + const { data: organizations } = useOrganizations(contextType); |
| 131 | + |
| 132 | + const allScopes = useMemo( |
| 133 | + () => scopesData?.pages.flatMap((page) => page.results) ?? [], |
| 134 | + [scopesData], |
| 135 | + ); |
| 136 | + |
| 137 | + const totalCount = scopesData?.pages[0]?.count ?? 0; |
| 138 | + |
| 139 | + const platformScopes = useMemo( |
| 140 | + () => allScopes.filter((s) => !s.org), |
| 141 | + [allScopes], |
| 142 | + ); |
| 143 | + |
| 144 | + const scopesByOrg = useMemo(() => { |
| 145 | + const orgScopes = allScopes.filter((s) => !!s.org); |
| 146 | + return orgScopes.reduce<Record<string, ScopeItem[]>>((acc, scope) => { |
| 147 | + if (!acc[scope.org]) { acc[scope.org] = []; } |
| 148 | + acc[scope.org].push(scope); |
| 149 | + return acc; |
| 150 | + }, {}); |
| 151 | + }, [allScopes]); |
| 152 | + |
| 153 | + const orderedOrgs = useMemo(() => Object.keys(scopesByOrg), [scopesByOrg]); |
| 154 | + |
| 155 | + useEffect(() => { |
| 156 | + const el = loadMoreRef.current; |
| 157 | + if (!el) { return undefined; } |
| 158 | + const observer = new IntersectionObserver( |
| 159 | + (entries) => { |
| 160 | + if (entries[0].isIntersecting && hasNextPage && !isFetchingNextPage) { |
| 161 | + fetchNextPage(); |
| 162 | + } |
| 163 | + }, |
| 164 | + { threshold: 0.1 }, |
| 165 | + ); |
| 166 | + observer.observe(el); |
| 167 | + return () => observer.disconnect(); |
| 168 | + }, [hasNextPage, isFetchingNextPage, fetchNextPage]); |
| 169 | + |
| 170 | + const selectedOrgLabel = organizations?.find((o) => o.org === selectedOrg)?.name |
| 171 | + || organizations?.find((o) => o.org === selectedOrg)?.org |
| 172 | + || 'Organization'; |
| 173 | + |
| 174 | + return ( |
| 175 | + <div className="define-application-scope-step"> |
| 176 | + <h3 className="mb-4">Applies to</h3> |
| 177 | + |
| 178 | + {/* Search + Organization filter + count */} |
| 179 | + <div className="d-flex align-items-center justify-content-between gap-3 mb-2 flex-wrap"> |
| 180 | + <div className="d-flex align-items-center gap-3"> |
| 181 | + <div style={{ width: '300px' }}> |
| 182 | + <Form.Group controlId="scope-search" className="mb-0"> |
| 183 | + <Form.Control |
| 184 | + type="text" |
| 185 | + value={search} |
| 186 | + onChange={(e: { target: { value: string } }) => setSearch(e.target.value)} |
| 187 | + placeholder="Search" |
| 188 | + trailingElement={<Icon src={Search} />} |
| 189 | + /> |
| 190 | + </Form.Group> |
| 191 | + </div> |
| 192 | + |
| 193 | + <Dropdown> |
| 194 | + <Dropdown.Toggle variant="outline-primary" id="org-filter-toggle"> |
| 195 | + <Icon src={FilterList} className="mr-2" /> |
| 196 | + {selectedOrg ? selectedOrgLabel : 'Organization'} |
| 197 | + </Dropdown.Toggle> |
| 198 | + <Dropdown.Menu> |
| 199 | + <Dropdown.Item onClick={() => setSelectedOrg('')} active={!selectedOrg}> |
| 200 | + All Organizations |
| 201 | + </Dropdown.Item> |
| 202 | + {organizations?.map((org) => ( |
| 203 | + <Dropdown.Item |
| 204 | + key={org.org} |
| 205 | + onClick={() => setSelectedOrg(org.org)} |
| 206 | + active={selectedOrg === org.org} |
| 207 | + > |
| 208 | + {org.name || org.org} |
| 209 | + </Dropdown.Item> |
| 210 | + ))} |
| 211 | + </Dropdown.Menu> |
| 212 | + </Dropdown> |
| 213 | + </div> |
| 214 | + |
| 215 | + <span className="text-muted small text-nowrap"> |
| 216 | + Showing {allScopes.length} of {totalCount}. |
| 217 | + </span> |
| 218 | + </div> |
| 219 | + |
| 220 | + {/* Active filter chip */} |
| 221 | + {contextType && ( |
| 222 | + <div className="mb-3 d-flex align-items-center gap-2"> |
| 223 | + <span className="text-muted small">Filter applied:</span> |
| 224 | + <Badge className="py-1 px-2" style={{ background: '#e8e8e8', color: '#333', fontWeight: 'normal' }}> |
| 225 | + {contextLabel} |
| 226 | + </Badge> |
| 227 | + </div> |
| 228 | + )} |
| 229 | + |
| 230 | + {/* Scopes list */} |
| 231 | + <div |
| 232 | + className="scope-list border rounded p-3" |
| 233 | + style={{ maxHeight: '500px', overflowY: 'auto' }} |
| 234 | + > |
| 235 | + {isLoading ? ( |
| 236 | + <div className="d-flex justify-content-center p-4"> |
| 237 | + <Spinner animation="border" screenReaderText="Loading scopes..." /> |
| 238 | + </div> |
| 239 | + ) : ( |
| 240 | + <> |
| 241 | + {platformScopes.map((scope) => ( |
| 242 | + <ScopeCheckboxItem |
| 243 | + key={scope.id} |
| 244 | + scope={scope} |
| 245 | + checked={selectedScopes.has(scope.id)} |
| 246 | + onToggle={onScopeToggle} |
| 247 | + /> |
| 248 | + ))} |
| 249 | + |
| 250 | + {orderedOrgs.map((org) => ( |
| 251 | + <OrgSection |
| 252 | + key={org} |
| 253 | + org={org} |
| 254 | + orgName={organizations?.find((o) => o.org === org)?.name || org} |
| 255 | + scopes={scopesByOrg[org]} |
| 256 | + selectedScopes={selectedScopes} |
| 257 | + onScopeToggle={onScopeToggle} |
| 258 | + /> |
| 259 | + ))} |
| 260 | + |
| 261 | + {allScopes.length === 0 && ( |
| 262 | + <p className="text-muted text-center py-3">No scopes found.</p> |
| 263 | + )} |
| 264 | + |
| 265 | + <div ref={loadMoreRef} /> |
13 | 266 |
|
14 | | - return <h1>Step 2</h1>; |
| 267 | + {isFetchingNextPage && ( |
| 268 | + <div className="d-flex justify-content-center py-2"> |
| 269 | + <Spinner animation="border" size="sm" screenReaderText="Loading more..." /> |
| 270 | + </div> |
| 271 | + )} |
| 272 | + </> |
| 273 | + )} |
| 274 | + </div> |
| 275 | + </div> |
| 276 | + ); |
15 | 277 | }; |
16 | 278 |
|
17 | 279 | export default DefineApplicationScopeStep; |
0 commit comments