import React from "react-native";
import RoomTitle from "../components/room-title";
import controller from "./controller";

const {
	InteractionManager
} = React;

@controller
export default class RoomTitleController extends React.Component {
	constructor(props) {
		super(props);

		const displayName = this.props.room.replace(/-+/g, " ").replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.slice(1)).trim();

		this.state = {
			room: { displayName }
		};
	}

	componentDidMount() {
		setTimeout(() => this._onDataArrived(this.store.getRoomById(this.props.room)), 0);
	}

	_onDataArrived(room) {
		InteractionManager.runAfterInteractions(() => {
			if (this._mounted) {
				this.setState({ room });
			}
		});
	}

	render() {
		return <RoomTitle {...this.state} />;
	}
}

RoomTitleController.propTypes = {
	room: React.PropTypes.string.isRequired
};

import React from "react-native";
import RoomTitle from "../components/room-title";
import controller from "./controller";

const {
	InteractionManager
} = React;

@controller
export default class RoomTitleController extends React.Component {
	constructor(props) {
		super(props);

		const displayName = this.props.room.replace(/-+/g, " ").replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.slice(1)).trim();

		this.state = {
			room: { displayName }
		};
	}

	componentDidMount() {
		this._updateData();

		this.handle("statechange", changes => {
			if (changes.entities && changes.entities[this.props.room]) {
				this._updateData();
			}
		});
	}

	_updateData() {
		InteractionManager.runAfterInteractions(() => {
			if (this._mounted) {
				const room = this.store.getRoom(this.props.room);

				if (room.displayName) {
					this.setState({ room });
				}
			}
		});
	}

	render() {
		return <RoomTitle {...this.state} />;
	}
}

RoomTitleController.propTypes = {
	room: React.PropTypes.string.isRequired
};

Update room title on store change
Update room title on store change
