|
| 1 | +import { |
| 2 | + OrgAbout, |
| 3 | + OrgHeader, |
| 4 | + OrgImage, |
| 5 | + OrgName, |
| 6 | + OrgTopHeader, |
| 7 | +} from '../components/user-repos/UserRepos.styles'; |
| 8 | +import Header from '../components/header/Header'; |
| 9 | +import UserRepos from '../components/user-repos/UserRepos'; |
| 10 | +import { ORG_INFO } from '../constants/url.constants'; |
| 11 | +import { fromFetchWithAuth } from '../hooks/auth/from-fetch-with-auth'; |
| 12 | +import { useState, useEffect } from 'react'; |
| 13 | +import { useParams } from 'react-router-dom'; |
| 14 | +import { tap } from 'rxjs'; |
| 15 | +import { IOrg } from '../interfaces/org.interface'; |
| 16 | +import { LoadingBulletList } from '../components/Loading'; |
| 17 | + |
| 18 | +export default function OrgPage() { |
| 19 | + const [loading, setLoading] = useState(true); |
| 20 | + const [orgInfo, setOrgInfo] = useState<IOrg>(); |
| 21 | + const { username } = useParams(); |
| 22 | + |
| 23 | + const request = (url: string) => |
| 24 | + fromFetchWithAuth(url, { |
| 25 | + selector: (response: Response) => { |
| 26 | + return response.json(); |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + if (!username) { |
| 32 | + return; |
| 33 | + } |
| 34 | + const GITHUB_URL = ORG_INFO(username); |
| 35 | + const subscription = request(GITHUB_URL) |
| 36 | + .pipe( |
| 37 | + tap((data) => { |
| 38 | + if (data) { |
| 39 | + setOrgInfo(data); |
| 40 | + setLoading(false); |
| 41 | + } |
| 42 | + }) |
| 43 | + ) |
| 44 | + .subscribe(); |
| 45 | + return () => { |
| 46 | + subscription.unsubscribe(); |
| 47 | + }; |
| 48 | + }, [username]); |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + <Header /> |
| 53 | + <OrgHeader> |
| 54 | + <OrgTopHeader> |
| 55 | + {loading && <LoadingBulletList />} |
| 56 | + {orgInfo && ( |
| 57 | + <OrgAbout> |
| 58 | + <OrgImage |
| 59 | + src={orgInfo.avatar_url} |
| 60 | + alt="Org Avatar" |
| 61 | + data-testid="org about avatar" |
| 62 | + /> |
| 63 | + <OrgName data-testid="org about name">{orgInfo.name}</OrgName> |
| 64 | + </OrgAbout> |
| 65 | + )} |
| 66 | + </OrgTopHeader> |
| 67 | + </OrgHeader> |
| 68 | + <UserRepos isOrg /> |
| 69 | + </> |
| 70 | + ); |
| 71 | +} |
0 commit comments