Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Convert active media to options store (#1628)

Olga Bulat747913e06980
diff --git a/src/stores/active-media.ts b/src/stores/active-media.ts
index b2bb0e73a..179a2cd92 100644
--- a/src/stores/active-media.ts
+++ b/src/stores/active-media.ts
@@ -1,10 +1,9 @@
import { defineStore } from 'pinia'
-import { reactive, readonly, toRefs } from '@nuxtjs/composition-api'
import type { SupportedMediaType } from '~/constants/media'
import type { Media } from '~/models/media'
-type MediaStatus = 'ejected' | 'playing' | 'paused' // 'ejected' means player is closed
+export type MediaStatus = 'ejected' | 'playing' | 'paused' // 'ejected' means player is closed
export interface ActiveMediaState {
type: SupportedMediaType | null
@@ -18,69 +17,54 @@ const ACTIVE_MEDIA = 'active-media'
/**
* Store information about the active media item.
*/
-export const useActiveMediaStore = defineStore(ACTIVE_MEDIA, () => {
+export const useActiveMediaStore = defineStore(ACTIVE_MEDIA, {
/* State */
- const state: ActiveMediaState = reactive({
+ state: (): ActiveMediaState => ({
type: null,
id: null,
status: 'ejected',
message: null,
- })
- /**
- * Only the properties used by components are exported as refs.
- * `status` is not used anywhere in the components.
- */
- const { type, id, message } = toRefs(state)
+ }),
- /* Actions */
+ actions: {
+ /**
+ * Sets a new media item as the active one.
+ *
+ * @param type - the type of the active media
+ * @param id - the unique identifier of the active media
+ * @param status - the current status of the active media
+ */
+ setActiveMediaItem({
+ type,
+ id,
+ status = 'playing',
+ }: {
+ type: SupportedMediaType
+ id: Media['id']
+ status?: MediaStatus
+ }) {
+ this.type = type
+ this.id = id
+ this.status = status
+ },
- /**
- * Sets a new media item as the active one.
- *
- * @param type - the type of the active media
- * @param id - the unique identifier of the active media
- * @param status - the current status of the active media
- */
- function setActiveMediaItem({
- type,
- id,
- status = 'playing',
- }: {
- type: SupportedMediaType
- id: Media['id']
- status?: MediaStatus
- }) {
- state.type = type
- state.id = id
- state.status = status
- }
- function pauseActiveMediaItem() {
- state.status = 'paused'
- }
- function ejectActiveMediaItem() {
- state.status = 'ejected'
- state.id = null
- state.type = null
- }
- /**
- * Set the message associated with rendering/playback of the active media.
- *
- * @param message - the message to display to the user
- */
- function setMessage({ message }: { message: string }) {
- state.message = message
- }
+ pauseActiveMediaItem() {
+ this.status = 'paused'
+ },
- return {
- type: readonly(type),
- id: readonly(id),
- message: readonly(message),
- state: readonly(state),
-
- setActiveMediaItem,
- pauseActiveMediaItem,
- ejectActiveMediaItem,
- setMessage,
- }
+ ejectActiveMediaItem() {
+ this.status = 'ejected'
+ this.id = null
+ this.type = null
+ },
+ /**
+ * Set the message associated with rendering/playback of the active media.
+ *
+ * @param message - the message to display to the user
+ */
+ setMessage({ message }: { message: string }) {
+ this.message = message
+ },
+ },
})
diff --git a/test/unit/specs/stores/active-media-store.spec.js b/test/unit/specs/stores/active-media-store.spec.js
index 03a3a3ee0..ab5a9ee0d 100644
--- a/test/unit/specs/stores/active-media-store.spec.js
+++ b/test/unit/specs/stores/active-media-store.spec.js
@@ -14,17 +14,22 @@ describe('Active Media Store', () => {
describe('state', () => {
it('sets initial filters to filterData', () => {
const activeMediaStore = useActiveMediaStore()
- expect(activeMediaStore.state).toEqual(initialState)
+ expect(activeMediaStore.type).toEqual(initialState.type)
+ expect(activeMediaStore.id).toEqual(initialState.id)
+ expect(activeMediaStore.status).toEqual(initialState.status)
+ expect(activeMediaStore.message).toEqual(initialState.message)
})
})
describe('actions', () => {
- it.each(statuses)('can set active media with status $status', (status) => {
+ it.each(statuses)(`can set active media with status $status`, (status) => {
const activeMediaStore = useActiveMediaStore()
const mediaItem = { type: AUDIO, id: 'audio1' }
activeMediaStore.setActiveMediaItem({ ...mediaItem, status })
const expectedState = { ...initialState, ...mediaItem, status }
- expect(activeMediaStore.state).toEqual(expectedState)
+ expect(activeMediaStore.id).toEqual(expectedState.id)
+ expect(activeMediaStore.type).toEqual(expectedState.type)
+ expect(activeMediaStore.status).toEqual(expectedState.status)
})
it.each(statuses)('can pause an item with any status', (status) => {
@@ -32,7 +37,7 @@ describe('Active Media Store', () => {
activeMediaStore.setActiveMediaItem({ status })
activeMediaStore.pauseActiveMediaItem()
- expect(activeMediaStore.state.status).toEqual('paused')
+ expect(activeMediaStore.status).toEqual('paused')
})
it('can eject an item', () => {
const activeMediaStore = useActiveMediaStore()
@@ -44,7 +49,9 @@ describe('Active Media Store', () => {
})
activeMediaStore.ejectActiveMediaItem()
- expect(activeMediaStore.state).toEqual(initialState)
+ expect(activeMediaStore.id).toEqual(initialState.id)
+ expect(activeMediaStore.type).toEqual(initialState.type)
+ expect(activeMediaStore.status).toEqual(initialState.status)
})
it('can set a message', () => {
const activeMediaStore = useActiveMediaStore()