-
-
Notifications
You must be signed in to change notification settings - Fork 676
streams: Show alert when stream name already exists. #5331
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,19 @@ | ||
| /* @flow strict-local */ | ||
| import React, { useCallback } from 'react'; | ||
| import React, { useCallback, useContext } from 'react'; | ||
| import type { Node } from 'react'; | ||
|
|
||
| import { TranslationContext } from '../boot/TranslationProvider'; | ||
| import type { RouteProp } from '../react-navigation'; | ||
| import type { AppNavigationProp } from '../nav/AppNavigator'; | ||
| import * as NavigationService from '../nav/NavigationService'; | ||
| import { useSelector } from '../react-redux'; | ||
| import { navigateBack } from '../actions'; | ||
| import { getAuth } from '../selectors'; | ||
| import { getStreamsByName } from '../subscriptions/subscriptionSelectors'; | ||
| import Screen from '../common/Screen'; | ||
| import EditStreamCard from './EditStreamCard'; | ||
| import { showErrorAlert } from '../utils/info'; | ||
| import { ApiError } from '../api/apiErrors'; | ||
| import * as api from '../api'; | ||
|
|
||
| type Props = $ReadOnly<{| | ||
|
|
@@ -18,14 +22,41 @@ type Props = $ReadOnly<{| | |
| |}>; | ||
|
|
||
| export default function CreateStreamScreen(props: Props): Node { | ||
| const _ = useContext(TranslationContext); | ||
|
|
||
| const auth = useSelector(getAuth); | ||
| const streamsByName = useSelector(getStreamsByName); | ||
|
|
||
| const handleComplete = useCallback( | ||
| (name: string, description: string, invite_only: boolean) => { | ||
| api.createStream(auth, { name, description, invite_only }); | ||
| NavigationService.dispatch(navigateBack()); | ||
| async (name: string, description: string, invite_only: boolean) => { | ||
| // This will miss existing streams that the client can't know about; | ||
| // for example, a private stream the user can't access. See comment | ||
| // where we catch an `ApiError`, below. | ||
| if (streamsByName.has(name)) { | ||
|
Contributor
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 rename should be squashed into the commit that added the wrong name, so there aren't any commits with the wrong name. I'll take care of that before merging. |
||
| showErrorAlert(_('A stream with this name already exists.')); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await api.createStream(auth, { name, description, invite_only }); | ||
| NavigationService.dispatch(navigateBack()); | ||
| } catch (error) { | ||
| // If the stream already exists but you can't access it (e.g., it's | ||
| // private), then it won't be in the client's data structures, so | ||
| // our client-side check above won't stop the request from being | ||
| // made. In that case, we expect the server to always give an error, | ||
| // because you can't subscribe to a stream that you can't access. | ||
| // That error will have: | ||
| // - error.message: `Unable to access stream (${streamName})`, or a | ||
| // translation of that into the user's own language | ||
| // - error.code: "BAD_REQUEST" (as of server feature level 126; | ||
| // possibly the server should be more specific) | ||
| if (error instanceof ApiError) { | ||
| showErrorAlert(error.message); | ||
| } | ||
| } | ||
| }, | ||
| [auth], | ||
| [auth, streamsByName, _], | ||
| ); | ||
|
|
||
| return ( | ||
|
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Commit message nit: Let's link to the discussion where we decided on this approach; perhaps here: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M5199/near/1363029 .
There were a few things that suggested this approach instead of others (one being the awkwardness of the server API); it could be helpful context in case we look into changing the design in the future.