Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 91e54e0
Merge: 99d4c57 f0c3319
Author: Samarth Chandna <[email protected]>
Date:   Fri Apr 21 16:11:39 2023 -0400

    Merge branch 'bog-changes-s23' of https://github.com/gt-scheduler/website into Samarth/172-load-friend-schedules

commit 99d4c57
Author: Samarth Chandna <[email protected]>
Date:   Fri Apr 21 13:20:02 2023 -0400

    added check to see if terms field exists in FriendData

commit 549c9fc
Author: Samarth Chandna <[email protected]>
Date:   Sat Apr 8 11:39:56 2023 -0400

    nits

commit b44ca3c
Author: Samarth Chandna <[email protected]>
Date:   Sat Apr 8 03:55:28 2023 -0400

    added useDeepCompareEffect and decoupled states from their setters

commit a11b277
Author: Samarth Chandna <[email protected]>
Date:   Sat Apr 8 01:33:00 2023 -0400

    changed request content type to prevent CORS preflight request

commit c6fde45
Author: Hailey Ho <[email protected]>
Date:   Fri Mar 31 03:17:56 2023 -0400

    Fix friend schedule type

commit 5a3a578
Merge: aa9e4bb adfc241
Author: Nghi Ho <[email protected]>
Date:   Fri Mar 31 03:08:23 2023 -0400

    Merge branch 'bog-changes-s23' into Samarth/172-load-friend-schedules

commit aa9e4bb
Merge: d2c0c01 20c58a2
Author: Nghi Ho <[email protected]>
Date:   Fri Mar 31 00:27:47 2023 -0400

    Merge branch 'bog-changes-s23' into Samarth/172-load-friend-schedules

commit d2c0c01
Author: Samarth Chandna <[email protected]>
Date:   Fri Mar 3 05:24:11 2023 -0500

    integrated friend stages and create friend context

commit 8d11931
Merge: 1771779 52006bb
Author: Samarth Chandna <[email protected]>
Date:   Fri Mar 3 00:24:29 2023 -0500

    Merge branch '171-friend-data' of https://github.com/gt-scheduler/website into Samarth/172-load-friend-schedules

commit 52006bb
Author: nathangong <[email protected]>
Date:   Fri Mar 3 00:19:06 2023 -0500

    Preliminary friend data workflow

commit 1771779
Author: Samarth Chandna <[email protected]>
Date:   Fri Mar 3 00:11:21 2023 -0500

    added hooks to extract friend info and fetch schedules
  • Loading branch information
samarth52 committed Oct 19, 2023
1 parent 749ecd2 commit f87c046
Show file tree
Hide file tree
Showing 18 changed files with 1,183 additions and 85 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@fortawesome/react-fontawesome": "^0.2.0",
"@sentry/react": "^6.12.0",
"@sentry/tracing": "^6.12.0",
"@types/lodash": "^4.14.192",
"@types/react-map-gl": "^6.1.3",
"axios": "^0.21.4",
"cheerio": "^1.0.0-rc.3",
Expand All @@ -23,6 +24,7 @@
"html-entities": "^2.3.3",
"immer": "^9.0.6",
"js-cookie": "^3.0.1",
"lodash": "^4.17.21",
"mapbox-gl": "^2.4.1",
"node-sass": "^6.0.1",
"normalize.css": "^8.0.1",
Expand Down
238 changes: 206 additions & 32 deletions src/components/AppDataLoader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import produce, { Immutable, Draft, original, castDraft } from 'immer';
import React, { useCallback, useMemo } from 'react';

import {
ScheduleContextValue,
TermsContext,
ScheduleContext,
ScheduleContextValue,
FriendContext,
FriendContextValue,
} from '../../contexts';
import { AccountContext, AccountContextValue } from '../../contexts/account';
import { Oscar } from '../../data/beans';
Expand All @@ -15,21 +17,30 @@ import {
TermScheduleData,
ScheduleVersion,
ScheduleData,
FriendTermData,
FriendInfo,
FriendScheduleData,
} from '../../data/types';
import { lexicographicCompare } from '../../utils/misc';
import {
StageLoadUIState,
StageLoadTerms,
StageEnsureValidTerm,
StageLoadAccount,
StageLoadRawFriendData,
StageLoadRawScheduleDataHybrid,
StageMigrateScheduleData,
StageCreateScheduleDataProducer,
StageExtractTermScheduleData,
StageLoadOscarData,
StageExtractScheduleVersion,
StageSkeletonProps,
StageCreateFriendDataProducer,
StageExtractFriendTermData,
StageLoadRawFriendScheduleDataFromFirebaseFunction,
StageExtractFriendInfo,
} from './stages';
import { softError, ErrorWithFields } from '../../log';

export type DataLoaderProps = {
children: React.ReactNode;
Expand Down Expand Up @@ -99,48 +110,70 @@ export default function DataLoader({
termScheduleData,
updateTermScheduleData,
}): React.ReactElement => (
<StageLoadOscarData
<GroupLoadFriendScheduleData
skeletonProps={{ termsState, accountState }}
term={currentTerm}
accountState={accountState}
currentTerm={currentTerm}
>
{({ oscar }): React.ReactElement => (
<StageExtractScheduleVersion
{({
friendScheduleData,
updateFriendTermData,
updateFriendInfo,
}): React.ReactElement => (
<StageLoadOscarData
skeletonProps={{ termsState, accountState }}
currentVersionRaw={currentVersionRaw}
setVersion={setVersion}
termScheduleData={termScheduleData}
updateTermScheduleData={
updateTermScheduleData
}
term={currentTerm}
>
{({
currentVersion,
scheduleVersion,
updateScheduleVersion,
}): React.ReactElement => (
<ContextProvider
terms={terms}
currentTerm={currentTerm}
setTerm={setTerm}
currentVersion={currentVersion}
{({ oscar }): React.ReactElement => (
<StageExtractScheduleVersion
skeletonProps={{
termsState,
accountState,
}}
currentVersionRaw={currentVersionRaw}
setVersion={setVersion}
oscar={oscar}
scheduleVersion={scheduleVersion}
updateScheduleVersion={
updateScheduleVersion
}
termScheduleData={termScheduleData}
updateTermScheduleData={
updateTermScheduleData
}
accountState={accountState}
>
{children}
</ContextProvider>
{({
currentVersion,
scheduleVersion,
updateScheduleVersion,
}): React.ReactElement => (
<ContextProvider
terms={terms}
currentTerm={currentTerm}
setTerm={setTerm}
currentVersion={currentVersion}
setVersion={setVersion}
oscar={oscar}
scheduleVersion={scheduleVersion}
updateScheduleVersion={
updateScheduleVersion
}
termScheduleData={termScheduleData}
updateTermScheduleData={
updateTermScheduleData
}
accountState={accountState}
friendScheduleData={
friendScheduleData
}
updateFriendTermData={
updateFriendTermData
}
updateFriendInfo={updateFriendInfo}
>
{children}
</ContextProvider>
)}
</StageExtractScheduleVersion>
)}
</StageExtractScheduleVersion>
</StageLoadOscarData>
)}
</StageLoadOscarData>
</GroupLoadFriendScheduleData>
)}
</StageExtractTermScheduleData>
)}
Expand Down Expand Up @@ -203,6 +236,87 @@ function GroupLoadScheduleData({
);
}

type GroupLoadFriendScheduleDataProps = {
skeletonProps?: StageSkeletonProps;
accountState: AccountContextValue;
currentTerm: string;
children: (props: {
friendScheduleData: Immutable<FriendScheduleData>;
updateFriendTermData: (
applyDraft: (
draft: Draft<FriendTermData>
) => void | Immutable<FriendTermData>
) => void;
updateFriendInfo: (
applyDraft: (draft: Draft<FriendInfo>) => void | Immutable<FriendInfo>
) => void;
}) => React.ReactNode;
};

function GroupLoadFriendScheduleData({
skeletonProps,
accountState,
currentTerm,
children,
}: GroupLoadFriendScheduleDataProps): React.ReactElement {
return (
<StageLoadRawFriendData
skeletonProps={skeletonProps}
accountState={accountState}
currentTerm={currentTerm}
>
{({ rawFriendData, setFriendData }): React.ReactElement => (
<StageCreateFriendDataProducer setFriendData={setFriendData}>
{({ updateFriendData }): React.ReactElement => (
<StageExtractFriendTermData
skeletonProps={skeletonProps}
accountState={accountState}
currentTerm={currentTerm}
rawFriendData={rawFriendData}
updateFriendData={updateFriendData}
>
{({
termFriendData,
updateFriendTermData,
}): React.ReactElement => (
<StageLoadRawFriendScheduleDataFromFirebaseFunction
skeletonProps={skeletonProps}
accountState={accountState}
currentTerm={currentTerm}
termFriendData={termFriendData}
>
{({ rawFriendScheduleData }): React.ReactElement => (
<StageExtractFriendInfo
skeletonProps={skeletonProps}
accountState={accountState}
rawFriendScheduleData={rawFriendScheduleData}
friendInfo={rawFriendData.info}
updateFriendData={updateFriendData}
>
{({
friendScheduleData,
updateFriendInfo,
}): React.ReactElement => (
<>
{children({
friendScheduleData,
updateFriendTermData,
updateFriendInfo,
})}
</>
)}
</StageExtractFriendInfo>
)}
</StageLoadRawFriendScheduleDataFromFirebaseFunction>
)}
</StageExtractFriendTermData>
)}
</StageCreateFriendDataProducer>
)}
</StageLoadRawFriendData>
);
}

type ContextProviderProps = {
terms: string[];
currentTerm: string;
Expand All @@ -223,6 +337,15 @@ type ContextProviderProps = {
) => void | Immutable<TermScheduleData>
) => void;
accountState: AccountContextValue;
friendScheduleData: Immutable<FriendScheduleData>;
updateFriendTermData: (
applyDraft: (
draft: Draft<FriendTermData>
) => void | Immutable<FriendTermData>
) => void;
updateFriendInfo: (
applyDraft: (draft: Draft<FriendInfo>) => void | Immutable<FriendInfo>
) => void;
children: React.ReactNode;
};

Expand All @@ -244,6 +367,9 @@ function ContextProvider({
termScheduleData,
updateTermScheduleData,
accountState,
friendScheduleData,
updateFriendTermData,
updateFriendInfo,
children,
}: ContextProviderProps): React.ReactElement {
// Create a `updateSchedule` function
Expand Down Expand Up @@ -298,6 +424,38 @@ function ContextProvider({
currentVersion,
});

// Create a rename friend function.
const renameFriend = useCallback(
(id: string, newName: string): void => {
updateFriendInfo((draft) => {
const existingDraft = draft[id];
if (existingDraft === undefined) {
softError(
new ErrorWithFields({
message:
"renameFriend called with current friend id that doesn't exist; ignoring",
fields: {
allFriendNames: Object.entries(draft).map(
([friendId, { name }]) => ({
id: friendId,
name,
})
),
id,
friendCount: Object.keys(draft).length,
newName,
},
})
);
return;
}

existingDraft.name = newName;
});
},
[updateFriendInfo]
);

// Memoize the context values so that they are stable
const scheduleContextValue = useMemo<ScheduleContextValue>(
() => [
Expand Down Expand Up @@ -340,11 +498,27 @@ function ContextProvider({
]
);

const friendContextValue = useMemo<FriendContextValue>(
() => [
{
friends: friendScheduleData,
},
{
renameFriend,
updateFriendTermData,
updateFriendInfo,
},
],
[friendScheduleData, renameFriend, updateFriendTermData, updateFriendInfo]
);

return (
<TermsContext.Provider value={terms}>
<ScheduleContext.Provider value={scheduleContextValue}>
<AccountContext.Provider value={accountState}>
{children}
<FriendContext.Provider value={friendContextValue}>
{children}
</FriendContext.Provider>
</AccountContext.Provider>
</ScheduleContext.Provider>
</TermsContext.Provider>
Expand Down
Loading

0 comments on commit f87c046

Please sign in to comment.