Skip to content

Commit

Permalink
chore: use custom fetch and nuxt state for data fetching #57
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab authored and frodrigo committed Apr 24, 2024
1 parent 4896c44 commit 4b47568
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions pages/[project]/changes_logs.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<script setup lang="ts">
import {
getAsyncDataOrThrows,
setAsyncRef,
} from '~/libs/getAsyncData'
import type { Log, ObjectId, Project } from '~/libs/types'
import { getLogs, getProject } from '~/libs/types'
definePageMeta({
validate({ params }) {
Expand All @@ -13,15 +8,27 @@ definePageMeta({
})
const params = useRoute().params
const project: string = params.project as string
const projectDetails = ref<Project>()
const projectSlug = params.project as string
const project = ref<Project>()
const logs = ref<Log[]>()
getAsyncDataOrThrows('fetchProject', () =>
getProject(useRuntimeConfig().public.API, project)).then(setAsyncRef(projectDetails))
try {
const projectData = await useFetchWithCache<Project>(`project-${projectSlug}`, `${useRuntimeConfig().public.API}/projects/${projectSlug}`)
project.value = projectData.value
useState<Project>('project', () => projectData.value)
}
catch (err: any) {
ElMessage.error(err.message)
}
getAsyncDataOrThrows('fetchSettings', () =>
getLogs(useRuntimeConfig().public.API, project)).then(setAsyncRef(logs))
try {
const logsData = await useFetchWithCache<Log[]>(`logs-${projectSlug}`, `${useRuntimeConfig().public.API}/projects/${projectSlug}/changes_logs`)
logs.value = logsData.value
useState<Log[]>('logs', () => logsData.value)
}
catch (err: any) {
ElMessage.error(err.message)
}
function removeLogs(objectIds: ObjectId[]) {
logs.value = logs.value?.filter(
Expand All @@ -35,10 +42,10 @@ function removeLogs(objectIds: ObjectId[]) {

<template>
<div>
<ProjectLight v-if="projectDetails" :project="projectDetails" />
<Logs
v-if="logs !== undefined"
:project="project"
<project-light v-if="project" :project="project" />
<logs
v-if="logs?.length"
:project="projectSlug"
:logs="logs"
@remove-logs="removeLogs($event)"
/>
Expand Down

0 comments on commit 4b47568

Please sign in to comment.