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
O3-1465: Disable the subscribe to snapshot option when version is provided #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import { useTranslation } from 'react-i18next'; | |
import { deleteSubscription, updateSubscription, useSubscription } from './subscription.resource'; | ||
import styles from './subscription.component.scss'; | ||
import { useSWRConfig } from 'swr'; | ||
import { isVersionDefinedInUrl } from '../utils'; | ||
|
||
const Subscription: React.FC = () => { | ||
const { t } = useTranslation(); | ||
|
@@ -26,6 +27,7 @@ const Subscription: React.FC = () => { | |
const [token, setToken] = useState(''); | ||
const [isSubscribedToSnapshot, setIsSubscribedToSnapshot] = useState(false); | ||
const [validationType, setValidationType] = useState<'NONE' | 'FULL'>('FULL'); | ||
const [isSnapshotOptionDisabled, setIsSnapshotOptionDisabled] = useState(false); | ||
|
||
const { data: subscription, isLoading, isError } = useSubscription(); | ||
|
||
|
@@ -35,11 +37,18 @@ const Subscription: React.FC = () => { | |
setToken(subscription?.token || ''); | ||
setIsSubscribedToSnapshot(subscription?.subscribedToSnapshot || false); | ||
setValidationType(subscription?.validationType || 'FULL'); | ||
setIsSnapshotOptionDisabled(subscription ? isVersionDefinedInUrl(subscription.url) : false); | ||
} | ||
}, [isLoading, isError, subscription]); | ||
|
||
const handleChangeSubscriptionUrl = useCallback((event: React.ChangeEvent<HTMLInputElement>) => { | ||
setSubscriptionUrl(event.target.value); | ||
if (isVersionDefinedInUrl(event.target.value)) { | ||
setIsSnapshotOptionDisabled(true); | ||
setIsSubscribedToSnapshot(false); | ||
} else { | ||
setIsSnapshotOptionDisabled(false); | ||
} | ||
}, []); | ||
|
||
const handleChangeToken = useCallback((event: React.ChangeEvent<HTMLInputElement>) => { | ||
|
@@ -59,6 +68,17 @@ const Subscription: React.FC = () => { | |
evt.preventDefault(); | ||
evt.stopPropagation(); | ||
|
||
if (isSnapshotOptionDisabled && isSubscribedToSnapshot) { | ||
showNotification({ | ||
kind: 'error', | ||
description: t( | ||
'snapshotDisabledError', | ||
"You cannot subscribe to a SNAPSHOT if you've provided the collection version", | ||
), | ||
}); | ||
return; | ||
} | ||
|
||
const abortController = new AbortController(); | ||
|
||
const updatedSubscription = { | ||
|
@@ -92,14 +112,15 @@ const Subscription: React.FC = () => { | |
|
||
return () => abortController.abort(); | ||
}, | ||
[subscriptionUrl, token, validationType, isSubscribedToSnapshot, t, subscription, mutate], | ||
[subscriptionUrl, token, validationType, isSubscribedToSnapshot, isSnapshotOptionDisabled, subscription, t, mutate], | ||
); | ||
|
||
const handleCancel = useCallback(() => { | ||
setSubscriptionUrl(subscription?.url || ''); | ||
setToken(subscription?.token || ''); | ||
setIsSubscribedToSnapshot(subscription?.subscribedToSnapshot || false); | ||
setValidationType(subscription?.validationType || 'FULL'); | ||
setIsSnapshotOptionDisabled(subscription ? isVersionDefinedInUrl(subscription.url) : false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're repeating this code multiple times in different places, it seems like maybe this should just be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part is already included in the useEffect() hook in lines 34-40. Is there a way to trigger it when calling this |
||
|
||
showNotification({ | ||
kind: 'info', | ||
|
@@ -121,6 +142,7 @@ const Subscription: React.FC = () => { | |
setToken(''); | ||
setIsSubscribedToSnapshot(false); | ||
setValidationType('FULL'); | ||
setIsSnapshotOptionDisabled(false); | ||
showNotification({ | ||
kind: 'success', | ||
description: t('subscriptionDeleted', 'Successfully unsubscribed'), | ||
|
@@ -205,6 +227,7 @@ const Subscription: React.FC = () => { | |
onChange={handleChangeSubscriptionType} | ||
labelText={t('subscribeToSnapshotText', 'Subscribe to SNAPSHOT versions (not recommended)')} | ||
id="isSubscribedToSnapshot" | ||
disabled={isSnapshotOptionDisabled} | ||
/> | ||
<Checkbox | ||
checked={validationType === 'NONE'} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const NUMBER_OF_SLASHES_AFTER_BASE_URL = 5; | ||
|
||
/* | ||
* This checks if collection version has been passed to subscription url by checking number of forward slashes after base url | ||
* If the number is 5, such as with https://api.openconceptlab.org/users/username/collections/collectionname/v1.0 | ||
* that means collection version was passed and isVersionDefinedInUrl() will return true | ||
* Also returns false if the string is not a valid URL | ||
*/ | ||
export const isVersionDefinedInUrl = (subscriptionUrl: string) => { | ||
if (subscriptionUrl.endsWith('/')) { | ||
subscriptionUrl = subscriptionUrl.substring(0, subscriptionUrl.lastIndexOf('/')); | ||
} | ||
|
||
let url; | ||
try { | ||
url = new URL(subscriptionUrl); | ||
} catch (e) { | ||
return false; | ||
} | ||
|
||
let count = url.pathname.match(/\//g)?.length ?? 0; | ||
if (count == NUMBER_OF_SLASHES_AFTER_BASE_URL) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we could do something with a modal dialog box instead? I.e., "You can't subscribe to a snapshot release if you've subscribed to a specific collection version. Do you want to remain subscribed to a specific version or switch to a snapshot?"
Also, it would be best to change "SNAPSHOT" -> "HEAD". I know the old UI was using SNAPSHOT, but OCL uses HEAD for the same concept, so it would probably be more understandable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ibacher Actually, this error is already prevented by disabling the snapshot checkbox when a version is given with the URL. So I thought a notification would be enough. If we are displaying a modal, do I have to use the
showModal
from@openmrs/esm-framework
?Shall I rename all the occurrences of
SNAPSHOT
toHEAD
?