From a177a3749b75d2f30c6bb6327f5f470dcf51317f Mon Sep 17 00:00:00 2001 From: Ishaan Shah Date: Sun, 12 Apr 2020 19:28:58 +0530 Subject: [PATCH 1/4] Fix ESLint errors --- .../webserver/static/js/src/FollowUsers.tsx | 149 +++++++++--------- .../webserver/static/js/src/RecentListens.tsx | 12 +- .../webserver/static/js/src/types.d.ts | 11 +- 3 files changed, 83 insertions(+), 89 deletions(-) diff --git a/listenbrainz/webserver/static/js/src/FollowUsers.tsx b/listenbrainz/webserver/static/js/src/FollowUsers.tsx index 389fb9f6b9..6091937923 100644 --- a/listenbrainz/webserver/static/js/src/FollowUsers.tsx +++ b/listenbrainz/webserver/static/js/src/FollowUsers.tsx @@ -1,7 +1,3 @@ -/* eslint-disable */ -// TODO: Make the code ESLint compliant -// TODO: Port to typescript - import { faPlusCircle, faSave, @@ -11,37 +7,40 @@ import { } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { IconProp } from "@fortawesome/fontawesome-svg-core"; import * as React from "react"; import { isNil as _isNil } from "lodash"; import { getArtistLink, getPlayButton, getTrackLink } from "./utils"; -import { IconProp } from "@fortawesome/fontawesome-svg-core"; type FollowUsersProps = { - followList?: Array, - saveUrl?: string, - listId: number, - listName: string, + followList?: Array; + saveUrl?: string; + listId?: number; + listName?: string; onUserListChange: (users: Array) => void; - creator: ListenBrainzUser, + creator: ListenBrainzUser; newAlert: ( alertType: AlertType, title: string, message?: string | JSX.Element - ) => void, - playingNow: FollowUsersPlayingNow, - playListen: (listen: Listen) => void, -} + ) => void; + playingNow: FollowUsersPlayingNow; + playListen: (listen: Listen) => void; +}; type FollowUsersState = { - users: Array, - saveUrl: string, - listId: number | null, - listName: string, -} + users: Array; + saveUrl: string; + listId: number | undefined; + listName: string | undefined; +}; -export default class FollowUsers extends React.Component { - private textInput = React.createRef(); - private nameInput = React.createRef(); +export default class FollowUsers extends React.Component< + FollowUsersProps, + FollowUsersState +> { + private textInput = React.createRef(); + private nameInput = React.createRef(); constructor(props: FollowUsersProps) { super(props); this.state = { @@ -59,10 +58,10 @@ export default class FollowUsers extends React.Component user === currentValue)) + currentValue === "" || + users.find((user: string) => user === currentValue) ) { return; } @@ -74,12 +73,11 @@ export default class FollowUsers extends React.Component { this.setState( @@ -88,30 +86,32 @@ export default class FollowUsers extends React.Component { - const {onUserListChange} = this.props; - const {users} = this.state; + const { onUserListChange } = this.props; + const { users } = this.state; onUserListChange(users); } ); - } + }; saveFollowList = (): void => { if (!this.nameInput) return; if (!this.nameInput.current) return; - const { listName , users, listId, saveUrl } = this.state; - const {creator, newAlert} = this.props; + const { listName, users, listId, saveUrl } = this.state; + const { creator, newAlert } = this.props; let finalListName = listName; - if (!_isNil(this.nameInput.current.value) && this.nameInput.current.value.length) { + if ( + !_isNil(this.nameInput.current.value) && + this.nameInput.current.value.length + ) { finalListName = this.nameInput.current.value; } - fetch(saveUrl, { method: "POST", body: JSON.stringify({ - users: users, + users, name: finalListName, id: listId, }), @@ -125,35 +125,38 @@ export default class FollowUsers extends React.Component { - console.debug(data); - this.setState( - { listId: data.list_id } - ); + this.setState({ listId: data.list_id }); }) .catch((error) => { - console.error(error); newAlert("danger", "Could not save list", error.message); }); - } + }; newFollowList = (event: React.MouseEvent) => { event.preventDefault(); - this.setState( - { - users: [], - listId: null, - listName: "", - } - ); + this.setState({ + users: [], + listId: undefined, + listName: "", + }); if (this.nameInput && this.nameInput.current) { this.nameInput.current.value = ""; } - } + }; - addFollowerOnClick = (event: React.MouseEvent) => { - event.preventDefault() + addFollowerOnClick = ( + event: React.MouseEvent + ) => { + event.preventDefault(); this.addUserToList(); - } + }; + + saveListOnClick = ( + event: React.MouseEvent + ) => { + event.preventDefault(); + this.saveFollowList(); + }; addFollowerOnEnter(event: React.KeyboardEvent) { event.preventDefault(); @@ -162,11 +165,6 @@ export default class FollowUsers extends React.Component) => { - event.preventDefault(); - this.saveFollowList() - } - saveListOnEnter(event: React.KeyboardEvent) { event.preventDefault(); if (event.key === "Enter") { @@ -175,10 +173,13 @@ export default class FollowUsers extends React.Component
@@ -225,7 +226,7 @@ export default class FollowUsers extends React.Component User Listening now - - + {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} + + {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} + - {this.state.users.map((user, index) => { + {users.map((user, index) => { return ( {user} - {this.props.playingNow[user] && ( + {playingNow[user] && ( <> - {getTrackLink(this.props.playingNow[user])} + {getTrackLink(playingNow[user])} {" "} - — {getArtistLink(this.props.playingNow[user])} + — {getArtistLink(playingNow[user])} )} - {this.props.playingNow[user] && + {playingNow[user] && getPlayButton( - this.props.playingNow[user], - this.props.playListen.bind( - this, - this.props.playingNow[user] - ) + playingNow[user], + playListen.bind(this, playingNow[user]) )} diff --git a/listenbrainz/webserver/static/js/src/RecentListens.tsx b/listenbrainz/webserver/static/js/src/RecentListens.tsx index 75bac1ed8a..05c46bd367 100644 --- a/listenbrainz/webserver/static/js/src/RecentListens.tsx +++ b/listenbrainz/webserver/static/js/src/RecentListens.tsx @@ -10,8 +10,7 @@ import * as ReactDOM from "react-dom"; import * as _ from "lodash"; import * as io from "socket.io-client"; import SpotifyPlayer from "./SpotifyPlayer"; -// @ts-ignore -import { FollowUsers } from "./follow-users"; +import FollowUsers from "./FollowUsers"; import APIService from "./APIService"; import { getArtistLink, @@ -37,7 +36,7 @@ export interface RecentListensProps { profileUrl?: string; saveUrl?: string; spotify: SpotifyUser; - user: User; + user: ListenBrainzUser; webSocketsServerUrl: string; } @@ -392,7 +391,7 @@ class RecentListens extends React.Component< User )} {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} - + @@ -406,11 +405,10 @@ class RecentListens extends React.Component< } return 0; }) - .map((listen, index) => { + .map((listen) => { return ( Date: Sun, 12 Apr 2020 20:39:25 +0530 Subject: [PATCH 2/4] Fix types --- listenbrainz/webserver/static/js/src/FollowUsers.tsx | 4 ++-- listenbrainz/webserver/static/js/src/RecentListens.tsx | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/listenbrainz/webserver/static/js/src/FollowUsers.tsx b/listenbrainz/webserver/static/js/src/FollowUsers.tsx index 6091937923..61e5aab5e5 100644 --- a/listenbrainz/webserver/static/js/src/FollowUsers.tsx +++ b/listenbrainz/webserver/static/js/src/FollowUsers.tsx @@ -17,10 +17,10 @@ type FollowUsersProps = { saveUrl?: string; listId?: number; listName?: string; - onUserListChange: (users: Array) => void; + onUserListChange: (users: Array, dontSendUpdate?: boolean) => void; creator: ListenBrainzUser; newAlert: ( - alertType: AlertType, + type: AlertType, title: string, message?: string | JSX.Element ) => void; diff --git a/listenbrainz/webserver/static/js/src/RecentListens.tsx b/listenbrainz/webserver/static/js/src/RecentListens.tsx index 05c46bd367..be0f9a6012 100644 --- a/listenbrainz/webserver/static/js/src/RecentListens.tsx +++ b/listenbrainz/webserver/static/js/src/RecentListens.tsx @@ -23,7 +23,7 @@ export interface RecentListensProps { apiUrl: string; artistCount?: number | null | undefined; followList?: string[]; - followListId?: string; + followListId?: number; followListName?: string; haveListenCount?: boolean; latestListenTs?: number; @@ -46,7 +46,7 @@ export interface RecentListensState { currentListen: Listen; direction: SpotifyPlayDirection; followList: Array; - listId: string; + listId?: number; listName: string; listens: Array; mode: "listens" | "follow" | "recent"; @@ -76,7 +76,7 @@ class RecentListens extends React.Component< playingNowByUser: {}, saveUrl: props.saveUrl || "", listName: props.followListName || "", - listId: props.followListId || "", + listId: props.followListId || undefined, direction: "down", }; @@ -121,7 +121,7 @@ class RecentListens extends React.Component< handleFollowUserListChange = ( userList: string[], - dontSendUpdate: boolean + dontSendUpdate?: boolean ): void => { const { mode } = this.state; const { user } = this.props; @@ -249,7 +249,7 @@ class RecentListens extends React.Component< newAlert = ( type: AlertType, title: string, - message: string | JSX.Element + message?: string | JSX.Element ): void => { const newAlert = { id: new Date().getTime(), From 5cdb98e14c3cad8692c1dd503ba39a7c28435e24 Mon Sep 17 00:00:00 2001 From: Ishaan Shah Date: Sun, 12 Apr 2020 20:57:52 +0530 Subject: [PATCH 3/4] Fix input problem --- listenbrainz/webserver/static/js/src/FollowUsers.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/listenbrainz/webserver/static/js/src/FollowUsers.tsx b/listenbrainz/webserver/static/js/src/FollowUsers.tsx index 61e5aab5e5..774154e3f3 100644 --- a/listenbrainz/webserver/static/js/src/FollowUsers.tsx +++ b/listenbrainz/webserver/static/js/src/FollowUsers.tsx @@ -39,8 +39,8 @@ export default class FollowUsers extends React.Component< FollowUsersProps, FollowUsersState > { - private textInput = React.createRef(); - private nameInput = React.createRef(); + textInput = React.createRef(); + nameInput = React.createRef(); constructor(props: FollowUsersProps) { super(props); this.state = { @@ -159,14 +159,12 @@ export default class FollowUsers extends React.Component< }; addFollowerOnEnter(event: React.KeyboardEvent) { - event.preventDefault(); if (event.key === "Enter") { this.addUserToList(); } } saveListOnEnter(event: React.KeyboardEvent) { - event.preventDefault(); if (event.key === "Enter") { this.saveFollowList(); } From e81c42b0576173f0fa1ddb7538a434be600b44db Mon Sep 17 00:00:00 2001 From: Ishaan Shah Date: Sun, 12 Apr 2020 21:30:50 +0530 Subject: [PATCH 4/4] Fix "Save List" --- listenbrainz/webserver/static/js/src/FollowUsers.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/listenbrainz/webserver/static/js/src/FollowUsers.tsx b/listenbrainz/webserver/static/js/src/FollowUsers.tsx index 774154e3f3..6ebcf07dd8 100644 --- a/listenbrainz/webserver/static/js/src/FollowUsers.tsx +++ b/listenbrainz/webserver/static/js/src/FollowUsers.tsx @@ -107,13 +107,12 @@ export default class FollowUsers extends React.Component< ) { finalListName = this.nameInput.current.value; } - fetch(saveUrl, { method: "POST", body: JSON.stringify({ users, name: finalListName, - id: listId, + id: listId === undefined ? listId : null, }), headers: { Authorization: `Token ${creator.auth_token}` }, }) @@ -121,7 +120,7 @@ export default class FollowUsers extends React.Component< if (!response.ok) { throw Error(response.statusText); } - newAlert("success", "Successfully saved list"); + newAlert("success", "Successfully saved list", "Success"); return response.json(); }) .then((data) => {