Skip to content

Commit

Permalink
Merge pull request #303 from shariquerik/refactor
Browse files Browse the repository at this point in the history
refactor: Removed login page & more
  • Loading branch information
shariquerik authored Aug 14, 2024
2 parents 928fdff + da64ccb commit 219447b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 128 deletions.
3 changes: 1 addition & 2 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<template>
<router-view v-if="$route.name == 'Login'" />
<Layout v-else-if="session().isLoggedIn">
<Layout v-if="session().isLoggedIn">
<router-view />
</Layout>
<Dialogs />
Expand Down
15 changes: 10 additions & 5 deletions frontend/src/components/Apps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<template #target="{ togglePopover }">
<button
:class="[
'group w-full flex h-7 items-center justify-between rounded px-2 text-base text-gray-800 hover:bg-gray-100',
active ? 'bg-gray-100' : 'text-gray-800',
'group w-full flex h-7 items-center justify-between rounded px-2 text-base hover:bg-gray-100',
]"
@click.prevent="togglePopover()"
>
Expand All @@ -20,13 +21,13 @@
<div
class="grid grid-cols-3 justify-between mx-3 p-2 rounded-lg border border-gray-100 bg-white shadow-xl"
>
<div v-for="app in apps.data" key="name">
<div v-for="app in apps.data" :key="app.name">
<a
:href="app.route"
class="flex flex-col gap-1.5 rounded justify-center items-center py-2 px-3 hover:bg-gray-100"
class="flex flex-col gap-1.5 rounded justify-center items-center py-2 px-1 hover:bg-gray-100"
>
<img class="size-8" :src="app.logo" />
<div class="text-sm" @click="app.onClick">
<div class="text-sm text-gray-700" @click="app.onClick">
{{ app.title }}
</div>
</a>
Expand All @@ -39,6 +40,10 @@
import AppsIcon from '@/components/Icons/AppsIcon.vue'
import { Popover, createResource } from 'frappe-ui'
const props = defineProps({
active: Boolean,
})
const apps = createResource({
url: 'frappe.apps.get_apps',
cache: 'apps',
Expand All @@ -47,7 +52,7 @@ const apps = createResource({
let _apps = [
{
name: 'frappe',
logo: '/assets/crm/images/desk.png',
logo: '/assets/frappe/images/framework.png',
title: __('Desk'),
route: '/app',
},
Expand Down
104 changes: 0 additions & 104 deletions frontend/src/pages/Login.vue

This file was deleted.

16 changes: 5 additions & 11 deletions frontend/src/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRouter, createWebHistory } from 'vue-router'
import { usersStore } from '@/stores/users'
import { userResource } from '@/stores/user'
import { sessionStore } from '@/stores/session'

const routes = [
Expand Down Expand Up @@ -102,11 +102,6 @@ const routes = [
name: 'Invalid Page',
component: () => import('@/pages/InvalidPage.vue'),
},
{
path: '/login',
name: 'Login',
component: () => import('@/pages/Login.vue'),
},
]

const handleMobileView = (componentName) => {
Expand Down Expand Up @@ -139,19 +134,18 @@ let router = createRouter({
})

router.beforeEach(async (to, from, next) => {
const { users } = usersStore()
const { isLoggedIn } = sessionStore()

isLoggedIn && (await users.promise)
isLoggedIn && (await userResource.promise)

if (from.meta?.scrollPos) {
from.meta.scrollPos.top = document.querySelector('#list-rows')?.scrollTop
}

if (to.name === 'Login' && isLoggedIn) {
if (to.name === 'Home' && isLoggedIn) {
next({ name: 'Leads' })
} else if (to.name !== 'Login' && !isLoggedIn) {
next({ name: 'Login' })
} else if (!isLoggedIn) {
window.location.href = "/login?redirect-to=/crm";
} else if (to.matched.length === 0) {
next({ name: 'Invalid Page' })
} else {
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/stores/session.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { defineStore } from 'pinia'
import { createResource } from 'frappe-ui'
import { usersStore } from './users'
import { userResource } from './user'
import router from '@/router'
import { ref, computed } from 'vue'

export const sessionStore = defineStore('crm-session', () => {
const { users } = usersStore()

function sessionUser() {
let cookies = new URLSearchParams(document.cookie.split('; ').join('&'))
let _sessionUser = cookies.get('user_id')
Expand All @@ -25,7 +23,7 @@ export const sessionStore = defineStore('crm-session', () => {
throw new Error('Invalid email or password')
},
onSuccess() {
users.reload()
userResource.reload()
user.value = sessionUser()
login.reset()
router.replace({ path: '/' })
Expand All @@ -35,9 +33,9 @@ export const sessionStore = defineStore('crm-session', () => {
const logout = createResource({
url: 'logout',
onSuccess() {
users.reset()
userResource.reset()
user.value = null
router.replace({ name: 'Login' })
router.replace({ name: 'Home' })
},
})

Expand Down
12 changes: 12 additions & 0 deletions frontend/src/stores/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import router from '@/router'
import { createResource } from 'frappe-ui'

export const userResource = createResource({
url: 'frappe.auth.get_logged_user',
cache: 'User',
onError(error) {
if (error && error.exc_type === 'AuthenticationError') {
router.push({ name: 'Home' })
}
},
})

0 comments on commit 219447b

Please sign in to comment.