From 768c3c57e6e237456328bccdb0cbc2a6cb85cac6 Mon Sep 17 00:00:00 2001 From: "ug.rp" Date: Thu, 23 May 2024 09:04:38 +0200 Subject: [PATCH] fix "share discogs track to r4" --- src/components/r4-track-create.js | 4 ++-- src/libs/discogs.js | 15 +++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/components/r4-track-create.js b/src/components/r4-track-create.js index ba4e7bdd..f3b61734 100644 --- a/src/components/r4-track-create.js +++ b/src/components/r4-track-create.js @@ -80,15 +80,15 @@ export default class R4TrackCreate extends R4Form { if (!this.state.title) { const {title} = await fetchOEmbed(value) if (title) { - /* cannot this.setAttribute('title') from here */ + this.state = {...this.state, title} const $trackTitle = this.querySelector('[name="title"]') $trackTitle.value = title - $trackTitle.dispatchEvent(new Event('input')) // trigger value change } } } if (name === 'title' && value) { const search = buildSearchUrl(value) + console.log('search url', search) } if (name === 'discogs_url') { const $discogs = this.querySelector('r4-discogs-resource') diff --git a/src/libs/discogs.js b/src/libs/discogs.js index ed977ec7..92bd876e 100644 --- a/src/libs/discogs.js +++ b/src/libs/discogs.js @@ -20,14 +20,21 @@ const buildApiUrl = ({type, id}) => { return new URL(`${type}s/${id}`, `https://${DISCOGS_API_URL}`).href } -/* parses a discogs release URL */ +/* parses a discogs release URL; + Ex: + - https://www.discogs.com/release/13304754-%EB%B0%95%ED%98%9C%EC%A7%84-Park-Hye-Jin-If-U-Want-It (new) + - https://www.discogs.com/Diego-The-Persuasion-Channel/release/17335 (legacy) + */ export const parseUrl = (url) => { const discogsUrl = new URL(url) if (discogsUrl.hostname.endsWith(DISCOGS_URL)) { const pathes = discogsUrl.pathname.slice(1).split('/') - const type = pathes[0] - const id = pathes[1].split('-')[0] - if (DiscogsResourceTypes.includes(type)) { + const type = [pathes[0], pathes[1]].find((typeInPath) => { + return DiscogsResourceTypes.includes(typeInPath) + }) + if (type) { + const typeInPathIndex = pathes.indexOf(type) + const id = pathes.slice(typeInPathIndex + 1)[0].split('-')[0] return {id, type} } }