From c8b868054168ff3405685e6f1f46da50d9a09fd9 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Tue, 20 Apr 2021 19:07:33 -0400 Subject: [PATCH] Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView --- app/i18n/locales/en.json | 3 +- app/lib/rocketchat.js | 8 +++- app/reducers/createTeam.js | 36 +++++++++++++++ app/reducers/index.js | 2 + app/sagas/createTeam.js | 80 ++++++++++++++++++++++++++++++++++ app/sagas/index.js | 2 + app/views/CreateChannelView.js | 17 +++++--- 7 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 app/reducers/createTeam.js create mode 100644 app/sagas/createTeam.js diff --git a/app/i18n/locales/en.json b/app/i18n/locales/en.json index 635a7957b..f621d00a3 100644 --- a/app/i18n/locales/en.json +++ b/app/i18n/locales/en.json @@ -714,5 +714,6 @@ "Team_Name": "Team Name", "Private_Team": "Private Team", "Read_Only_Team": "Read Only Team", - "Broadcast_Team": "Broadcast Team" + "Broadcast_Team": "Broadcast Team", + "creating_team" : "creating team" } diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index bdff997ae..9dfa39857 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -728,9 +728,13 @@ const RocketChat = { prid, pmid, t_name, reply, users, encrypted }); }, - createTeam(name) { + createTeam({ + name, users, type, readOnly, broadcast, encrypted + }) { // RC 3.13.0 - return this.post('teams.create', name); + return this.post('teams.create', { + name, users, type, readOnly, broadcast, encrypted + }); }, joinRoom(roomId, joinCode, type) { // TODO: join code diff --git a/app/reducers/createTeam.js b/app/reducers/createTeam.js new file mode 100644 index 000000000..e962c6315 --- /dev/null +++ b/app/reducers/createTeam.js @@ -0,0 +1,36 @@ +import { CREATE_TEAM } from '../actions/actionsTypes'; + +const initialState = { + isFetching: false, + failure: false, + result: {}, + error: {} +}; + +export default function(state = initialState, action) { + switch (action.type) { + case CREATE_TEAM.REQUEST: + return { + ...state, + isFetching: true, + failure: false, + error: {} + }; + case CREATE_TEAM.SUCCESS: + return { + ...state, + isFetching: false, + failure: false, + result: action.data + }; + case CREATE_TEAM.FAILURE: + return { + ...state, + isFetching: false, + failure: true, + error: action.err + }; + default: + return state; + } +} diff --git a/app/reducers/index.js b/app/reducers/index.js index dfee5f3eb..aaad34fce 100644 --- a/app/reducers/index.js +++ b/app/reducers/index.js @@ -7,6 +7,7 @@ import rooms from './rooms'; import server from './server'; import selectedUsers from './selectedUsers'; import createChannel from './createChannel'; +import createTeam from './createTeam'; import app from './app'; import sortPreferences from './sortPreferences'; import share from './share'; @@ -29,6 +30,7 @@ export default combineReducers({ server, selectedUsers, createChannel, + createTeam, app, room, rooms, diff --git a/app/sagas/createTeam.js b/app/sagas/createTeam.js new file mode 100644 index 000000000..7da3e0394 --- /dev/null +++ b/app/sagas/createTeam.js @@ -0,0 +1,80 @@ +import { + select, put, call, take, takeLatest +} from 'redux-saga/effects'; +import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; + +import { CREATE_TEAM, LOGIN } from '../actions/actionsTypes'; +import { createTeamSuccess, createTeamFailure } from '../actions/createTeam'; +import { showErrorAlert } from '../utils/info'; +import RocketChat from '../lib/rocketchat'; +import Navigation from '../lib/Navigation'; +import database from '../lib/database'; +import I18n from '../i18n'; +import { logEvent, events } from '../utils/log'; +import { goRoom } from '../utils/goRoom'; + +const createTeam = function createTeam(data) { + return RocketChat.createTeam(data); +}; + +const handleRequest = function* handleRequest({ data }) { + try { + const auth = yield select(state => state.login.isAuthenticated); + if (!auth) { + yield take(LOGIN.SUCCESS); + } + + const { + type, readOnly, broadcast, encrypted + } = data; + + logEvent(events.CR_CREATE, { + type, + readOnly, + broadcast, + encrypted + }); + const sub = yield call(createTeam, data); + + try { + const db = database.active; + const subCollection = db.get('subscriptions'); + yield db.action(async() => { + await subCollection.create((s) => { + s._raw = sanitizedRaw({ id: sub.rid }, subCollection.schema); + Object.assign(s, sub); + }); + }); + } catch { + // do nothing + } + + yield put(createTeamSuccess(sub)); + } catch (err) { + logEvent(events[data.group ? 'SELECTED_USERS_CREATE_GROUP_F' : 'CR_CREATE_F']); + yield put(createTeamFailure(err)); + } +}; + +const handleSuccess = function* handleSuccess({ data }) { + const isMasterDetail = yield select(state => state.app.isMasterDetail); + if (isMasterDetail) { + Navigation.navigate('DrawerNavigator'); + } + goRoom({ item: data, isMasterDetail }); +}; + +const handleFailure = function handleFailure({ err }) { + setTimeout(() => { + const msg = err.reason || I18n.t('There_was_an_error_while_action', { action: I18n.t('creating_team') }); + showErrorAlert(msg); + }, 300); +}; + +const root = function* root() { + yield takeLatest(CREATE_TEAM.REQUEST, handleRequest); + yield takeLatest(CREATE_TEAM.SUCCESS, handleSuccess); + yield takeLatest(CREATE_TEAM.FAILURE, handleFailure); +}; + +export default root; diff --git a/app/sagas/index.js b/app/sagas/index.js index e499d74ec..b5787d424 100644 --- a/app/sagas/index.js +++ b/app/sagas/index.js @@ -5,6 +5,7 @@ import room from './room'; import messages from './messages'; import selectServer from './selectServer'; import createChannel from './createChannel'; +import createTeam from './createTeam'; import init from './init'; import state from './state'; import deepLinking from './deepLinking'; @@ -18,6 +19,7 @@ const root = function* root() { yield all([ init(), createChannel(), + createTeam(), rooms(), room(), login(), diff --git a/app/views/CreateChannelView.js b/app/views/CreateChannelView.js index 5674f1d5e..9a300b8af 100644 --- a/app/views/CreateChannelView.js +++ b/app/views/CreateChannelView.js @@ -376,13 +376,16 @@ class CreateChannelView extends React.Component { } } -const mapStateToProps = state => ({ - baseUrl: state.server.server, - isFetching: state.createChannel.isFetching, - encryptionEnabled: state.encryption.enabled, - users: state.selectedUsers.users, - user: getUserSelector(state) -}); +const mapStateToProps = (state, ownProps) => { + const { route } = ownProps; + return { + baseUrl: state.server.server, + isFetching: route?.params?.isTeam ? state.createTeam.isFetching : state.createChannel.isFetching, + encryptionEnabled: state.encryption.enabled, + users: state.selectedUsers.users, + user: getUserSelector(state) + }; +}; const mapDispatchToProps = dispatch => ({ createChannel: data => dispatch(createChannelRequestAction(data)),