Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last Shortcut Bugs #530

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
return url
}

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

Check warning on line 57 in src/components/AddShortcut.js

View check run for this annotation

Codecov / codecov/patch

src/components/AddShortcut.js#L57

Added line #L57 was not covered by tests
}
}

const AddShortcut = ({
onCancel,
onSave,
Expand All @@ -59,6 +67,8 @@
}) => {
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 @@
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.'

Check warning on line 95 in src/components/AddShortcut.js

View check run for this annotation

Codecov / codecov/patch

src/components/AddShortcut.js#L95

Added line #L95 was not covered by tests
} 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 @@
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 @@
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 @@
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
Loading