From a34b1afd6cd9594cef6b5f422cadcde4ea925a70 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Mon, 19 Mar 2018 17:38:25 +0100
Subject: [PATCH] Store for fetching user playlists

---
 front/src/store/auth.js                       |  1 +
 front/src/store/index.js                      |  2 ++
 front/src/store/playlists.js                  | 24 +++++++++++++
 front/test/unit/specs/store/auth.spec.js      |  3 +-
 front/test/unit/specs/store/playlists.spec.js | 36 +++++++++++++++++++
 5 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 front/src/store/playlists.js
 create mode 100644 front/test/unit/specs/store/playlists.spec.js

diff --git a/front/src/store/auth.js b/front/src/store/auth.js
index 7944cae0..e72e1968 100644
--- a/front/src/store/auth.js
+++ b/front/src/store/auth.js
@@ -91,6 +91,7 @@ export default {
         commit('profile', data)
         commit('username', data.username)
         dispatch('favorites/fetch', null, {root: true})
+        dispatch('playlists/fetchOwn', null, {root: true})
         Object.keys(data.permissions).forEach(function (key) {
           // this makes it easier to check for permissions in templates
           commit('permission', {key, status: data.permissions[String(key)].status})
diff --git a/front/src/store/index.js b/front/src/store/index.js
index 2453c0e7..298fa04e 100644
--- a/front/src/store/index.js
+++ b/front/src/store/index.js
@@ -8,6 +8,7 @@ import instance from './instance'
 import queue from './queue'
 import radios from './radios'
 import player from './player'
+import playlists from './playlists'
 import ui from './ui'
 
 Vue.use(Vuex)
@@ -20,6 +21,7 @@ export default new Vuex.Store({
     instance,
     queue,
     radios,
+    playlists,
     player
   },
   plugins: [
diff --git a/front/src/store/playlists.js b/front/src/store/playlists.js
new file mode 100644
index 00000000..75cac87b
--- /dev/null
+++ b/front/src/store/playlists.js
@@ -0,0 +1,24 @@
+import axios from 'axios'
+
+export default {
+  namespaced: true,
+  state: {
+    playlists: []
+  },
+  mutations: {
+    playlists (state, value) {
+      state.playlists = value
+    }
+  },
+  actions: {
+    fetchOwn ({commit, rootState}) {
+      let userId = rootState.auth.profile.id
+      if (!userId) {
+        return
+      }
+      return axios.get('playlists/', {params: {user: userId}}).then((response) => {
+        commit('playlists', response.data.results)
+      })
+    }
+  }
+}
diff --git a/front/test/unit/specs/store/auth.spec.js b/front/test/unit/specs/store/auth.spec.js
index 3271f516..518dc10d 100644
--- a/front/test/unit/specs/store/auth.spec.js
+++ b/front/test/unit/specs/store/auth.spec.js
@@ -180,7 +180,8 @@ describe('store/auth', () => {
           { type: 'permission', payload: {key: 'admin', status: true} }
         ],
         expectedActions: [
-          { type: 'favorites/fetch', payload: null, options: {root: true} }
+          { type: 'favorites/fetch', payload: null, options: {root: true} },
+          { type: 'playlists/fetchOwn', payload: null, options: {root: true} },
         ]
       }, done)
     })
diff --git a/front/test/unit/specs/store/playlists.spec.js b/front/test/unit/specs/store/playlists.spec.js
new file mode 100644
index 00000000..e82af60b
--- /dev/null
+++ b/front/test/unit/specs/store/playlists.spec.js
@@ -0,0 +1,36 @@
+var sinon = require('sinon')
+import moxios from 'moxios'
+import store from '@/store/playlists'
+
+import { testAction } from '../../utils'
+
+describe('store/playlists', () => {
+  var sandbox
+
+  beforeEach(function () {
+    sandbox = sinon.sandbox.create()
+    moxios.install()
+  })
+  afterEach(function () {
+    sandbox.restore()
+    moxios.uninstall()
+  })
+
+  describe('mutations', () => {
+    it('set playlists', () => {
+      const state = { playlists: [] }
+      store.mutations.playlists(state, [{id: 1, name: 'test'}])
+      expect(state.playlists).to.deep.equal([{id: 1, name: 'test'}])
+    })
+  })
+  describe('actions', () => {
+    it('fetchOwn does nothing with no user', (done) => {
+      testAction({
+        action: store.actions.fetchOwn,
+        payload: null,
+        params: {state: { playlists: [] }, rootState: {auth: {profile: {}}}},
+        expectedMutations: []
+      }, done)
+    })
+  })
+})
-- 
GitLab