Skip to content

Commit

Permalink
refactor: add const
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 17, 2023
1 parent 801c8b7 commit e6a53ad
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
6 changes: 3 additions & 3 deletions apps/frontend/src/components/header/TheHeader.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useGoto } from '@/composables/goto'
import { openGithub, useGoto } from '@/composables/goto'
const { gotoHome, gotoGithub, gotoSettings } = useGoto()
const { gotoHome, gotoSettings } = useGoto()
</script>

<template>
Expand All @@ -13,7 +13,7 @@ const { gotoHome, gotoGithub, gotoSettings } = useGoto()
<button class="mx-2 !outline-none" @click="gotoSettings()">
<div i="carbon-settings" />
</button>
<button class="mx-2 !outline-none" @click="gotoGithub()">
<button class="mx-2 !outline-none" @click="openGithub()">
<div i="mdi-github" />
</button>
</div>
Expand Down
17 changes: 9 additions & 8 deletions apps/frontend/src/composables/goto.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import { useRouter } from 'vue-router'
import { RouteNames } from '@/router/const'

export function useGoto() {
const router = useRouter()

function gotoHome() {
router.push({
name: 'Home',
name: RouteNames.HOME,
})
}

function gotoSettings() {
router.push({
name: 'Settings',
name: RouteNames.SETTINGS,
})
}

function gotoSettingsTheme() {
router.push({
name: 'SettingsTheme',
name: RouteNames.SETTINGS_THEME,
})
}

function gotoGithub() {
window.open('https://github.com/cuixueshe/dida')
}

return {
gotoHome,
gotoSettings,
gotoGithub,
gotoSettingsTheme,
}
}

export const GITHUB_URL = 'https://github.com/cuixueshe/dida'
export function openGithub() {
window.open(GITHUB_URL)
}
25 changes: 16 additions & 9 deletions apps/frontend/src/composables/tests/goto.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import { useGoto } from '../goto'
import { GITHUB_URL, openGithub, useGoto } from '../goto'
import { useSetup } from '@/tests/helper'
import { RouteNames } from '@/router/const'

describe('goto', () => {
it('should be go to Settings', () => {
Expand All @@ -9,7 +10,7 @@ describe('goto', () => {
gotoSettings()
})

expect(router.push).toBeCalledWith({ name: 'Settings' })
expect(router.push).toBeCalledWith({ name: RouteNames.SETTINGS })
})

it('should be go to Home', () => {
Expand All @@ -18,17 +19,23 @@ describe('goto', () => {
gotoHome()
})

expect(router.push).toBeCalledWith({ name: 'Home' })
expect(router.push).toBeCalledWith({ name: RouteNames.HOME })
})

it('should be go to github', () => {
it('should be go to SettingsTheme', () => {
const { router } = useSetup(() => {
const { gotoSettingsTheme } = useGoto()
gotoSettingsTheme()
})

expect(router.push).toBeCalledWith({ name: RouteNames.SETTINGS_THEME })
})

it('should be open github url', () => {
window.open = vi.fn()

useSetup(() => {
const { gotoGithub } = useGoto()
gotoGithub()
})
openGithub()

expect(window.open).toBeCalledWith('https://github.com/cuixueshe/dida')
expect(window.open).toBeCalledWith(GITHUB_URL)
})
})
6 changes: 6 additions & 0 deletions apps/frontend/src/router/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum RouteNames {
HOME = 'Home',
TASK = 'Task',
SETTINGS = 'Settings',
SETTINGS_THEME = 'SettingsTheme',
}
5 changes: 3 additions & 2 deletions apps/frontend/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import type { RouteRecordRaw, Router } from 'vue-router'
import { createRouter, createWebHashHistory } from 'vue-router'
import type { App } from 'vue'
import { SettingsRoute } from './settings'
import { RouteNames } from './const'
import Task from '@/pages/Task.vue'
import { getDiscreteApi } from '@/composables/useNaiveDiscreteApi'

export const routes: RouteRecordRaw[] = [
{
path: '/',
redirect: '/task',
name: 'Home',
name: RouteNames.HOME,
},
{ path: '/task', component: Task, name: 'Task' },
{ path: '/task', component: Task, name: RouteNames.TASK },
SettingsRoute,
]

Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/src/router/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RouteRecordRaw } from 'vue-router'
import type { Component } from 'vue'
import { RouteNames } from './const'
import Settings from '@/pages/Settings.vue'
import { sidebars } from '@/composables/settings'

Expand All @@ -19,7 +20,7 @@ const subRoutes: RouteRecordRaw[] = sidebars.map(sidebar => ({
export const SettingsRoute = {
path: `/${SETTINGS_PATH}`,
component: Settings,
name: 'Settings',
name: RouteNames.SETTINGS,
meta: { title: '设置' },
children: subRoutes,
redirect: subRoutes[0].path,
Expand Down

1 comment on commit e6a53ad

@vercel
Copy link

@vercel vercel bot commented on e6a53ad Jun 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.