Skip to content
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

Fetch #64

Merged
merged 15 commits into from
Nov 18, 2024
22 changes: 14 additions & 8 deletions app.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<script setup lang="ts">
const themeStore = useThemeStore()

onMounted(() => {
themeStore.theme = themeStore.getTheme()
})

useHead({
titleTemplate: (title) => (title ? `${title} - Metschoo Library` : "Metschoo Library"),
meta: [
Expand All @@ -13,16 +9,26 @@ useHead({
content: "Aplikasi Perpustakan Metschoo.",
},
],
htmlAttrs: {
class: computed(() => themeStore.theme),
},
bodyAttrs: {
class: "bg-surface-100/80 dark:bg-surface-900 dark:text-gray-300",
class: computed(() => [
"bg-surface-100/80 dark:bg-surface-900 dark:text-gray-300",
// on the server themeStore.theme is always undefined
// so we pass an empty string like so
themeStore.theme ?? "",
]),
},
})

onMounted(() => {
themeStore.theme = themeStore.getTheme()
})

const authStore = useAuthStore()
useLazyAsyncData(() => authStore.init().then(() => true))
</script>

<template>
<NuxtLoadingIndicator />
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
Expand Down
2 changes: 1 addition & 1 deletion components/ThemeToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function toggleTheme(theme: Theme) {
</script>

<template>
<button :title="`mode ${themeStore.theme}`" @click="toggleTheme(themeStore.theme)">
<button :title="`mode ${themeStore.theme}`" @click="toggleTheme(themeStore.theme ?? 'light')">
<IconMoonCrescent v-if="themeStore.theme === 'dark'" />
<IconSun v-else />
</button>
Expand Down
7 changes: 2 additions & 5 deletions components/home/BookList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ interface Props {

const { typeId } = defineProps<Props>()

const isLoading = ref(false)

const { data: bukus } = await useAsyncData(async () => {
const { data: bukus } = await useAsyncData(String(typeId), async () => {
try {
return await getBukus(typeId)
} catch (err) {
Expand All @@ -21,8 +19,7 @@ const { data: bukus } = await useAsyncData(async () => {

<template>
<ul class="book-list">
<LoadingSpinner v-if="isLoading" />
<li v-if="!isLoading && !bukus">Bukunya ga ada gaes</li>
<li v-if="!bukus">Bukunya ga ada gaes</li>
<BookItem v-for="buku in bukus" v-else :key="buku.no_isbn" :buku="buku as Buku" />
</ul>
</template>
8 changes: 4 additions & 4 deletions error.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

useHead({
title: "404 Gak Ketemu",
htmlAttrs: {
class: computed(() => themeStore.theme),
},
bodyAttrs: {
class: "bg-surface-100/80 dark:bg-surface-900 dark:text-gray-300",
class: computed(() => [
"bg-surface-100/80 dark:bg-surface-900 dark:text-gray-300",
themeStore.theme ?? "",
]),
},
})

defineProps({
error: Object as () => NuxtError,

Check warning on line 17 in error.vue

View workflow job for this annotation

GitHub Actions / Run linters (20)

Prop 'error' requires default value to be set
})

const handleError = () => clearError({ redirect: "/" })
Expand Down
18 changes: 3 additions & 15 deletions layouts/admin.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
<script lang="ts" setup>
import type { Pengguna } from "@/types"
import { ref } from "vue"

const authStore = useAuthStore()

const profile = ref<Pengguna | null>(null)

onMounted(async () => {
await authStore.init()
authStore.$subscribe((_, state) => {
if (!state.profile) return

profile.value = state.profile
})
const { data: profile } = await useAsyncData(async () => {
const authStore = useAuthStore()
return authStore.profile
})
</script>

<template>
<div class="grid flex-1 wrapper">
<TheNavbar />
<NuxtLoadingIndicator />
<AdminSidebar :profile="profile" />
<main class="p-4 flex flex-col align-stretch gap-4 mx-auto">
<slot />
Expand Down
9 changes: 0 additions & 9 deletions layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<script setup lang="ts">
const authStore = useAuthStore()

onMounted(async () => {
await authStore.init()
})
</script>

<template>
<TheNavbar />
<NuxtLoadingIndicator />
<main class="p-2 md:p-8">
<slot />
</main>
Expand Down
8 changes: 0 additions & 8 deletions layouts/profile-edit.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
<script setup lang="ts">
const authStore = useAuthStore()

onMounted(async () => {
await authStore.init()
})
</script>

<template>
<NuxtLayout name="default">
<div class="max-w-[100ch] mx-auto flex flex-col gap-4">
Expand Down
File renamed without changes.
44 changes: 17 additions & 27 deletions pages/admin/buku/[isbn].vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script setup lang="ts">
import { StorageError } from "@supabase/storage-js"
import { useDialog } from "@/composables"
import type { Kategori } from "@/types"
import type { PostgrestError, QueryData } from "@supabase/supabase-js"
import type { PostgrestError } from "@supabase/supabase-js"

import type { Database } from "~/types/database.types.ts"
import IconArrowLeft from "~icons/mdi/arrow-left"
import type { Buku } from "~/types"

definePageMeta({
layout: "admin",
Expand All @@ -14,43 +14,33 @@ definePageMeta({
const supabase = useSupabaseClient<Database>()
const isLoading = ref(false)

const availableCategories = ref<Kategori[]>([])
const router = useRouter()
const currentRoute = useRoute()

const isbn = currentRoute.params.isbn
const bukuQuery = supabase
.from("buku")
.select("*, kategori_buku(kategori)")
.eq("no_isbn", isbn)
.single()

const { dialog } = useDialog()
const { dialog: errDialog } = useDialog()

type Buku = QueryData<typeof bukuQuery>
const buku = ref<Buku | null>(null)
const { data: buku } = await useAsyncData(async () => {
const { data, error } = await supabase
.from("buku")
.select("*, kategori_buku(kategori)")
.eq("no_isbn", isbn)
.single()

async function ambilBuku(): Promise<Buku | null> {
try {
isLoading.value = true
const { data, error } = await bukuQuery
if (error) throw error
return data
} catch (err) {
console.trace((err as PostgrestError).message)
errDialog.value.open(`Buku dengan ISBN ${isbn} tidak ditemukan.`)
return null
} finally {
isLoading.value = false
if (error) {
console.error(error.message)
throw error
}
}

onMounted(async () => {
buku.value = await ambilBuku()
availableCategories.value = await getAllAvailableCategories()
return data
})

const { data: availableCategories } = await useAsyncData(
async () => await getAllAvailableCategories()
)

async function editBook(buku: Buku) {
try {
const { error } = await supabase
Expand Down Expand Up @@ -185,7 +175,7 @@ function toggleFormVisibility() {
<Select
v-model="buku.kategori_id"
placeholder="Pilih kategori"
:options="availableCategories"
:options="availableCategories ?? []"
checkmark
option-label="kategori"
option-value="id"
Expand Down
6 changes: 3 additions & 3 deletions pages/admin/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const _peminjamanQuery = supabase
.from("peminjaman")
.select("*, peminjaman_state(name), pengguna(nama, kelas, jurusan), buku(*)")
export type PeminjamanData = QueryData<typeof _peminjamanQuery>
const { data: allPeminjamanData } = useAsyncData(async () => await getPeminjamanData())
const { data: allPeminjamanData } = useLazyAsyncData(async () => await getPeminjamanData())

const peminjamanData = computed(() => {
if (!allPeminjamanData.value) return null
Expand All @@ -38,9 +38,9 @@ const peminjamanData = computed(() => {
return { bukusBorrowConfirmed, bukusBorrowPending, bukusReturnPending }
})

const { data: counts } = await useAsyncData(async () => {
const { data: counts } = await useLazyAsyncData(async () => {
const [bukuCount, penggunaCount] = await Promise.all([countBukus(), countPenggunas()])
return { bukuCount, penggunaCount }
return { bukuCount: bukuCount ?? 0, penggunaCount: penggunaCount ?? 0 }
})

onMounted(async () => {
Expand Down
25 changes: 10 additions & 15 deletions pages/admin/pengguna/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@ const supabase = useSupabaseClient<Database>()
const toast = useToast()

const { data: penggunas } = await useAsyncData(async () => {
try {
const { data, error } = await supabase.from("pengguna").select("*, pengguna_roles(name)")
if (error) {
toast.add({
severity: "error",
summary: "Gagal mengambil data",
detail: "Gagal mengambil data pengguna, silahkan coba lagi.",
})
}
const { data, error } = await supabase.from("pengguna").select("*, pengguna_roles(name)")
if (error) {
toast.add({
severity: "error",
summary: "Gagal mengambil data",
detail: "Gagal mengambil data pengguna, silahkan coba lagi.",
})
}

if (error) throw error
if (error) throw error

return data
} catch (err) {
console.error(err)
return null
}
return data
})
</script>

Expand Down
6 changes: 4 additions & 2 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ definePageMeta({
layout: "default",
})

const authStore = useAuthStore()
const { profile } = authStore
const { data: profile } = await useLazyAsyncData(async () => {
const authStore = useAuthStore()
return authStore.profile
})
</script>

<template>
Expand Down
9 changes: 3 additions & 6 deletions pages/profil/edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ useHead({

definePageMeta({
layout: "profile-edit",
middleware: "profil",
})

const authStore = useAuthStore()
const supabaseUser = useSupabaseUser()
const user = ref<Pengguna | null>(null)

onMounted(async () => {
if (supabaseUser.value) {
user.value = await authStore.getProfile(supabaseUser.value.id)
}
const { data: user } = await useAsyncData(async () => {
return authStore.profile
})

const toast = useToast()
Expand Down
1 change: 1 addition & 0 deletions pages/profil/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ useHead({

definePageMeta({
layout: "default",
middleware: "profil",
})

const supabase = useSupabaseClient<Database>()
Expand Down
11 changes: 3 additions & 8 deletions pages/profil/keamanan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ useHead({

definePageMeta({
layout: "profile-edit",
middleware: "profil",
})

const supabase = useSupabaseClient<Database>()
const user = useSupabaseUser()

const { dialog } = useDialog()
const kredensialPengguna = ref({
password: "",
passwordKonfirmasi: "",
email: "",
email: user.value!.email,
})

async function ubahKredensial() {
Expand Down Expand Up @@ -72,13 +74,6 @@ async function signOut() {
await authStore.handleSignOut()
router.push("/")
}

onMounted(async () => {
authStore.$subscribe((_, state) => {
if (!state.profile) return
kredensialPengguna.value.email = state.profile.email
})
})
</script>

<template>
Expand Down
9 changes: 3 additions & 6 deletions pages/wishlist.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import type { Buku } from "@/types/"
import type { PostgrestError, QueryData } from "@supabase/supabase-js"
import type { PostgrestError } from "@supabase/supabase-js"

import Toast from "primevue/toast"
import { useToast } from "primevue/usetoast"
Expand All @@ -17,8 +17,6 @@ definePageMeta({
const supabase = useSupabaseClient<Database>()

const wishlistQuery = supabase.from("wishlist").select("*, buku(*)")
type Wishlist = QueryData<typeof wishlistQuery>
const wishlist = ref<Wishlist>([])

async function ambilWishlist() {
try {
Expand All @@ -31,8 +29,7 @@ async function ambilWishlist() {
}
}

const { data } = useAsyncData(async () => await ambilWishlist())
wishlist.value = data.value ?? []
const { data: wishlist } = useAsyncData(async () => await ambilWishlist())

const toast = useToast()

Expand Down Expand Up @@ -81,7 +78,7 @@ const user = useSupabaseUser()
</section>

<section v-else class="main-section">
<p v-if="!wishlist.length">Kamu belum punya apa-apa dalam wishlist kamu.</p>
<p v-if="!wishlist">Kamu belum punya apa-apa dalam wishlist kamu.</p>
<ul v-else class="book-list">
<WishlistBook
v-for="wishlistItem in wishlist"
Expand Down
Loading