Skip to content

Commit

Permalink
Shortcut Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jedtan committed Sep 1, 2023
1 parent 51c3ab9 commit 9d3fb3e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
52 changes: 47 additions & 5 deletions src/components/AddShortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ const addProtocolToURLIfNeeded = (url) => {
return url
}

const isValidUrl = (urlString) => {
try {
return Boolean(new URL(urlString))
} catch (e) {
return false
}
}

const AddShortcut = ({
onCancel,
onSave,
Expand All @@ -59,6 +67,8 @@ const AddShortcut = ({
}) => {
const [name, setName] = useState(existingName)
const [url, setUrl] = useState(existingUrl)
const [nameError, setNameError] = useState(null)
const [urlError, setUrlError] = useState(null)
useEffect(() => setName(existingName), [existingName])
useEffect(() => setUrl(existingUrl), [existingUrl])
const classes = useStyles()
Expand All @@ -67,17 +77,49 @@ const AddShortcut = ({
setUrl(existingUrl)
onCancel()
}
const validateName = (newName) => {
let newNameError
if (newName.length === 0) {
newNameError = 'Shortcut name cannot be empty.'
} else {
newNameError = null
}
setNameError(newNameError)
return newNameError
}
const validateUrl = (newUrl) => {
let newUrlError
if (newUrl.length === 0) {
newUrlError = 'URL cannot be empty.'
} else if (!isValidUrl(addProtocolToURLIfNeeded(newUrl))) {
newUrlError = 'URL is not valid.'
} else {
newUrlError = null
}
setUrlError(newUrlError)
return newUrlError
}
const validateForm = () => {
const newNameError = validateName(name)
const newUrlError = validateUrl(url)
return newNameError || newUrlError
}
const onSaveClick = () => {
const newUrl = addProtocolToURLIfNeeded(url)
if (validateForm()) {
return
}
onSave(existingId, name, newUrl)
setName(name)
setUrl(newUrl)
}
const changeName = (e) => {
setName(e.target.value)
validateName(e.target.value)
}
const changeUrl = (e) => {
setUrl(e.target.value)
validateUrl(e.target.value)
}
return (
<Notification
Expand All @@ -100,8 +142,8 @@ const AddShortcut = ({
label="Name"
value={name}
onChange={changeName}
error={name.length === 0}
helperText={name.length === 0 && 'Bookmark name cannot be empty.'}
error={!!nameError}
helperText={nameError}
className={classes.textField}
/>
<TextField
Expand All @@ -110,8 +152,8 @@ const AddShortcut = ({
label="URL"
value={url}
onChange={changeUrl}
error={url.length === 0}
helperText={url.length === 0 && 'URL cannot be empty.'}
error={!!urlError}
helperText={urlError}
className={classes.textField}
/>
</div>
Expand All @@ -130,8 +172,8 @@ const AddShortcut = ({
onClick={onSaveClick}
className={classes.yesButton}
variant="contained"
disabled={name.length === 0 || url.length === 0}
disableElevation
disabled={!!(nameError || urlError)}
>
Save
</Button>
Expand Down
2 changes: 2 additions & 0 deletions src/components/FrontpageShortcutList.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const useStyles = makeStyles((theme) => ({
flexDirection: 'row',
maxWidth: '550px',
flexWrap: 'wrap',
zIndex: 1.4e3,
},
addCircle: {
marginTop: '24px',
Expand All @@ -61,6 +62,7 @@ const useStyles = makeStyles((theme) => ({
},
addShortcutWrapper: {
width: '400px',
position: 'relative',
},
}))
const FrontpageShortcutList = ({ openHandler, user }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ const Account = ({ data: fallbackData }) => {
{localStorageFeaturesManager.getFeatureValue(LAUNCH_BOOKMARKS) !==
'false' && (
<AccountItem
name="Bookmarks"
name="Shortcuts"
actionButton={
<Switch
checked={bookmarks}
Expand Down
5 changes: 1 addition & 4 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,6 @@ const useStyles = makeStyles((theme) => ({
zIndex: 1e4, // must be higher than all content besides ads and modal
margin: theme.spacing(1),
},
frontpageShortcutList: {
zIndex: 1.4e3,
},
logo: {
height: 50,
margin: theme.spacing(0.5),
Expand Down Expand Up @@ -1143,7 +1140,7 @@ const Index = ({ data: fallbackData, userAgent }) => {
{localStorageFeaturesManager.getFeatureValue(LAUNCH_BOOKMARKS) !==
'false' &&
bookmarkWidgetEnabled && (
<div className={classes.frontpageShortcutList}>
<div>
<FrontpageShortcutListContainer
userId={userId}
user={user}
Expand Down

0 comments on commit 9d3fb3e

Please sign in to comment.