Skip to content

Commit

Permalink
Automatically create basket if one doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Mar 29, 2023
1 parent f016197 commit 055badc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
16 changes: 11 additions & 5 deletions src/components/courses-table/details-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,25 @@ const Stats = observer(({courseKey}: {courseKey: string}) => {
});

const DetailsRow = ({course, onlyShowSections, onShowEverything, onShareCourse}: {course: ICourseWithFilteredSections; onlyShowSections: boolean; onShowEverything: () => void; onShareCourse: () => void}) => {
const {allBasketsState: {currentBasket}} = useStore();
const {allBasketsState, apiState: {selectedTerm}} = useStore();
const courseKey = `${course.course.subject}${course.course.crse}`;

const courseSections = course.sections.wasFiltered ? course.sections.filtered : course.sections.all;

const isCourseInBasket = currentBasket?.hasCourse(course.course.id);
const isCourseInBasket = allBasketsState.currentBasket?.hasCourse(course.course.id);

const handleBasketAction = () => {
if (!allBasketsState.currentBasket && selectedTerm) {
const basket = allBasketsState.addBasket(selectedTerm);
allBasketsState.setSelectedBasket(basket.id);
basket.addCourse(course.course.id);
return;
}

if (isCourseInBasket) {
currentBasket?.removeCourse(course.course.id);
allBasketsState.currentBasket?.removeCourse(course.course.id);
} else {
currentBasket?.addCourse(course.course.id);
allBasketsState.currentBasket?.addCourse(course.course.id);
}
};

Expand Down Expand Up @@ -106,7 +113,6 @@ const DetailsRow = ({course, onlyShowSections, onShowEverything, onShareCourse}:
<Tooltip label={isCourseInBasket ? 'remove course from basket' : 'add course to basket'}>
<IconButton
icon={isCourseInBasket ? <DeleteIcon/> : <AddIcon/>}
isDisabled={!currentBasket}
aria-label="Add course to basket"
size="xs"
colorScheme={isCourseInBasket ? 'red' : undefined}
Expand Down
22 changes: 14 additions & 8 deletions src/components/search-bar/courses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,29 @@ const CoursesSearchBar = observer(() => {
const {
uiState,
apiState,
allBasketsState: {currentBasket},
allBasketsState,
} = useStore();

const isQuerySaved = uiState.searchValue === '' ? false : currentBasket?.searchQueries.includes(uiState.searchValue);
const isQuerySaved = uiState.searchValue === '' ? false : allBasketsState.currentBasket?.searchQueries.includes(uiState.searchValue);

const handleQuerySaveOrDelete = useCallback(() => {
if (!currentBasket) {
if (!allBasketsState.currentBasket && apiState.selectedTerm) {
const basket = allBasketsState.addBasket(apiState.selectedTerm);
allBasketsState.setSelectedBasket(basket.id);
basket.addSearchQuery(uiState.searchValue);
return;
}

if (currentBasket.searchQueries.includes(uiState.searchValue)) {
currentBasket.removeSearchQuery(uiState.searchValue);
if (!allBasketsState.currentBasket) {
return;
}

if (allBasketsState.currentBasket.searchQueries.includes(uiState.searchValue)) {
allBasketsState.currentBasket.removeSearchQuery(uiState.searchValue);
} else {
currentBasket.addSearchQuery(uiState.searchValue);
allBasketsState.currentBasket.addSearchQuery(uiState.searchValue);
}
}, [currentBasket, uiState.searchValue]);
}, [allBasketsState, uiState.searchValue, apiState.selectedTerm]);

const handleSearchChange = useCallback((newValue: string) => {
uiState.setSearchValue(newValue);
Expand All @@ -179,7 +186,6 @@ const CoursesSearchBar = observer(() => {
rounded="full"
size="xs"
mr={2}
isDisabled={!currentBasket}
onClick={handleQuerySaveOrDelete}
/>
</Tooltip>
Expand Down
16 changes: 11 additions & 5 deletions src/components/sections-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ interface ISectionsTableProps {
}

const Row = observer(({section}: {section: ISectionFromAPIWithSchedule}) => {
const {allBasketsState: {currentBasket}, apiState} = useStore();
const {allBasketsState, apiState} = useStore();
const creditsString = getCreditsStr(section.minCredits, section.maxCredits);

const isSectionInBasket = currentBasket?.hasSection(section.id);
const isSectionInBasket = allBasketsState.currentBasket?.hasSection(section.id);

const handleBasketAction = () => {
if (!allBasketsState.currentBasket && apiState.selectedTerm) {
const basket = allBasketsState.addBasket(apiState.selectedTerm);
allBasketsState.setSelectedBasket(basket.id);
basket.addSection(section.id);
return;
}

if (isSectionInBasket) {
currentBasket?.removeSection(section.id);
allBasketsState.currentBasket?.removeSection(section.id);
} else {
currentBasket?.addSection(section.id);
allBasketsState.currentBasket?.addSection(section.id);
}
};

Expand Down Expand Up @@ -69,7 +76,6 @@ const Row = observer(({section}: {section: ISectionFromAPIWithSchedule}) => {
size="xs"
colorScheme={isSectionInBasket ? 'red' : undefined}
icon={isSectionInBasket ? <DeleteIcon/> : <AddIcon/>}
isDisabled={!currentBasket}
aria-label={isSectionInBasket ? 'Remove from basket' : 'Add to basket'}
onClick={handleBasketAction}/>
</Td>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const AboutPage = () => (
<VStack align="flex-start" spacing={10}>
<VStack align="flex-start">
<Text as="span">
👋 Hi! I'm <WrappedLink href="https://maxisom.me" display="inline-block">Max</WrappedLink>, a student at Michigan Tech.
👋 Hi! I'm <WrappedLink href="https://maxisom.me" display="inline-block">Max</WrappedLink>, a former student of Michigan Tech.
</Text>

<Text as="span">
Expand Down

1 comment on commit 055badc

@vercel
Copy link

@vercel vercel bot commented on 055badc Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.