0
0
Fork 0

First iteration of playlist exporting -- functional

This commit is contained in:
Oliver-Akins 2020-08-15 01:43:31 -06:00
parent 0f42d6b458
commit 79e3b6c0c7

View file

@ -29,9 +29,20 @@
<option :value="false">Private Playlist</option> <option :value="false">Private Playlist</option>
<option :value="true">Public Playlist</option> <option :value="true">Public Playlist</option>
</select> </select>
<button>Create Playlist</button> <button
:disabled="!create_button_enabled"
@click.self="create_playlist()"
>
Create Playlist
</button>
</div> </div>
</div> </div>
<div
v-if="success"
class="alert success"
>
<a :href="playlist.url">Playlist</a> created successfully!
</div>
</div> </div>
</transition> </transition>
</div> </div>
@ -39,6 +50,8 @@
</template> </template>
<script> <script>
import * as axios from "axios";
export default { export default {
name: `PlaylistExportModal`, name: `PlaylistExportModal`,
props: { props: {
@ -50,13 +63,23 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
data: {
type: Array,
required: true,
}
}, },
data() {return { data() {return {
container: false, container: false,
content: false, content: false,
create_button_enabled: true,
name: ``, name: ``,
is_public: false, is_public: false,
description: `Playlist auto-generated by Top Lists for Spotify`, description: `Playlist auto-generated by Top Lists for Spotify`,
success: false,
playlist: {
id: null,
url: null,
},
}}, }},
mounted() { mounted() {
let date = new Date(); let date = new Date();
@ -70,7 +93,62 @@ export default {
}, },
methods: { methods: {
create_playlist() { create_playlist() {
this.create_button_enabled = false;
// Generate payload object
let payload = {
name: this.name,
description: this.description,
public: this.is_public
};
// Create the Spotify playlist (no tracks yet)
axios.post(
`${this.api_url}/users/${this.user_id}/playlists`,
JSON.stringify(payload),
{ headers: {
Authorization: `Bearer ${this.api_token}`,
"Content-Type": `application/json`
} }
)
.then(response => {
this.playlist.id = response.data.id;
this.playlist.url = response.data.external_urls.spotify;
this.populate_playlist()
})
.catch((err) => {
console.error(err)
window.location.hash = ``;
window.location.href = `${this.auth_redirect}?error=${encodeURI(err)}`;;
return
})
},
populate_playlist() {
// POST /playlists/{playlist_id}/tracks
let payload = { uris: [] };
for (var track of this.data) {
payload.uris.push(track.uri);
};
axios.post(
`${this.api_url}/playlists/${this.playlist.id}/tracks`,
payload,
{ headers: {
Authorization: `Bearer ${this.api_token}`,
"Content-Type": `application/json`
} }
)
.then(response => {
alert(`Playlist created successfully.`);
this.success = true;
})
.catch((err) => {
console.error(err)
window.location.hash = ``;
window.location.href = `${this.auth_redirect}?error=${encodeURI(err)}`;;
return;
})
}, },
}, },
} }