-
Notifications
You must be signed in to change notification settings - Fork 1
Adding listen-view with mock-storage #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Angamanga
wants to merge
1
commit into
ushahidi:main
Choose a base branch
from
Angamanga:listen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { defineStore } from 'pinia' | ||
| import { ref, computed } from 'vue' | ||
| import api from '@/lib/api' | ||
| import { useMockStorageStore } from '@/stores/mockStorage' | ||
| import { useUserStore } from '@/stores/user' | ||
|
|
||
| export type ListenSlotStatus = 'loading' | 'ready' | 'voted' | 'skipped' | 'error' | ||
|
|
||
| export interface ListenSlot { | ||
| audioId: string | ||
| userId: string | ||
| sentence: string | null | ||
| audioUrl: string | null | ||
| status: ListenSlotStatus | ||
| vote: 'yes' | 'no' | null | ||
| error: string | null | ||
| } | ||
|
|
||
| export const useListenStore = defineStore('listen', () => { | ||
| const slots = ref<ListenSlot[]>([]) | ||
| const activeIndex = ref(0) | ||
| const loading = ref(false) | ||
|
|
||
| const activeSlot = computed(() => slots.value[activeIndex.value] ?? null) | ||
| const allDone = computed(() => | ||
| slots.value.length > 0 && | ||
| slots.value.every((s) => s.vote !== null || s.status === 'skipped'), | ||
| ) | ||
|
|
||
| async function loadBatch() { | ||
| const mockStore = useMockStorageStore() | ||
| const currentUserId = useUserStore().userId | ||
| const entries = mockStore.recordings.filter((e) => e.userId !== currentUserId).slice(0, 5) | ||
| if (entries.length === 0) { | ||
| slots.value = [] | ||
| return | ||
| } | ||
|
|
||
| loading.value = true | ||
| slots.value = entries.map((e) => ({ | ||
| audioId: e.audioId, | ||
| userId: e.userId, | ||
| sentence: null, | ||
| audioUrl: null, | ||
| status: 'loading' as ListenSlotStatus, | ||
| vote: null, | ||
| error: null, | ||
| })) | ||
| activeIndex.value = 0 | ||
|
|
||
| await Promise.all( | ||
| entries.map(async (entry, i) => { | ||
| try { | ||
| const { data } = await api.get(`/audio/${entry.audioId}`) | ||
| console.log(`Loaded audio ${entry.audioId}:`, data) | ||
| slots.value[i] = { | ||
| ...slots.value[i], | ||
| sentence: data.metadata?.sentence ?? null, | ||
| audioUrl: data.url ?? null, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The audioUrl here comes from the common-voice API but I have had problems accessing it. Check with Bülent :) |
||
| status: 'ready', | ||
| } | ||
| } catch { | ||
| slots.value[i] = { ...slots.value[i], status: 'error', error: 'Failed to load' } | ||
| } | ||
| }), | ||
| ) | ||
| loading.value = false | ||
| } | ||
|
|
||
| function setActiveIndex(index: number) { | ||
| activeIndex.value = index | ||
| } | ||
|
|
||
| function vote(index: number, choice: 'yes' | 'no') { | ||
| if (!slots.value[index]) return | ||
| slots.value[index] = { ...slots.value[index], vote: choice, status: 'voted' } | ||
| const voterId = useUserStore().userId ?? '' | ||
| useMockStorageStore().recordVote(slots.value[index].audioId, voterId, choice) | ||
| const next = slots.value.findIndex( | ||
| (s, i) => i > index && s.vote === null && s.status !== 'skipped', | ||
| ) | ||
| if (next !== -1) activeIndex.value = next | ||
| } | ||
|
|
||
| function skip(index: number) { | ||
| if (!slots.value[index]) return | ||
| slots.value[index] = { ...slots.value[index], status: 'skipped' } | ||
| const next = slots.value.findIndex( | ||
| (s, i) => i > index && s.vote === null && s.status !== 'skipped', | ||
| ) | ||
| if (next !== -1) activeIndex.value = next | ||
| } | ||
|
|
||
| function reset() { | ||
| slots.value = [] | ||
| activeIndex.value = 0 | ||
| loading.value = false | ||
| } | ||
|
|
||
| return { slots, activeIndex, activeSlot, allDone, loading, loadBatch, setActiveIndex, vote, skip, reset } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { defineStore } from 'pinia' | ||
| import { ref } from 'vue' | ||
|
|
||
| export interface MockVote { | ||
| voterId: string | ||
| vote: 'yes' | 'no' | ||
| } | ||
|
|
||
| export interface MockRecordingEntry { | ||
| audioId: string | ||
| userId: string | ||
| votes: MockVote[] | ||
| } | ||
|
|
||
| export const useMockStorageStore = defineStore('mockStorage', () => { | ||
| const recordings = ref<MockRecordingEntry[]>([]) | ||
|
|
||
| function addRecording(audioId: string, userId: string) { | ||
| if (!import.meta.env.DEV) return | ||
| recordings.value.push({ audioId, userId, votes: [] }) | ||
| console.log(`Added recording: audioId=${audioId}, userId=${userId}`) | ||
| } | ||
|
|
||
| function recordVote(audioId: string, voterId: string, vote: 'yes' | 'no') { | ||
| if (!import.meta.env.DEV) return | ||
| const entry = recordings.value.find((e) => e.audioId === audioId) | ||
| if (entry) entry.votes.push({ voterId, vote }) | ||
| console.log(`Recorded vote: audioId=${audioId}, voterId=${voterId}, vote=${vote}`) | ||
| } | ||
|
|
||
| return { recordings, addRecording, recordVote } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use aws here instead