Skip to content

Commit

Permalink
fix: store and api
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jan 13, 2022
1 parent 595b8b9 commit c6ef59d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion web/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default {
return jsonResponse(fetch(API_URL + "/v1/preset", { method: "POST", body: JSON.stringify(preset) }))
},
discoverRadios() {
return emptyResponse(fetch(API_URL + "/v1/radios", { method: "POST" }))
return jsonResponse(fetch(API_URL + "/v1/radios", { method: "POST" }))
},
listRadios() {
return jsonResponse(fetch(API_URL + "/v1/radios"))
Expand Down
37 changes: 28 additions & 9 deletions web/src/store/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
return state.radioUUID
},
radioLoaded(state) {
return state.radio.uuid == state.radioUUID
return state.radio.uuid == state.radioUUID && state.radioWSConnected
}
},
mutations: {
Expand All @@ -43,9 +43,16 @@ export default {
},
SET_RADIO_UUID(state, radioUUID) {
state.radioUUID = radioUUID
localStorage.lastRadioUUID = radioUUID;
},
SET_RADIOS(state, radios) {
state.radios = radios
for (let i in radios) {
if (state.radios[i].uuid == state.radioUUID) {
return
}
}
state.radioUUID = ''
},
SET_RADIOS_DISCOVERING(state, radiosDiscovering) {
state.radiosDiscovering = radiosDiscovering
Expand All @@ -64,12 +71,24 @@ export default {
}
},
actions: {
initRadio({ dispatch, state }) {
return dispatch('listRadios').then(() => {
if (localStorage.lastRadioUUID) {
for (let radio of state.radios) {
if (radio.uuid == localStorage.lastRadioUUID) {
dispatch('setRadioUUID', radio.uuid);
return
}
}
}
})
},
discoverRadios({ commit, dispatch }) {
commit("SET_RADIOS_DISCOVERING", true);
return api.discoverRadios()
.then(({ ok, error }) => {
if (ok) {
dispatch("listRadios");
return dispatch("listRadios");
} else {
console.error(error)
}
Expand Down Expand Up @@ -98,8 +117,9 @@ export default {
commit("SET_RADIOS_LOADING", false)
})
},
refreshRadio({ commit, state }) {
refreshRadio({ commit, dispatch, state }) {
commit("SET_RADIO_REFRESHING", true)
dispatch("refreshRadioWS")
return api.refreshRadio(state.radioUUID)
.then(({ ok, error }) => {
if (!ok) {
Expand Down Expand Up @@ -129,7 +149,7 @@ export default {
})
},
toggleRadioPower({ commit, state }) {
power = !state.radio.power
let power = !state.radio.power
return api.patchRadio(state.radioUUID, { power })
.then(({ ok, error }) => {
if (ok) {
Expand All @@ -142,20 +162,18 @@ export default {
console.error(error)
})
},
setRadioVolume({ commit }, volume) {
setRadioVolume({ state }, volume) {
return api.patchRadio(state.radioUUID, { volume })
.then(({ ok, error }) => {
if (ok) {
commit('SET_RADIO_VOLUME', volume)
} else {
if (!ok) {
console.error(error)
}
})
.catch(error => {
console.error(error)
})
},
setRadioPreset({ commit }, preset) {
setRadioPreset({ commit, state }, preset) {
return api.patchRadio(state.radioUUID, { preset })
.then(({ ok, error }) => {
if (ok) {
Expand Down Expand Up @@ -205,6 +223,7 @@ export default {

let onDisconnect = function () {
commit("SET_RADIO_WS_CONNECTED", false);
commit("SET_RADIO_WS_CONNECTING", false);
};

ws.addEventListener("close", onDisconnect);
Expand Down

0 comments on commit c6ef59d

Please sign in to comment.