🐛 (home) watch props to retrieve repo on change
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
|
import { Ref, ref } from '@vue/reactivity'
|
||||||
import { nextTick, onUnmounted, watch } from '@vue/runtime-core'
|
import { nextTick, onUnmounted, watch } from '@vue/runtime-core'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
|
|
||||||
import { noteEventBus } from '@/bus/noteBusEvent'
|
import { noteEventBus } from '@/bus/noteBusEvent'
|
||||||
import { ref } from '@vue/reactivity'
|
|
||||||
import { useLinks } from '@/hooks/useLinks.hook'
|
import { useLinks } from '@/hooks/useLinks.hook'
|
||||||
import { useRepo } from '@/hooks/useRepo.hook'
|
import { useRepo } from '@/hooks/useRepo.hook'
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ const sanitizePath = (path: string) => {
|
|||||||
return decodeURIComponent(path)
|
return decodeURIComponent(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useNote = (user?: string, repo?: string) => {
|
export const useNote = (user: Ref<string>, repo: Ref<string>) => {
|
||||||
const { push } = useRouter()
|
const { push } = useRouter()
|
||||||
const { query } = useRoute()
|
const { query } = useRoute()
|
||||||
const stackedNotes = ref(
|
const stackedNotes = ref(
|
||||||
@@ -24,14 +24,6 @@ export const useNote = (user?: string, repo?: string) => {
|
|||||||
: []
|
: []
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!user || !repo) {
|
|
||||||
return {
|
|
||||||
readme: ref(null),
|
|
||||||
notFound: ref(true),
|
|
||||||
stackedNotes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { readme, notFound, tree } = useRepo(user, repo)
|
const { readme, notFound, tree } = useRepo(user, repo)
|
||||||
const { listenToClick } = useLinks('note-display')
|
const { listenToClick } = useLinks('note-display')
|
||||||
|
|
||||||
@@ -78,8 +70,8 @@ export const useNote = (user?: string, repo?: string) => {
|
|||||||
push({
|
push({
|
||||||
name: 'Home',
|
name: 'Home',
|
||||||
params: {
|
params: {
|
||||||
user,
|
user: user.value,
|
||||||
repo
|
repo: repo.value
|
||||||
},
|
},
|
||||||
query: {
|
query: {
|
||||||
stackedNotes: newStackedNotes
|
stackedNotes: newStackedNotes
|
||||||
@@ -90,12 +82,10 @@ export const useNote = (user?: string, repo?: string) => {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
watch(readme, () => {
|
watch([readme, user, repo], () => {
|
||||||
if (readme.value) {
|
nextTick(() => {
|
||||||
nextTick(() => {
|
listenToClick()
|
||||||
listenToClick()
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { onMounted, ref } from '@vue/runtime-core'
|
import { Ref, onMounted, ref, watch } from '@vue/runtime-core'
|
||||||
|
|
||||||
import { request } from '@octokit/request'
|
import { request } from '@octokit/request'
|
||||||
import { useMarkdown } from '@/hooks/useMarkdown.hook'
|
import { useMarkdown } from '@/hooks/useMarkdown.hook'
|
||||||
@@ -12,17 +12,21 @@ interface Tree {
|
|||||||
url?: string
|
url?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useRepo = (owner: string, repo: string) => {
|
export const useRepo = (owner: Ref<string>, repo: Ref<string>) => {
|
||||||
const { render } = useMarkdown()
|
const { render } = useMarkdown()
|
||||||
const readme = ref<string | null>(null)
|
const readme = ref<string | null>(null)
|
||||||
const notFound = ref(false)
|
const notFound = ref(false)
|
||||||
const tree = ref<Tree[]>([])
|
const tree = ref<Tree[]>([])
|
||||||
|
|
||||||
onMounted(async () => {
|
const retrieveRepo = async () => {
|
||||||
|
if (!owner.value || !repo.value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const README = await request('GET /repos/{owner}/{repo}/readme', {
|
const README = await request('GET /repos/{owner}/{repo}/readme', {
|
||||||
repo,
|
repo: repo.value,
|
||||||
owner
|
owner: owner.value
|
||||||
})
|
})
|
||||||
|
|
||||||
if (README) {
|
if (README) {
|
||||||
@@ -30,8 +34,8 @@ export const useRepo = (owner: string, repo: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const commits = await request('GET /repos/{owner}/{repo}/commits', {
|
const commits = await request('GET /repos/{owner}/{repo}/commits', {
|
||||||
owner,
|
repo: repo.value,
|
||||||
repo
|
owner: owner.value
|
||||||
})
|
})
|
||||||
|
|
||||||
const lastCommit = commits.data.shift()
|
const lastCommit = commits.data.shift()
|
||||||
@@ -43,8 +47,8 @@ export const useRepo = (owner: string, repo: string) => {
|
|||||||
const treeResponse = await request(
|
const treeResponse = await request(
|
||||||
'GET /repos/{owner}/{repo}/git/trees/{tree_sha}',
|
'GET /repos/{owner}/{repo}/git/trees/{tree_sha}',
|
||||||
{
|
{
|
||||||
owner,
|
repo: repo.value,
|
||||||
repo,
|
owner: owner.value,
|
||||||
tree_sha: lastCommit.commit.tree.sha,
|
tree_sha: lastCommit.commit.tree.sha,
|
||||||
recursive: 'true'
|
recursive: 'true'
|
||||||
}
|
}
|
||||||
@@ -57,7 +61,11 @@ export const useRepo = (owner: string, repo: string) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
notFound.value = true
|
notFound.value = true
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
onMounted(() => retrieveRepo())
|
||||||
|
|
||||||
|
watch([owner, repo], () => retrieveRepo())
|
||||||
|
|
||||||
return {
|
return {
|
||||||
readme,
|
readme,
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
<template>
|
<template>
|
||||||
<div v-if="!user || !repo">
|
<div v-if="!user || !repo" :key="routeKey">
|
||||||
Bonjour
|
Bonjour
|
||||||
|
|
||||||
<form @submit.prevent>
|
<form @submit.prevent>
|
||||||
<div class="columns is-centered is-vcentered">
|
<div class="columns is-mobile is-centered is-vcentered">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">user</label>
|
<label class="label">user</label>
|
||||||
@@ -37,7 +37,10 @@
|
|||||||
<div class="columns">
|
<div class="columns">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<h1 class="title is-1">
|
<h1 class="title is-1">
|
||||||
<router-link :to="{ name: 'Home' }">
|
<router-link
|
||||||
|
:to="{ name: 'Home', params: { user, repo } }"
|
||||||
|
:key="routeKey"
|
||||||
|
>
|
||||||
{{ repo }}
|
{{ repo }}
|
||||||
</router-link>
|
</router-link>
|
||||||
</h1>
|
</h1>
|
||||||
@@ -56,7 +59,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent, defineAsyncComponent } from 'vue'
|
import { defineComponent, defineAsyncComponent, computed, toRefs } from 'vue'
|
||||||
import { useNote } from '@/hooks/useNote.hook'
|
import { useNote } from '@/hooks/useNote.hook'
|
||||||
import { useForm } from '@/hooks/useForm.hook'
|
import { useForm } from '@/hooks/useForm.hook'
|
||||||
|
|
||||||
@@ -70,11 +73,16 @@ export default defineComponent({
|
|||||||
StackedNote
|
StackedNote
|
||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
user: { type: String, required: false },
|
user: { type: String, required: false, default: '' },
|
||||||
repo: { type: String, required: false }
|
repo: { type: String, required: false, default: '' }
|
||||||
},
|
},
|
||||||
setup(props) {
|
setup(props) {
|
||||||
return { ...useNote(props.user, props.repo), ...useForm() }
|
const refProps = toRefs(props)
|
||||||
|
return {
|
||||||
|
...useNote(refProps.user, refProps.repo),
|
||||||
|
...useForm(),
|
||||||
|
routeKey: computed(() => `${props.user}-${props.repo}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user