[FIX] Add Existing Channel screen showing discussions and channels without permission (#3151)

* [Fix] the filter to show the existing channel

* [Refactor] the function that filter to isolate it

* Refactor how to wsearch properly

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-05-26 18:10:20 -03:00 committed by GitHub
parent 6dcb9a51f1
commit 5c4772427a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 21 deletions

View File

@ -17,9 +17,10 @@ import StatusBar from '../containers/StatusBar';
import { themes } from '../constants/colors'; import { themes } from '../constants/colors';
import { withTheme } from '../theme'; import { withTheme } from '../theme';
import SafeAreaView from '../containers/SafeAreaView'; import SafeAreaView from '../containers/SafeAreaView';
import Loading from '../containers/Loading';
import { animateNextTransition } from '../utils/layoutAnimation'; import { animateNextTransition } from '../utils/layoutAnimation';
import { goRoom } from '../utils/goRoom'; import { goRoom } from '../utils/goRoom';
import Loading from '../containers/Loading'; import debounce from '../utils/debounce';
const QUERY_SIZE = 50; const QUERY_SIZE = 50;
@ -34,7 +35,7 @@ class AddExistingChannelView extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.init(); this.query();
this.teamId = props.route?.params?.teamId; this.teamId = props.route?.params?.teamId;
this.state = { this.state = {
search: [], search: [],
@ -66,7 +67,7 @@ class AddExistingChannelView extends React.Component {
navigation.setOptions(options); navigation.setOptions(options);
} }
init = async() => { query = async(stringToSearch = '') => {
try { try {
const { addTeamChannelPermission } = this.props; const { addTeamChannelPermission } = this.props;
const db = database.active; const db = database.active;
@ -75,39 +76,42 @@ class AddExistingChannelView extends React.Component {
.query( .query(
Q.where('team_id', ''), Q.where('team_id', ''),
Q.where('t', Q.oneOf(['c', 'p'])), Q.where('t', Q.oneOf(['c', 'p'])),
Q.where('name', Q.like(`%${ stringToSearch }%`)),
Q.experimentalTake(QUERY_SIZE), Q.experimentalTake(QUERY_SIZE),
Q.experimentalSortBy('room_updated_at', Q.desc) Q.experimentalSortBy('room_updated_at', Q.desc)
) )
.fetch(); .fetch();
const filteredChannels = channels.filter(async(channel) => {
const permissions = await RocketChat.hasPermission([addTeamChannelPermission], channel.rid); const asyncFilter = async(channelsArray) => {
if (!permissions[0]) { const results = await Promise.all(channelsArray.map(async(channel) => {
return; if (channel.prid) {
} return false;
return channel; }
}); const permissions = await RocketChat.hasPermission([addTeamChannelPermission], channel.rid);
this.setState({ channels: filteredChannels }); if (!permissions[0]) {
return false;
}
return true;
}));
return channelsArray.filter((_v, index) => results[index]);
};
const channelFiltered = await asyncFilter(channels);
this.setState({ channels: channelFiltered });
} catch (e) { } catch (e) {
log(e); log(e);
} }
} }
onSearchChangeText(text) { onSearchChangeText = debounce((text) => {
this.search(text); this.query(text);
} }, 300)
dismiss = () => { dismiss = () => {
const { navigation } = this.props; const { navigation } = this.props;
return navigation.pop(); return navigation.pop();
} }
search = async(text) => {
const result = await RocketChat.search({ text, filterUsers: false });
this.setState({
search: result
});
}
submit = async() => { submit = async() => {
const { selected } = this.state; const { selected } = this.state;
const { isMasterDetail } = this.props; const { isMasterDetail } = this.props;