Skip to content

Commit

Permalink
refactor: init org details syncer (#118)
Browse files Browse the repository at this point in the history
* refactor: init org details syncer

* refactor: cleanup import path
  • Loading branch information
johnshift authored Mar 19, 2024
1 parent 2e04e29 commit 999ecd7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/jobs/components/init-job-details-syncer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const InitJobDetailsSyncer = ({ id }: Props) => {
}
}, [data, initJob, setInitJob]);

// TODO: Remove this (duplicate with initJobAtom)
// Set active job ID on desktop
useEffect(() => {
if (isDesktop && !activeJobId && data) {
Expand Down
4 changes: 2 additions & 2 deletions src/orgs/atoms/init-org-atom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { atom } from 'jotai';

import { OrgListItem } from '~/orgs/core/schemas';
import { OrgDetails } from '~/orgs/core/schemas';

export const initOrgAtom = atom<OrgListItem | null>(null);
export const initOrgAtom = atom<OrgDetails | null>(null);
26 changes: 21 additions & 5 deletions src/orgs/components/init-org-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { usePathname } from 'next/navigation';
import { useAtomValue } from 'jotai';

import { HREFS } from '~/shared/core/constants';
import { getFundingRoundsData } from '~/shared/utils/get-funding-rounds-data';
import { initPathAtom } from '~/shared/atoms/init-path-atom';
import { CardSkeleton } from '~/shared/components/card-skeleton';

import { initOrgAtom } from '../atoms/init-org-atom';
import { OrgListItem } from '~/orgs/core/schemas';
import { initOrgAtom } from '~/orgs/atoms/init-org-atom';

import { OrgCard } from './org-card';

Expand All @@ -22,21 +24,35 @@ export const InitOrgCard = ({ filterParamsString }: Props) => {
const initPath = useAtomValue(initPathAtom);
const initOrg = useAtomValue(initOrgAtom);

// Do not render if initially in /jobs
// Do not render if initially on list page
if (initPath === HREFS.ORGS_PAGE) return null;

// Do not render if on /jobs page and no initOrg
// Do not render if on list page and no initOrg (mobile)
if (!initOrg && pathname === HREFS.ORGS_PAGE) return null;

// Render initOrg if set
if (initOrg)
if (initOrg) {
const { lastFundingAmount, lastFundingDate } = getFundingRoundsData(
initOrg.fundingRounds,
);

const orgItem: OrgListItem = {
...initOrg,
url: initOrg.website!,
jobCount: initOrg.jobs.length,
projectCount: initOrg.projects.length,
lastFundingAmount,
lastFundingDate,
community: [], // TODO: require mw to return community in org details
};
return (
<OrgCard
isInit
orgItem={initOrg}
orgItem={orgItem}
filterParamsString={filterParamsString}
/>
);
}

// Defaults to a skeleton
return <CardSkeleton />;
Expand Down
38 changes: 38 additions & 0 deletions src/orgs/components/init-org-details-syncer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useEffect } from 'react';

import { useAtom } from 'jotai';

import { useIsDesktop } from '~/shared/hooks/use-media-query';

import { activeOrgIdAtom } from '~/orgs/atoms/active-org-id-atom';
import { initOrgAtom } from '~/orgs/atoms/init-org-atom';
import { useOrgDetails } from '~/orgs/hooks/use-org-details';

interface Props {
id: string;
}

export const InitOrgDetailsSyncer = ({ id }: Props) => {
const [activeOrgId, setActiveOrgId] = useAtom(activeOrgIdAtom);
const [initOrg, setInitOrg] = useAtom(initOrgAtom);

const isDesktop = useIsDesktop();

const { data } = useOrgDetails(id);

// Initialize org details
useEffect(() => {
if (!initOrg && data) {
setInitOrg(data);
}
}, [data, initOrg, setInitOrg]);

// Set active org ID on desktop
useEffect(() => {
if (isDesktop && !activeOrgId && data) {
setActiveOrgId(data.orgId);
}
}, [activeOrgId, data, isDesktop, setActiveOrgId]);

return null;
};
17 changes: 17 additions & 0 deletions src/shared/utils/get-funding-rounds-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FundingRound } from '~/shared/core/schemas';

export const getFundingRoundsData = (fundingRounds: FundingRound[]) => {
let lastFundingAmount = 0;
let lastFundingDate = 0;

if (fundingRounds.length > 0) {
for (const fundingRound of fundingRounds) {
if (fundingRound.date && fundingRound.date > (lastFundingDate ?? 0)) {
lastFundingDate = fundingRound.date;
lastFundingAmount = fundingRound.raisedAmount ?? 0;
}
}
}

return { lastFundingAmount, lastFundingDate };
};

0 comments on commit 999ecd7

Please sign in to comment.