You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractAppSettings.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // @flow
  2. import { Component } from 'react';
  3. import { hideAppSettings } from '../actions';
  4. import { getProfile, updateProfile } from '../../base/profile';
  5. /**
  6. * The type of the React {@code Component} props of {@link AbstractAppSettings}
  7. */
  8. type Props = {
  9. /**
  10. * The current profile object.
  11. */
  12. _profile: Object,
  13. /**
  14. * The visibility prop of the settings modal.
  15. */
  16. _visible: boolean,
  17. /**
  18. * Redux store dispatch function.
  19. */
  20. dispatch: Dispatch<*>
  21. };
  22. /**
  23. * The type of the React {@code Component} state of {@link AbstractAppSettings}.
  24. */
  25. type State = {
  26. /**
  27. * The display name field value on the settings screen.
  28. */
  29. displayName: string,
  30. /**
  31. * The email field value on the settings screen.
  32. */
  33. email: string,
  34. /**
  35. * The server url field value on the settings screen.
  36. */
  37. serverURL: string,
  38. /**
  39. * The start audio muted switch value on the settings screen.
  40. */
  41. startWithAudioMuted: boolean,
  42. /**
  43. * The start video muted switch value on the settings screen.
  44. */
  45. startWithVideoMuted: boolean
  46. }
  47. /**
  48. * Base (abstract) class for container component rendering
  49. * the app settings page.
  50. *
  51. * @abstract
  52. */
  53. export class AbstractAppSettings extends Component<Props, State> {
  54. /**
  55. * Initializes a new {@code AbstractAppSettings} instance.
  56. *
  57. * @param {Props} props - The React {@code Component} props to initialize
  58. * the component.
  59. */
  60. constructor(props: Props) {
  61. super(props);
  62. this._onChangeDisplayName = this._onChangeDisplayName.bind(this);
  63. this._onChangeEmail = this._onChangeEmail.bind(this);
  64. this._onChangeServerName = this._onChangeServerName.bind(this);
  65. this._onRequestClose = this._onRequestClose.bind(this);
  66. this._onSaveDisplayName = this._onSaveDisplayName.bind(this);
  67. this._onSaveEmail = this._onSaveEmail.bind(this);
  68. this._onSaveServerName = this._onSaveServerName.bind(this);
  69. this._onStartAudioMutedChange
  70. = this._onStartAudioMutedChange.bind(this);
  71. this._onStartVideoMutedChange
  72. = this._onStartVideoMutedChange.bind(this);
  73. }
  74. /**
  75. * Invokes React's {@link Component#componentWillReceiveProps()} to make
  76. * sure we have the state Initialized on component mount.
  77. *
  78. * @inheritdoc
  79. */
  80. componentWillMount() {
  81. this._updateStateFromProps(this.props);
  82. }
  83. /**
  84. * Implements React's {@link Component#componentWillReceiveProps()}. Invoked
  85. * before this mounted component receives new props.
  86. *
  87. * @inheritdoc
  88. * @param {Props} nextProps - New props component will receive.
  89. */
  90. componentWillReceiveProps(nextProps: Props) {
  91. this._updateStateFromProps(nextProps);
  92. }
  93. _onChangeDisplayName: (string) => void;
  94. /**
  95. * Handles the display name field value change.
  96. *
  97. * @protected
  98. * @param {string} text - The value typed in the name field.
  99. * @returns {void}
  100. */
  101. _onChangeDisplayName(text) {
  102. this.setState({
  103. displayName: text
  104. });
  105. }
  106. _onChangeEmail: (string) => void;
  107. /**
  108. * Handles the email field value change.
  109. *
  110. * @protected
  111. * @param {string} text - The value typed in the email field.
  112. * @returns {void}
  113. */
  114. _onChangeEmail(text) {
  115. this.setState({
  116. email: text
  117. });
  118. }
  119. _onChangeServerName: (string) => void;
  120. /**
  121. * Handles the server name field value change.
  122. *
  123. * @protected
  124. * @param {string} text - The server URL typed in the server field.
  125. * @returns {void}
  126. */
  127. _onChangeServerName(text) {
  128. this.setState({
  129. serverURL: text
  130. });
  131. }
  132. _onRequestClose: () => void;
  133. /**
  134. * Handles the hardware back button.
  135. *
  136. * @returns {void}
  137. */
  138. _onRequestClose() {
  139. this.props.dispatch(hideAppSettings());
  140. }
  141. _onSaveDisplayName: () => void;
  142. /**
  143. * Handles the display name field onEndEditing.
  144. *
  145. * @protected
  146. * @returns {void}
  147. */
  148. _onSaveDisplayName() {
  149. this._updateProfile({
  150. displayName: this.state.displayName
  151. });
  152. }
  153. _onSaveEmail: () => void;
  154. /**
  155. * Handles the email field onEndEditing.
  156. *
  157. * @protected
  158. * @returns {void}
  159. */
  160. _onSaveEmail() {
  161. this._updateProfile({
  162. email: this.state.email
  163. });
  164. }
  165. _onSaveServerName: () => void;
  166. /**
  167. * Handles the server name field onEndEditing.
  168. *
  169. * @protected
  170. * @returns {void}
  171. */
  172. _onSaveServerName() {
  173. let serverURL;
  174. if (this.state.serverURL.endsWith('/')) {
  175. serverURL = this.state.serverURL.substr(
  176. 0, this.state.serverURL.length - 1
  177. );
  178. } else {
  179. serverURL = this.state.serverURL;
  180. }
  181. this._updateProfile({
  182. defaultURL: serverURL
  183. });
  184. this.setState({
  185. serverURL
  186. });
  187. }
  188. _onStartAudioMutedChange: (boolean) => void;
  189. /**
  190. * Handles the start audio muted change event.
  191. *
  192. * @protected
  193. * @param {boolean} newValue - The new value for the
  194. * start audio muted option.
  195. * @returns {void}
  196. */
  197. _onStartAudioMutedChange(newValue) {
  198. this.setState({
  199. startWithAudioMuted: newValue
  200. });
  201. this._updateProfile({
  202. startWithAudioMuted: newValue
  203. });
  204. }
  205. _onStartVideoMutedChange: (boolean) => void;
  206. /**
  207. * Handles the start video muted change event.
  208. *
  209. * @protected
  210. * @param {boolean} newValue - The new value for the
  211. * start video muted option.
  212. * @returns {void}
  213. */
  214. _onStartVideoMutedChange(newValue) {
  215. this.setState({
  216. startWithVideoMuted: newValue
  217. });
  218. this._updateProfile({
  219. startWithVideoMuted: newValue
  220. });
  221. }
  222. _updateProfile: (Object) => void;
  223. /**
  224. * Updates the persisted profile on any change.
  225. *
  226. * @private
  227. * @param {Object} updateObject - The partial update object for the profile.
  228. * @returns {void}
  229. */
  230. _updateProfile(updateObject: Object) {
  231. this.props.dispatch(updateProfile({
  232. ...this.props._profile,
  233. ...updateObject
  234. }));
  235. }
  236. _updateStateFromProps: (Object) => void;
  237. /**
  238. * Updates the component state when (new) props are received.
  239. *
  240. * @private
  241. * @param {Object} props - The component's props.
  242. * @returns {void}
  243. */
  244. _updateStateFromProps(props) {
  245. this.setState({
  246. displayName: props._profile.displayName,
  247. email: props._profile.email,
  248. serverURL: props._profile.defaultURL,
  249. startWithAudioMuted: props._profile.startWithAudioMuted,
  250. startWithVideoMuted: props._profile.startWithVideoMuted
  251. });
  252. }
  253. }
  254. /**
  255. * Maps (parts of) the redux state to the React {@code Component} props of
  256. * {@code AbstractAppSettings}.
  257. *
  258. * @param {Object} state - The redux state.
  259. * @protected
  260. * @returns {Object}
  261. */
  262. export function _mapStateToProps(state: Object) {
  263. return {
  264. _profile: getProfile(state),
  265. _visible: state['features/app-settings'].visible
  266. };
  267. }