Skip to content

Commit

Permalink
booksearch more detail
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeWL committed Nov 15, 2024
1 parent a0e9dee commit dc49ab1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ section h2 {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
max-height: 33vh;
max-height: 50vh;
overflow-y: auto;
}

Expand Down
11 changes: 9 additions & 2 deletions src/components/BookSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ const getSearchResults = async () => {

<template>
<div>
<input type="text" v-model="currentSearch" />
<input
type="text"
v-model="currentSearch"
placeholder="Search for books"
/>
<button
type="submit"
:disabled="isLoading === true || error?.length > 0"
@click="getSearchResults()"
>
Search for books
Search for books
</button>
<small>
(default if no search is top 10 downloads)
</small>
<p v-if="isLoading">
<strong>Loading...</strong>
</p>
Expand Down
38 changes: 32 additions & 6 deletions src/components/SearchResult.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
<script setup lang="ts">
import type { BookDetail } from '@/types';
import getGutenbergText from '@/api/getGutenbergText'
import splitStringToTokens from '@/scripts/splitStringToTokens'
import { mainStore } from '@/stores/mainStore'
import type { BookDetail } from '@/types'
import { ref } from 'vue'
const props = defineProps<{ value: BookDetail }>()
const getBookText = (textUrl: string) => {
console.log('Getting text for book with url:', textUrl)
const isLoading = ref(false)
const error = ref('')
const getBookText = async (textUrl: string) => {
isLoading.value = true
try {
const results = await getGutenbergText(textUrl)
if (results.length > 0) {
mainStore.setText(results)
mainStore.setWordsArray(splitStringToTokens(results))
error.value = ''
}
} catch (err) {
error.value = (err as Error)?.message ?? 'An unknown error occurred'
}
isLoading.value = false
setTimeout(() => {
error.value = ''
}, 3000)
}
</script>

<template>
<li class="book">
<h3>
Expand All @@ -14,13 +35,18 @@ const getBookText = (textUrl: string) => {
<p>
<strong>Author(s):</strong>
<span class="author" v-for="author in props.value.authors" :key="author.id">
{{ author.name }} [{{ author.birthYear }} - {{ author.deathYear }}]
</span>
{{ author.name }} [{{ author.birthYear }} - {{ author.deathYear }}]
</span>
</p>
<p>
<strong> Bookshelves: </strong>
{{ props.value.bookshelves.join(', ') }}
</p>
<button @click="()=>getBookText(props.value.textUrl)">Set Text to Random Page</button>
<p><strong>Download count:</strong>
{{ props.value.downloadCount }} times
</p>
<button @click="() => getBookText(props.value.textUrl)">Set Text to Random Page</button>
<strong v-if="isLoading">Loading text...</strong>
<strong v-if="error">{{ error }}</strong>
</li>
</template>
15 changes: 13 additions & 2 deletions src/components/SearchResults.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<script setup lang="ts">
import { mainStore } from '@/stores/mainStore'
import SearchResult from './SearchResult.vue';
import SearchResult from './SearchResult.vue'
import { ref } from 'vue'
const hidden = ref(false)
</script>

<template>
<ul v-if="mainStore.searchResults.length > 0" class="unstyled-list">
<div v-if="mainStore.searchResults.length > 0">
<label for="show-hide-results">Show/Hide Results</label>
<input
type="checkbox"
v-if="mainStore.searchResults.length > 0"
v-model="hidden"
id="show-hide-results"
/>
</div>
<ul v-if="mainStore.searchResults.length > 0 && !hidden" class="unstyled-list">
<SearchResult v-for="result in mainStore.searchResults" :key="result.id" :value="result">
<strong>{{ result.title }}</strong> by {{ result.authors.join(', ') }}
</SearchResult>
Expand Down

0 comments on commit dc49ab1

Please sign in to comment.