Skip to content

Commit

Permalink
fix: make the header reactive (#7)
Browse files Browse the repository at this point in the history
* Makes the header reactive, previously it would not update after you login into the website

* Persist auth status between sessions

* fix the failing test
  • Loading branch information
0xbrayo committed Feb 7, 2025
1 parent db38a62 commit a236629
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 9 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"firebase": "^9.23.0",
"pinia": "^2.1.3",
"pinia-plugin-persistedstate": "^3.2.1",
"vue": "^3.3.4",
"vue-router": "^4.2.2"
},
Expand Down
7 changes: 6 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="container-float">
<AWLHeader />
<AWLHeader v-if="showHeader" />
</div>
<div class="container">
<router-view />
Expand All @@ -19,6 +19,11 @@ export default {
components: {
AWLHeader,
AWLFooter
},
computed: {
showHeader() {
return this.$route.name === 'Login' || this.$route.name === 'Signup'
}
}
}
</script>
17 changes: 11 additions & 6 deletions src/components/Dashboard.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
<template>
<AWLHeader />
<div>
<h1>Dashboard</h1>
<p>TODO: Insert the user dashboard here</p>
<p>Username: {{ auth.user?.displayName }}</p>
<p v-if="user">Username: {{ user.email }}</p>
</div>
</template>

<script lang="ts">
import { useAuthStore } from '@/stores/auth'
import AWLHeader from '@/components/Header.vue'
import router from '@/router'
// must be logged in to see this page
export default {
name: 'AWLDashboard',
setup() {
const auth = useAuthStore()
if (!auth.isAuthenticated) {
auth.logout()
const {user, isAuthenticated, logout} = useAuthStore()
if (!isAuthenticated) {
logout()
router.push({ name: 'Login' })
}
return { auth }
return { user}
},
components: {
AWLHeader
}
}
</script>
5 changes: 5 additions & 0 deletions src/components/Leaderboard.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<AWLHeader />
<div>
<h1>Leaderboard</h1>
<table>
Expand All @@ -22,6 +23,7 @@
import { ref, onMounted } from 'vue'
import { getLeaderboard } from '@/firebase/data'
import { type ScreenTimeSummary } from '@/types'
import AWLHeader from '@/components/Header.vue'
export default {
name: 'AWLLeaderboard',
Expand All @@ -39,6 +41,9 @@ export default {
})
return { entries }
},
components: {
AWLHeader
}
}
</script>
Expand Down
12 changes: 10 additions & 2 deletions src/components/__tests__/SignupForm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { describe, it, expect } from 'vitest'
import { createPinia } from 'pinia' // Import the createPinia function from the 'pinia' package


import { mount } from '@vue/test-utils'
import SignupForm from '../SignupForm.vue'

describe('SignupForm', () => {
it('renders properly', () => {
const wrapper = mount(SignupForm, { props: { msg: 'Signup Vitest' } })
expect(wrapper.text()).toContain('Signup Vitest')
const pinia = createPinia() // Create a Pinia instance
const wrapper = mount(SignupForm, {
props: { msg: 'Signup Vitest' },
global: {
plugins: [pinia] // Use the Pinia instance as a plugin
}
})
expect(wrapper.text()).toContain('Sign Up')
})
})
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

import App from './App.vue'
import router from './router'

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)

const app = createApp(App)
app.use(pinia)
Expand Down
2 changes: 2 additions & 0 deletions src/stores/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const useAuthStore = defineStore('auth', {
state: () => ({
user: null as User | null
}),
persist: true,
getters: {
isAuthenticated: (state) => Boolean(state.user)
},
Expand All @@ -24,6 +25,7 @@ export const useAuthStore = defineStore('auth', {
},
async logout() {
await auth.signOut()
await this.$reset()
this.user = null
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/views/AboutView.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<AWLHeader />
<div class="container">
<div>
<h1>About this website</h1>
Expand Down Expand Up @@ -27,3 +28,13 @@
</div>
</div>
</template>

<script lang="ts">
import AWLHeader from '@/components/Header.vue'
export default {
name: 'AWLAboutView',
components: {
AWLHeader
}
}
</script>
11 changes: 11 additions & 0 deletions src/views/HomeView.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<AWLHeader />
<main>
<div style="max-width: 50em; margin: 5em auto">
<h1 class="mb-3 mt-5">Welcome to the ActivityWatch Leaderboard</h1>
Expand Down Expand Up @@ -56,3 +57,13 @@
</ul>
</main>
</template>

<script lang="ts">
import AWLHeader from '@/components/Header.vue'
export default {
name: 'AWLHomeView',
components: {
AWLHeader
}
}
</script>

0 comments on commit a236629

Please sign in to comment.