Settings.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import CustomInput from "components/CustomInput.jsx";
  4. import CustomErrors from "components/CustomMessages.jsx";
  5. import { connect } from "react-redux";
  6. import { closeOverlay1 } from "actions/stationOverlay";
  7. import io from "io";
  8. @connect(state => ({
  9. stationId: state.station.get("id"),
  10. name: state.station.get("name"),
  11. displayName: state.station.get("displayName"),
  12. description: state.station.get("description"),
  13. privacy: state.station.get("privacy"),
  14. }))
  15. export default class Settings extends Component {
  16. constructor(props) {
  17. super(props);
  18. CustomInput.initialize(this);
  19. this.state = {
  20. privacy: props.privacy,
  21. };
  22. }
  23. close = () => {
  24. this.props.dispatch(closeOverlay1());
  25. };
  26. changeName = () => {
  27. this.messages.clearErrorSuccess();
  28. if (CustomInput.hasInvalidInput(this.input, ["name"])) {
  29. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  30. } else {
  31. io.getSocket(socket => {
  32. socket.emit("stations.updateName", this.props.stationId, this.input.name.getValue(), res => {
  33. if (res.status === "success") {
  34. this.messages.clearAddSuccess("Successfully changed name.");
  35. } else {
  36. this.messages.addError(res.message);
  37. }
  38. });
  39. });
  40. }
  41. };
  42. changeDisplayName = () => {
  43. this.messages.clearErrorSuccess();
  44. if (CustomInput.hasInvalidInput(this.input, ["displayName"])) {
  45. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  46. } else {
  47. io.getSocket(socket => {
  48. socket.emit("stations.updateDisplayName", this.props.stationId, this.input.displayName.getValue(), res => {
  49. if (res.status === "success") {
  50. this.messages.clearAddSuccess("Successfully changed display name.");
  51. } else {
  52. this.messages.addError(res.message);
  53. }
  54. });
  55. });
  56. }
  57. };
  58. changeDescription = () => {
  59. this.messages.clearErrorSuccess();
  60. if (CustomInput.hasInvalidInput(this.input, ["description"])) {
  61. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  62. } else {
  63. io.getSocket(socket => {
  64. socket.emit("stations.updateDescription", this.props.stationId, this.input.description.getValue(), res => {
  65. if (res.status === "success") {
  66. this.messages.clearAddSuccess("Successfully changed description.");
  67. } else {
  68. this.messages.addError(res.message);
  69. }
  70. });
  71. });
  72. }
  73. };
  74. savePrivacy = () => {
  75. this.messages.clearErrorSuccess();
  76. if (CustomInput.hasInvalidInput(this.input, ["privacy"])) {
  77. console.log(this.props);
  78. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  79. } else {
  80. io.getSocket(socket => {
  81. socket.emit("stations.updatePrivacy", this.props.stationId, this.input.privacy.getValue(), res => {
  82. if (res.status === "success") {
  83. this.messages.clearAddSuccess("Successfully saved privacy.");
  84. } else {
  85. this.messages.addError(res.message);
  86. }
  87. });
  88. });
  89. }
  90. };
  91. deleteStation = () => {
  92. io.getSocket(socket => {
  93. socket.emit('stations.remove', this.props.stationId, res => {
  94. if (res.status === "success") {
  95. this.messages.clearAddSuccess("Successfully deleted station.");
  96. } else {
  97. this.messages.addError(res.message);
  98. }
  99. });
  100. });
  101. };
  102. render() {
  103. return (
  104. <div className="overlay">
  105. <button onClick={ this.close }>Back</button>
  106. <h1>Settings</h1>
  107. <CustomErrors onRef={ ref => (this.messages = ref) } />
  108. <CustomInput key="name" type="stationName" name="name" label="Station name" placeholder="Station name" original={ this.props.name } onRef={ ref => (this.input.name = ref) } />
  109. <button onClick={ this.changeName }>Change name</button>
  110. <CustomInput key="displayName" type="stationDisplayName" name="displayName" label="Station display name" placeholder="Station display name" original={ this.props.displayName } onRef={ ref => (this.input.displayName = ref) } />
  111. <button onClick={ this.changeDisplayName }>Change display name</button>
  112. <CustomInput key="description" type="stationDescription" name="description" label="Station description" placeholder="Station description" original={ this.props.description } onRef={ ref => (this.input.description = ref) } />
  113. <button onClick={ this.changeDescription }>Change description</button>
  114. <CustomInput key="privacy" type="stationPrivacy" name="privacy" label="Station privacy" placeholder="Station privacy" original={ this.state.privacy } onRef={ ref => (this.input.privacy = ref) } />
  115. <button onClick={ this.savePrivacy }>Save privacy</button>
  116. <button onClick={ this.deleteStation }>Delete station</button>
  117. </div>
  118. );
  119. }
  120. }