generated from codetheweb/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from DylanHojnoski/share-url
- Loading branch information
Showing
5 changed files
with
271 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import {Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalHeader, | ||
ModalOverlay, | ||
IconButton, | ||
useToast, | ||
Tooltip, | ||
Stack, | ||
HStack, | ||
Box, | ||
} from '@chakra-ui/react'; | ||
import {CopyIcon} from '@chakra-ui/icons'; | ||
import {observer} from 'mobx-react-lite'; | ||
import useStore from 'src/lib/state/context'; | ||
import {type IPotentialFutureTerm} from 'src/lib/types'; | ||
import Link from 'next/link'; | ||
import {useEffect} from 'react'; | ||
|
||
type ExportLinkProps = { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
}; | ||
|
||
export type BasketData = { | ||
term: IPotentialFutureTerm; | ||
name: string; | ||
sections: string[]; | ||
courses: string[]; | ||
searchQueries: string[]; | ||
}; | ||
|
||
const ExportLink = observer(({isOpen, onClose}: ExportLinkProps) => { | ||
const {allBasketsState: {currentBasket}} = useStore(); | ||
const toast = useToast(); | ||
let url = ''; | ||
|
||
if (currentBasket) { | ||
const basketData: BasketData = {term: currentBasket.forTerm, | ||
name: currentBasket.name, | ||
sections: currentBasket.sections.map(element => element.id), | ||
courses: currentBasket.courses.map(element => element.id), | ||
searchQueries: currentBasket.searchQueries}; | ||
|
||
// Get json data | ||
const jsonString: string = JSON.stringify(basketData); | ||
url = window.location.toString() + '?basket=' + encodeURIComponent(jsonString); | ||
} | ||
|
||
const handleLinkCopy = async () => { | ||
if (url.length > 0) { | ||
try { | ||
await navigator.clipboard.writeText(url); | ||
|
||
toast({ | ||
title: 'Link Copied', | ||
status: 'success', | ||
duration: 500, | ||
}); | ||
} catch (error) { | ||
console.error('Failed to copy link to clipboard:', error); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const copyOnOpen = async () => { | ||
if (isOpen) { | ||
await handleLinkCopy(); | ||
} | ||
}; | ||
|
||
void copyOnOpen(); | ||
}, [isOpen]); | ||
|
||
return ( | ||
<> | ||
<Modal | ||
isOpen={isOpen} | ||
size='3xl' | ||
autoFocus={false} | ||
onClose={onClose} | ||
> | ||
<ModalOverlay/> | ||
<ModalContent> | ||
<ModalHeader>Share Link</ModalHeader> | ||
<ModalCloseButton/> | ||
<ModalBody> | ||
<Stack spacing={4}> | ||
<Box overflow='hidden' textOverflow='ellipsis' whiteSpace='nowrap'> | ||
<Link href={url}> | ||
{url} | ||
</Link> | ||
</Box> | ||
<HStack w='full' justifyContent='end'> | ||
<Tooltip label='copy link'> | ||
<IconButton aria-label='copy link' icon={<CopyIcon />} onClick={async () => { | ||
await handleLinkCopy(); | ||
}}/> | ||
</Tooltip> | ||
</HStack> | ||
</Stack> | ||
</ModalBody> | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
); | ||
}); | ||
|
||
export default ExportLink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import React from 'react'; | ||
import { | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalHeader, | ||
ModalOverlay, | ||
IconButton, | ||
Tooltip, | ||
Stack, | ||
HStack, | ||
Text, | ||
} from '@chakra-ui/react'; | ||
import {CheckIcon} from '@chakra-ui/icons'; | ||
import {observer} from 'mobx-react-lite'; | ||
import useStore from 'src/lib/state/context'; | ||
import {BasketState} from 'src/lib/state/basket'; | ||
import {type IPotentialFutureTerm} from 'src/lib/types'; | ||
import toTitleCase from 'src/lib/to-title-case'; | ||
import {SEMESTER_DISPLAY_MAPPING} from 'src/lib/constants'; | ||
import {type BasketData} from '../export-options/link'; | ||
import BasketTable from '../table'; | ||
|
||
type ImportBasketProps = { | ||
basketData: BasketData; | ||
isOpen: boolean; | ||
onClose: () => void; | ||
}; | ||
|
||
const ImportBasket = observer(({basketData, isOpen, onClose}: ImportBasketProps) => { | ||
const {allBasketsState} = useStore(); | ||
const {apiState} = useStore(); | ||
|
||
const partialBasket: Partial<BasketState> = { | ||
id: '0', | ||
name: basketData.name, | ||
forTerm: basketData.term, | ||
sectionIds: basketData.sections, | ||
courseIds: basketData.courses, | ||
searchQueries: basketData.searchQueries, | ||
}; | ||
|
||
const createdBasket = new BasketState(apiState, basketData.term, basketData.name, partialBasket); | ||
|
||
const getTermDisplayName = (term: IPotentialFutureTerm) => { | ||
if (term.isFuture) { | ||
return toTitleCase(`Future ${term.semester.toLowerCase()} Semester`); | ||
} | ||
|
||
return `${SEMESTER_DISPLAY_MAPPING[term.semester]} ${term.year}`; | ||
}; | ||
|
||
const importBasket = () => { | ||
const newBasket = allBasketsState.addBasket(basketData.term); | ||
|
||
newBasket.setName(basketData.name); | ||
|
||
for (const element of basketData.sections) { | ||
newBasket.addSection(element); | ||
} | ||
|
||
for (const element of basketData.courses) { | ||
newBasket.addCourse(element); | ||
} | ||
|
||
for (const element of basketData.searchQueries) { | ||
newBasket.addSearchQuery(element); | ||
} | ||
|
||
allBasketsState.setSelectedBasket(newBasket.id); | ||
|
||
onClose(); | ||
}; | ||
|
||
return ( | ||
<> | ||
{ | ||
apiState.hasDataForTrackedEndpoints | ||
&& <Modal | ||
isOpen={isOpen} | ||
size='3xl' | ||
autoFocus={false} | ||
onClose={onClose} | ||
> | ||
<ModalOverlay/> | ||
<ModalContent> | ||
<ModalHeader>Import Basket - {createdBasket.name}</ModalHeader> | ||
<ModalCloseButton/> | ||
<ModalBody> | ||
<Stack spacing={4}> | ||
<Text> | ||
Term: {getTermDisplayName(createdBasket.forTerm)} | ||
</Text> | ||
<BasketTable basket={createdBasket} isForCapture={true}/> | ||
<HStack w='full' justifyContent='end'> | ||
<Tooltip label='import basket'> | ||
<IconButton aria-label='copy link' icon={<CheckIcon />} onClick={() => { | ||
importBasket(); | ||
}}/> | ||
</Tooltip> | ||
</HStack> | ||
</Stack> | ||
</ModalBody> | ||
</ModalContent> | ||
</Modal> | ||
} | ||
</> | ||
); | ||
}); | ||
|
||
export default ImportBasket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters