index.jsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. this.props.onStationIndex(data.stations);
  49. }
  50. });
  51. });
  52. }
  53. isOwner = (ownerId) => {
  54. if (this.props.loggedIn) {
  55. if (this.props.user.role === "admin") return true;
  56. if (this.props.user.userId === ownerId) return true;
  57. }
  58. return false;
  59. };
  60. /*listStations = (type) => {
  61. let stations = [];
  62. this.state.stations[type].forEach((station) => {
  63. stations.push(<StationCard station={ station }/>);
  64. });
  65. return stations;
  66. };*/
  67. togglePrivate = () => {
  68. this.setState({
  69. createStation: {
  70. private: !this.state.createStation.private,
  71. },
  72. });
  73. };
  74. createCommunity = () => {
  75. this.messages.clearErrorSuccess();
  76. if (CustomInput.hasInvalidInput(this.input)) {
  77. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  78. } else {
  79. io.getSocket(socket => {
  80. //TODO Add private value
  81. socket.emit("stations.create", {
  82. name: this.input.stationName.getValue(),
  83. type: "community",
  84. displayName: this.input.stationDisplayName.getValue(),
  85. description: this.input.stationDescription.getValue(),
  86. }, res => {
  87. if (res.status === "success") {
  88. location.href = "/community/" + this.input.stationName.getValue();//TODO Remove
  89. } else {
  90. this.messages.addError(res.message);
  91. }
  92. });
  93. });
  94. }
  95. };
  96. render() {
  97. const { t } = this.props;
  98. //TODO Make this not re-render a lot
  99. return (
  100. <main id="homepage">
  101. <h1>{ t("home:title") }</h1>
  102. <CustomMessages onRef={ ref => (this.messages = ref) } />
  103. <h2>{ t("home:officialStations") }</h2>
  104. <div className="official-stations stations">
  105. { this.props.officialStations.map((station) => {
  106. return <StationCard station={ station } key={ station.get("stationId") } />;
  107. }) }
  108. </div>
  109. <h2>{ t("home:communityStations") }</h2>
  110. <div className="community-stations stations">
  111. { (this.props.user.loggedIn) ? (
  112. <div className="station-card">
  113. <div className="station-media station-media-icon">
  114. <div className="create-station-header">
  115. <h3>Create community station</h3>
  116. </div>
  117. <i className="material-icons" title={ this.props.t("createCommunityStation:addCommunityStation") } onClick={ this.createCommunity }>add</i>
  118. </div>
  119. <div className="station-body">
  120. <CustomInput key="stationDisplayName" type="stationDisplayName" name="stationDisplayName" showLabel={ false } placeholder={ this.props.t("createCommunityStation:displayNameHere") } onRef={ ref => (this.input.stationDisplayName = ref) } />
  121. <CustomInput key="stationDescription" type="stationDescription" name="stationDescription" showLabel={ false } placeholder={ this.props.t("createCommunityStation:descriptionHere") } onRef={ ref => (this.input.stationDescription = ref) } />
  122. </div>
  123. <div className="station-footer">
  124. <div className="nameContainer">
  125. <span>musare.com/c/</span>
  126. <CustomInput key="stationName" type="stationName" name="stationName" showLabel={ false } placeholder={ this.props.t("createCommunityStation:nameHere") } onRef={ ref => (this.input.stationName = ref) } />
  127. </div>
  128. {(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>}
  129. </div>
  130. </div>
  131. ) : null }
  132. { this.props.communityStations.map((station) => {
  133. return <StationCard station={ station } key={ station.get("stationId") }/>;
  134. }) }
  135. </div>
  136. </main>
  137. );
  138. }
  139. }