-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement storing words, separate it from list store
- Loading branch information
Showing
6 changed files
with
63 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,36 +1,46 @@ | ||
import api from '@/lib/api' | ||
import { defineStore } from 'pinia' | ||
import Word from '@/types/word' | ||
import { useListStore } from './list' | ||
import { ref } from 'vue' | ||
import { useListStore } from './list' | ||
import { ref, computed } from 'vue' | ||
|
||
interface WordParams { | ||
origin: string, | ||
translation: string, | ||
list_id: number | ||
origin: string | ||
translation: string | ||
list_id: number | ||
} | ||
|
||
export const useWordStore = defineStore('word', () => { | ||
const listStore = useListStore() | ||
const listStore = useListStore() | ||
|
||
const all = ref<Map<number, Word>>(new Map()) | ||
const all = ref<Map<number, Word>>(new Map()) | ||
|
||
async function createWord(wordParams: WordParams) { | ||
const newWord = await api.post('words', wordParams) | ||
const wordsForList = computed(() => { | ||
return (listId: number): Word[] => | ||
Array.from(all.value.values()).filter((word) => word.list_id == listId) | ||
}) | ||
|
||
listStore.all.get(newWord.list_id)?.words?.push(newWord) | ||
all.value.set(newWord.id, newWord) | ||
return newWord | ||
} | ||
|
||
async function deleteWord(wordId: number) { | ||
await api.delete(`words/${wordId}`) | ||
async function fetchWords(listId: number) { | ||
const words = await api.get(`lists/${listId}/words`) | ||
|
||
all.value.delete(wordId) | ||
for (const word of words) { | ||
all.value.set(word.id, word) | ||
} | ||
} | ||
|
||
return { all, createWord, deleteWord } | ||
}) | ||
async function createWord(wordParams: WordParams) { | ||
const newWord = await api.post('words', wordParams) | ||
|
||
listStore.all.get(newWord.list_id)?.words?.push(newWord) | ||
all.value.set(newWord.id, newWord) | ||
return newWord | ||
} | ||
|
||
async function deleteWord(wordId: number) { | ||
await api.delete(`words/${wordId}`) | ||
|
||
all.value.delete(wordId) | ||
} | ||
|
||
return { all, wordsForList, fetchWords, createWord, deleteWord } | ||
}) |
This file contains 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ export default interface Word { | |
origin: string | ||
translation: string | ||
proficiency: number | ||
list_id: number | ||
} |
This file contains 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 |
---|---|---|
@@ -1,39 +1,38 @@ | ||
<script setup lang="ts"> | ||
import { Ref, ref, watch } from 'vue' | ||
import { useRoute } from 'vue-router' | ||
import { ref } from 'vue' | ||
import { storeToRefs } from 'pinia' | ||
import { useListStore } from '@/stores/list' | ||
import { useWordStore } from '@/stores/word' | ||
import List from '@/types/list' | ||
import Word from '@/types/word' | ||
import NewWordForm from '@/components/NewWordForm.vue' | ||
import WordListing from '@/components/WordListing.vue' | ||
import ListHeader from '@/components/ListHeader.vue' | ||
const list: Ref<List | null> = ref(null) | ||
const props = defineProps<{ listId: number }>() | ||
const route = useRoute() | ||
const store = useListStore() | ||
const isFetching = ref(true) | ||
watch( | ||
() => route.params.id as string, | ||
async (newId) => { | ||
list.value = await store.fetchList(newId) | ||
}, | ||
{ immediate: true } | ||
) | ||
const listStore = useListStore() | ||
const wordStore = useWordStore() | ||
const { getListById } = storeToRefs(listStore) | ||
const { wordsForList } = storeToRefs(wordStore) | ||
wordStore.fetchWords(props.listId).then(() => { | ||
isFetching.value = false | ||
}) | ||
</script> | ||
wordStore | ||
|
||
<template> | ||
<section v-if="list" class=""> | ||
<list-header :list="list" :list-id="list.id" /> | ||
<section v-if="!isFetching" class=""> | ||
<list-header :list="getListById(listId) as List" /> | ||
|
||
<h4 v-if="list.words" class="text-h6 ml-4"> | ||
words: {{ list.words.length }} | ||
</h4> | ||
<h4 class="text-h6 ml-4">words: {{ wordsForList(listId).length }}</h4> | ||
|
||
<new-word-form :list-id="list.id" /> | ||
<new-word-form :list-id="listId" /> | ||
|
||
<word-listing v-if="list.words?.length" :words="list.words as Word[]" /> | ||
<word-listing :words="wordsForList(listId) as Word[]" /> | ||
</section> | ||
</template> | ||
<style scoped lang="scss"></style> |
This file contains 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