index.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import React, { Component } from "react";
  2. import { NavLink } from "react-router-dom";
  3. import CustomInput from "components/CustomInput.jsx";
  4. import CustomMessages from "components/CustomMessages.jsx";
  5. import PropTypes from "prop-types";
  6. import { translate, Trans } from "react-i18next";
  7. import { connect } from "react-redux";
  8. import { actionCreators as homepageActionCreators } from "ducks/homepage";
  9. import { bindActionCreators } from "redux";
  10. import StationCard from "./StationCard";
  11. import io from "io";
  12. import config from "config";
  13. @connect(state => ({
  14. user: {
  15. loggedIn: state.session.get("loggedIn"),
  16. userId: state.session.get("userId"),
  17. role: state.session.get("role"),
  18. },
  19. officialStations: state.homepage.getIn(["stations", "official"]),
  20. communityStations: state.homepage.getIn(["stations", "community"]),
  21. }),
  22. (dispatch) => ({
  23. onStationIndex: bindActionCreators(homepageActionCreators.stationIndex, dispatch),
  24. onStationCreate: bindActionCreators(homepageActionCreators.stationCreate, dispatch),
  25. onStationRemove: bindActionCreators(homepageActionCreators.stationRemove, dispatch),
  26. onStationSongUpdate: bindActionCreators(homepageActionCreators.stationSongUpdate, dispatch),
  27. onStationUserCountUpdate: bindActionCreators(homepageActionCreators.stationUserCountUpdate, dispatch),
  28. }))
  29. @translate(["home", "createCommunityStation"], { wait: true })
  30. export default class Homepage extends Component {
  31. static propTypes = {
  32. t: PropTypes.func,
  33. };
  34. static defaultProps = {
  35. t: () => {},
  36. };
  37. constructor() {
  38. super();
  39. CustomInput.initialize(this);
  40. this.state = {
  41. createStation: {
  42. private: false,
  43. },
  44. };
  45. io.getSocket(socket => {
  46. socket.emit("stations.index", data => {
  47. if (data.status === "success") {
  48. console.log(data.stations)
  49. this.props.onStationIndex(data.stations);
  50. }
  51. });
  52. });
  53. }
  54. isOwner = (ownerId) => {
  55. if (this.props.loggedIn) {
  56. if (this.props.user.role === "admin") return true;
  57. if (this.props.user.userId === ownerId) return true;
  58. }
  59. return false;
  60. };
  61. /*listStations = (type) => {
  62. let stations = [];
  63. this.state.stations[type].forEach((station) => {
  64. stations.push(<StationCard station={ station }/>);
  65. });
  66. return stations;
  67. };*/
  68. togglePrivate = () => {
  69. this.setState({
  70. createStation: {
  71. private: !this.state.createStation.private,
  72. },
  73. });
  74. };
  75. createCommunity = () => {
  76. this.messages.clearErrorSuccess();
  77. if (CustomInput.hasInvalidInput(this.input)) {
  78. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  79. } else {
  80. io.getSocket(socket => {
  81. //TODO Add private value
  82. socket.emit("stations.create", {
  83. name: this.input.stationName.getValue(),
  84. type: "community",
  85. displayName: this.input.stationDisplayName.getValue(),
  86. description: this.input.stationDescription.getValue(),
  87. }, res => {
  88. if (res.status === "success") {
  89. location.href = "/community/" + this.input.stationName.getValue();//TODO Remove
  90. } else {
  91. this.messages.addError(res.message);
  92. }
  93. });
  94. });
  95. }
  96. };
  97. render() {
  98. const { t } = this.props;
  99. //TODO Make this not re-render a lot
  100. return (
  101. <main id="homepage">
  102. <h1>{ t("home:title") }</h1>
  103. <CustomMessages onRef={ ref => (this.messages = ref) } />
  104. <h2>{ t("home:officialStations") }</h2>
  105. <div className="official-stations stations">
  106. { this.props.officialStations.map((station) => {
  107. return <StationCard station={ station } />;
  108. }) }
  109. </div>
  110. <h2>{ t("home:communityStations") }</h2>
  111. <div className="community-stations stations">
  112. { (this.props.loggedIn) ? (
  113. <div className="station-card">
  114. <div className="station-media station-media-icon">
  115. <div className="create-station-header">
  116. <h3>Create community station</h3>
  117. </div>
  118. <i className="material-icons" title={ this.props.t("createCommunityStation:addCommunityStation") } onClick={ this.createCommunity }>add</i>
  119. </div>
  120. <div className="station-body">
  121. <CustomInput key="stationDisplayName" type="stationDisplayName" name="stationDisplayName" showLabel={ false } placeholder={ this.props.t("createCommunityStation:displayNameHere") } onRef={ ref => (this.input.stationDisplayName = ref) } />
  122. <CustomInput key="stationDescription" type="stationDescription" name="stationDescription" showLabel={ false } placeholder={ this.props.t("createCommunityStation:descriptionHere") } onRef={ ref => (this.input.stationDescription = ref) } />
  123. </div>
  124. <div className="station-footer">
  125. <div className="nameContainer">
  126. <span>musare.com/c/</span>
  127. <CustomInput key="stationName" type="stationName" name="stationName" showLabel={ false } placeholder={ this.props.t("createCommunityStation:nameHere") } onRef={ ref => (this.input.stationName = ref) } />
  128. </div>
  129. {(this.state.createStation.private) ? <i className="material-icons" title={ this.props.t("createCommunityStation:makeThisStationPublic") } onClick={ this.togglePrivate }>lock</i> : <i className="material-icons active" title={ this.props.t("createCommunityStation:makeThisStationPrivate") } onClick={ this.togglePrivate }>lock</i>}
  130. </div>
  131. </div>
  132. ) : null }
  133. { this.props.communityStations.map((station) => {
  134. return <StationCard station={ station } isOwner={ this.isOwner(station.ownerId) } key={ station.stationId }/>;
  135. }) }
  136. </div>
  137. </main>
  138. );
  139. }
  140. }