Discogs.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <template>
  2. <div class="discogs-tab">
  3. <div class="selected-discogs-info" v-if="!song.discogs">
  4. <p class="selected-discogs-info-none">None</p>
  5. </div>
  6. <div class="selected-discogs-info" v-if="song.discogs">
  7. <div class="top-container">
  8. <img :src="song.discogs.album.albumArt" />
  9. <div class="right-container">
  10. <p class="album-title">
  11. {{ song.discogs.album.title }}
  12. </p>
  13. <div class="bottom-row">
  14. <p class="type-year">
  15. <span>{{ song.discogs.album.type }}</span>
  16. <span>{{ song.discogs.album.year }}</span>
  17. </p>
  18. </div>
  19. </div>
  20. </div>
  21. <div class="bottom-container">
  22. <p class="bottom-container-field">
  23. Artists:
  24. <span>{{ song.discogs.album.artists.join(", ") }}</span>
  25. </p>
  26. <p class="bottom-container-field">
  27. Genres:
  28. <span>{{ song.discogs.album.genres.join(", ") }}</span>
  29. </p>
  30. <p class="bottom-container-field">
  31. Data quality:
  32. <span>{{ song.discogs.dataQuality }}</span>
  33. </p>
  34. <p class="bottom-container-field">
  35. Track:
  36. <span
  37. >{{ song.discogs.track.position }}.
  38. {{ song.discogs.track.title }}</span
  39. >
  40. </p>
  41. </div>
  42. </div>
  43. <label class="label"> Search for a song from Discogs </label>
  44. <div class="control is-grouped input-with-button">
  45. <p class="control is-expanded">
  46. <input
  47. class="input"
  48. type="text"
  49. placeholder="Enter your Discogs query here..."
  50. ref="discogs-input"
  51. v-model="discogsQuery"
  52. @keyup.enter="searchDiscogsForPage(1)"
  53. @change="onDiscogsQueryChange"
  54. v-focus
  55. />
  56. </p>
  57. <p class="control">
  58. <button class="button is-info" @click="searchDiscogsForPage(1)">
  59. <i class="material-icons icon-with-button">search</i>Search
  60. </button>
  61. </p>
  62. </div>
  63. <label class="label" v-if="discogs.apiResults.length > 0"
  64. >API results</label
  65. >
  66. <div class="api-results-container" v-if="discogs.apiResults.length > 0">
  67. <div
  68. class="api-result"
  69. v-for="(result, index) in discogs.apiResults"
  70. :key="result.album.id"
  71. tabindex="0"
  72. @keydown.space.prevent
  73. @keyup.enter="toggleAPIResult(index)"
  74. >
  75. <div class="top-container">
  76. <img :src="result.album.albumArt" />
  77. <div class="right-container">
  78. <p class="album-title">
  79. {{ result.album.title }}
  80. </p>
  81. <div class="bottom-row">
  82. <img
  83. src="/assets/arrow_up.svg"
  84. v-if="result.expanded"
  85. @click="toggleAPIResult(index)"
  86. />
  87. <img
  88. src="/assets/arrow_down.svg"
  89. v-if="!result.expanded"
  90. @click="toggleAPIResult(index)"
  91. />
  92. <p class="type-year">
  93. <span>{{ result.album.type }}</span>
  94. <span>{{ result.album.year }}</span>
  95. </p>
  96. </div>
  97. </div>
  98. </div>
  99. <div class="bottom-container" v-if="result.expanded">
  100. <p class="bottom-container-field">
  101. Artists:
  102. <span>{{ result.album.artists.join(", ") }}</span>
  103. </p>
  104. <p class="bottom-container-field">
  105. Genres:
  106. <span>{{ result.album.genres.join(", ") }}</span>
  107. </p>
  108. <p class="bottom-container-field">
  109. Data quality:
  110. <span>{{ result.dataQuality }}</span>
  111. </p>
  112. <button
  113. class="button is-primary"
  114. :disabled="bulk"
  115. @click="importAlbum(result)"
  116. >
  117. Import album
  118. </button>
  119. <div class="tracks">
  120. <div
  121. class="track"
  122. tabindex="0"
  123. v-for="(track, trackIndex) in result.tracks"
  124. :key="`${track.position}-${track.title}`"
  125. @click="selectTrack(index, trackIndex)"
  126. @keyup.enter="selectTrack(index, trackIndex)"
  127. >
  128. <span>{{ track.position }}.</span>
  129. <p>{{ track.title }}</p>
  130. </div>
  131. </div>
  132. </div>
  133. </div>
  134. <button
  135. v-if="
  136. discogs.apiResults.length > 0 &&
  137. !discogs.disableLoadMore &&
  138. discogs.page < discogs.pages
  139. "
  140. class="button is-fullwidth is-info discogs-load-more"
  141. @click="loadNextDiscogsPage()"
  142. >
  143. Load more...
  144. </button>
  145. </div>
  146. </div>
  147. </template>
  148. <script>
  149. import { mapState, mapGetters, mapActions } from "vuex";
  150. import Toast from "toasters";
  151. import keyboardShortcuts from "@/keyboardShortcuts";
  152. export default {
  153. props: {
  154. bulk: { type: Boolean, default: false }
  155. },
  156. data() {
  157. return {
  158. discogs: {
  159. apiResults: [],
  160. page: 1,
  161. pages: 1,
  162. disableLoadMore: false
  163. },
  164. discogsQuery: ""
  165. };
  166. },
  167. computed: {
  168. ...mapState("modals/editSong", {
  169. song: state => state.song
  170. }),
  171. ...mapGetters({
  172. socket: "websockets/getSocket"
  173. })
  174. },
  175. mounted() {
  176. this.discogsQuery = this.song.title;
  177. keyboardShortcuts.registerShortcut("editSong.focusDiscogs", {
  178. keyCode: 35,
  179. preventDefault: true,
  180. handler: () => {
  181. this.$refs["discogs-input"].focus();
  182. }
  183. });
  184. },
  185. methods: {
  186. toggleAPIResult(index) {
  187. const apiResult = this.discogs.apiResults[index];
  188. if (apiResult.expanded === true) apiResult.expanded = false;
  189. else if (apiResult.gotMoreInfo === true) apiResult.expanded = true;
  190. else {
  191. fetch(apiResult.album.resourceUrl)
  192. .then(response => response.json())
  193. .then(data => {
  194. apiResult.album.artists = [];
  195. apiResult.album.artistIds = [];
  196. const artistRegex = /\\([0-9]+\\)$/;
  197. apiResult.dataQuality = data.data_quality;
  198. data.artists.forEach(artist => {
  199. apiResult.album.artists.push(
  200. artist.name.replace(artistRegex, "")
  201. );
  202. apiResult.album.artistIds.push(artist.id);
  203. });
  204. apiResult.tracks = data.tracklist.map(track => ({
  205. position: track.position,
  206. title: track.title
  207. }));
  208. apiResult.expanded = true;
  209. apiResult.gotMoreInfo = true;
  210. });
  211. }
  212. },
  213. searchDiscogsForPage(page) {
  214. const query = this.discogsQuery;
  215. this.socket.dispatch("apis.searchDiscogs", query, page, res => {
  216. if (res.status === "success") {
  217. if (page === 1)
  218. new Toast(
  219. `Successfully searched. Got ${res.data.results.length} results.`
  220. );
  221. else
  222. new Toast(
  223. `Successfully got ${res.data.results.length} more results.`
  224. );
  225. if (page === 1) {
  226. this.discogs.apiResults = [];
  227. }
  228. this.discogs.pages = res.data.pages;
  229. this.discogs.apiResults = this.discogs.apiResults.concat(
  230. res.data.results.map(result => {
  231. const type =
  232. result.type.charAt(0).toUpperCase() +
  233. result.type.slice(1);
  234. return {
  235. expanded: false,
  236. gotMoreInfo: false,
  237. album: {
  238. id: result.id,
  239. title: result.title,
  240. type,
  241. year: result.year,
  242. genres: result.genre,
  243. albumArt: result.cover_image,
  244. resourceUrl: result.resource_url
  245. }
  246. };
  247. })
  248. );
  249. this.discogs.page = page;
  250. this.discogs.disableLoadMore = false;
  251. } else new Toast(res.message);
  252. });
  253. },
  254. loadNextDiscogsPage() {
  255. this.discogs.disableLoadMore = true;
  256. this.searchDiscogsForPage(this.discogs.page + 1);
  257. },
  258. onDiscogsQueryChange() {
  259. this.discogs.page = 1;
  260. this.discogs.pages = 1;
  261. this.discogs.apiResults = [];
  262. this.discogs.disableLoadMore = false;
  263. },
  264. selectTrack(apiResultIndex, trackIndex) {
  265. const apiResult = JSON.parse(
  266. JSON.stringify(this.discogs.apiResults[apiResultIndex])
  267. );
  268. apiResult.track = apiResult.tracks[trackIndex];
  269. delete apiResult.tracks;
  270. delete apiResult.expanded;
  271. delete apiResult.gotMoreInfo;
  272. this.selectDiscogsInfo(apiResult);
  273. },
  274. ...mapActions("modals/editSong", ["selectDiscogsInfo"])
  275. }
  276. };
  277. </script>
  278. <style lang="less" scoped>
  279. .night-mode {
  280. .api-section,
  281. .api-result {
  282. background-color: var(--dark-grey-3) !important;
  283. }
  284. .api-result .tracks .track:hover,
  285. .api-result .tracks .track:focus,
  286. .selected-discogs-info {
  287. background-color: var(--dark-grey-2) !important;
  288. }
  289. .label,
  290. p,
  291. strong {
  292. color: var(--light-grey-2);
  293. }
  294. .discogs-tab .top-container .right-container .bottom-row img {
  295. filter: invert(100%);
  296. }
  297. }
  298. .discogs-tab {
  299. > label {
  300. margin-top: 12px;
  301. }
  302. .top-container {
  303. display: flex;
  304. img {
  305. height: 85px;
  306. width: 85px;
  307. }
  308. .right-container {
  309. padding: 8px;
  310. display: flex;
  311. flex-direction: column;
  312. flex: 1;
  313. .album-title {
  314. flex: 1;
  315. font-weight: 600;
  316. }
  317. .bottom-row {
  318. display: flex;
  319. flex-flow: row;
  320. line-height: 15px;
  321. img {
  322. height: 15px;
  323. align-self: end;
  324. flex: 1;
  325. user-select: none;
  326. -moz-user-select: none;
  327. -ms-user-select: none;
  328. -webkit-user-select: none;
  329. cursor: pointer;
  330. }
  331. p {
  332. text-align: right;
  333. }
  334. .type-year {
  335. font-size: 13px;
  336. align-self: end;
  337. }
  338. }
  339. }
  340. }
  341. .bottom-container {
  342. padding: 12px;
  343. .bottom-container-field {
  344. line-height: 16px;
  345. margin-bottom: 8px;
  346. font-weight: 600;
  347. span {
  348. font-weight: 400;
  349. }
  350. }
  351. .bottom-container-field:last-of-type {
  352. margin-bottom: 8px;
  353. }
  354. }
  355. .selected-discogs-info {
  356. background-color: var(--white);
  357. border: 1px solid var(--light-grey-3);
  358. border-radius: @border-radius;
  359. margin-bottom: 16px;
  360. .selected-discogs-info-none {
  361. font-size: 18px;
  362. text-align: center;
  363. }
  364. .bottom-row > p {
  365. flex: 1;
  366. }
  367. }
  368. .api-results-container {
  369. .api-result {
  370. background-color: var(--white);
  371. border: 0.5px solid var(--light-grey-3);
  372. border-radius: @border-radius;
  373. margin-bottom: 16px;
  374. }
  375. }
  376. button {
  377. background-color: var(--primary-color) !important;
  378. &:focus,
  379. &:hover {
  380. filter: contrast(0.75);
  381. }
  382. }
  383. .tracks {
  384. margin-top: 12px;
  385. .track:first-child {
  386. margin-top: 0;
  387. border-radius: @border-radius @border-radius 0 0;
  388. }
  389. .track:last-child {
  390. border-radius: 0 0 @border-radius @border-radius;
  391. }
  392. .track {
  393. border: 0.5px solid var(--black);
  394. margin-top: -1px;
  395. line-height: 16px;
  396. display: flex;
  397. cursor: pointer;
  398. span {
  399. font-weight: 600;
  400. display: inline-block;
  401. margin-top: 7px;
  402. margin-bottom: 7px;
  403. margin-left: 7px;
  404. }
  405. p {
  406. display: inline-block;
  407. margin: 7px;
  408. flex: 1;
  409. }
  410. }
  411. .track:hover,
  412. .track:focus {
  413. background-color: var(--light-grey);
  414. }
  415. }
  416. .discogs-load-more {
  417. margin-bottom: 8px;
  418. }
  419. }
  420. </style>