import React, { Component } from "react"; import { NavLink } from "react-router-dom"; import CustomInput from "components/CustomInput.jsx"; import CustomMessages from "components/CustomMessages.jsx"; import PropTypes from "prop-types"; import { translate, Trans } from "react-i18next"; import { connect } from "react-redux"; import io from "io"; import config from "config"; @connect(state => ({ user: { userId: state.user.get("userId"), role: state.user.get("role"), }, loggedIn: state.user.get("loggedIn"), })) @translate(["home", "createCommunityStation"], { wait: true }) export default class Homepage extends Component { static propTypes = { t: PropTypes.func, }; static defaultProps = { t: () => {}, }; constructor() { super(); CustomInput.initialize(this); this.state = { stations: { official: [], community: [], }, createStation: { private: false, } }; io.getSocket(socket => { socket.emit("stations.index", data => { if (data.status === "success") { let community = []; let official = []; data.stations.forEach(station => { if (!station.currentSong) station.currentSong = { thumbnail: '/assets/images/notes-transparent.png' }; if (station.currentSong && !station.currentSong.thumbnail) station.currentSong.thumbnail = "/assets/images/notes-transparent.png"; if (station.type === 'official') official.push(station); else community.push(station); }); this.setState({ stations: { official, community, } }); } }); }); } isOwner = (ownerId) => { if (this.props.loggedIn) { if (this.props.user.role === "admin") return true; if (this.props.user.userId === ownerId) return true; } return false; }; listStations = (type) => { let stations = []; this.state.stations[type].forEach((station) => { let icon = null; if (station.type === "official") { if (station.privacy !== "public") icon = lock; } else { // TODO Add isOwner function globally if (this.isOwner(station.ownerId)) icon = home; if (station.privacy !== "public") icon = lock; } stations.push( (

{station.displayName}

{station.description}

people {station.userCount}
{ icon }
) ); }); return stations; }; togglePrivate = () => { this.setState({ createStation: { private: !this.state.createStation.private, }, }); }; createCommunity = () => { this.messages.clearErrorSuccess(); if (CustomInput.hasInvalidInput(this.input)) { this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError")); } else { io.getSocket(socket => { //TODO Add private value socket.emit("stations.create", { name: this.input.stationName.getValue(), type: "community", displayName: this.input.stationDisplayName.getValue(), description: this.input.stationDescription.getValue(), }, res => { if (res.status === "success") { location.href = "/community/" + this.input.stationName.getValue();//TODO Remove } else { this.messages.addError(res.message); } }); }); } }; render() { const { t } = this.props; //TODO Make this not re-render a lot return (

{ t("home:title") }

(this.messages = ref) } />

{ t("home:officialStations") }

{ this.listStations("official") }

{ t("home:communityStations") }

{ (this.props.loggedIn) ? (

Create community station

add
(this.input.stationDisplayName = ref) } /> (this.input.stationDescription = ref) } />
musare.com/c/ (this.input.stationName = ref) } />
{(this.state.createStation.private) ? lock : lock}
) : null } { this.listStations("community") }
); } }