Refactor sidebar state to allow for multiple sidebars

This commit is contained in:
Ramon Wenger 2020-06-02 09:37:12 +02:00
parent 6d43226ebb
commit 5db13e9124
13 changed files with 73 additions and 38 deletions

View File

@ -10,7 +10,7 @@
<div class="user-header"> <div class="user-header">
<a <a
class="user-header__sidebar-link" class="user-header__sidebar-link"
@click="openSidebar()"> @click="openSidebar('profile')">
<current-class class="user-header__current-class"/> <current-class class="user-header__current-class"/>
</a> </a>

View File

@ -5,7 +5,7 @@
<div <div
class="user-widget__avatar" class="user-widget__avatar"
data-cy="user-widget-avatar" data-cy="user-widget-avatar"
@click="openSidebar()"> @click="openSidebar('profile')">
<avatar <avatar
:avatar-url="avatarUrl" :avatar-url="avatarUrl"
:icon-highlighted="isProfile"/> :icon-highlighted="isProfile"/>

View File

@ -7,7 +7,7 @@
tag="div" tag="div"
class="book-topics__topic book-subnavigation__item" class="book-topics__topic book-subnavigation__item"
v-for="topic in topics" v-for="topic in topics"
@click.native="hideMobileNavigation"> @click.native="closeSidebar('navigation')">
{{ topic.order }}. {{ topic.order }}.
{{ topic.title }} {{ topic.title }}
</router-link> </router-link>
@ -16,6 +16,7 @@
<script> <script>
import ALL_TOPICS_QUERY from '@/graphql/gql/allTopicsQuery.gql'; import ALL_TOPICS_QUERY from '@/graphql/gql/allTopicsQuery.gql';
import sidebarMixin from '@/mixins/sidebar';
export default { export default {
props: { props: {
@ -24,6 +25,8 @@
} }
}, },
mixins: [sidebarMixin],
data() { data() {
return { return {
topics: [] topics: []
@ -33,9 +36,6 @@
methods: { methods: {
topicId(id) { topicId(id) {
return atob(id) return atob(id)
},
hideMobileNavigation() {
this.$store.dispatch('showMobileNavigation', false);
} }
}, },

View File

@ -1,12 +1,14 @@
<template> <template>
<div class="mobile-navigation"> <div
class="navigation-sidebar"
v-if="sidebar.navigation">
<content-navigation <content-navigation
:mobile="true" :is-sidebar="true"
class="mobile-navigation__main"/> class="navigation-sidebar__main"/>
<div <div
class="mobile-navigation__close-button" class="navigation-sidebar__close-button"
@click="hideMobileNavigation"> @click="close">
<cross class="mobile-navigation__close-icon"/> <cross class="navigation-sidebar__close-icon"/>
</div> </div>
</div> </div>
</template> </template>
@ -15,9 +17,13 @@
import Cross from '@/components/icons/Cross'; import Cross from '@/components/icons/Cross';
import ContentNavigation from '@/components/book-navigation/ContentNavigation'; import ContentNavigation from '@/components/book-navigation/ContentNavigation';
import sidebarMixin from '@/mixins/sidebar';
import {meQuery} from '@/graphql/queries'; import {meQuery} from '@/graphql/queries';
export default { export default {
mixins: [sidebarMixin],
components: { components: {
ContentNavigation, ContentNavigation,
Cross Cross
@ -30,8 +36,8 @@
}, },
methods: { methods: {
hideMobileNavigation() { close() {
this.$store.dispatch('showMobileNavigation', false); this.closeSidebar('navigation');
} }
}, },
@ -45,7 +51,7 @@
@import "@/styles/_variables.scss"; @import "@/styles/_variables.scss";
@import "@/styles/_mixins.scss"; @import "@/styles/_mixins.scss";
.mobile-navigation { .navigation-sidebar {
position: fixed; position: fixed;
left: 0; left: 0;
right: 0; right: 0;
@ -69,7 +75,8 @@
overflow-y: auto; overflow-y: auto;
@include desktop { @include desktop {
display: none; //display: none;
width: 285px;
} }
&__main { &__main {

View File

@ -69,7 +69,7 @@
this.$apollo.mutate({ this.$apollo.mutate({
mutation: TOGGLE_SIDEBAR, mutation: TOGGLE_SIDEBAR,
variables: { variables: {
open: false profile: false
} }
}) })
} }

View File

@ -2,32 +2,34 @@
<div <div
v-click-outside="closeSidebar" v-click-outside="closeSidebar"
class="profile-sidebar" class="profile-sidebar"
v-if="sidebar.open"> v-if="sidebar.profile">
<a <a
class="profile-sidebar__close-link" class="profile-sidebar__close-link"
@click="closeSidebar"> @click="close">
<cross class="profile-sidebar__close-icon"/> <cross class="profile-sidebar__close-icon"/>
</a> </a>
<profile-widget class="profile-sidebar__item"/> <profile-widget class="profile-sidebar__item"/>
<div <div
class="profile-sidebar__item" class="profile-sidebar__item"
@click="closeSidebar"> @click="close">
<router-link <router-link
to="/me/activity" to="/me/activity"
class="profile-sidebar__link">Meine Aktivitäten</router-link> class="profile-sidebar__link">Meine Aktivitäten
</router-link>
</div> </div>
<div class="profile-sidebar__item"> <div class="profile-sidebar__item">
<h3 class="profile-sidebar__subtitle">Klasse</h3> <h3 class="profile-sidebar__subtitle">Klasse</h3>
<class-selection-widget/> <class-selection-widget/>
<div @click="closeSidebar"> <div @click="close">
<router-link <router-link
:to="{name: 'my-class'}" :to="{name: 'my-class'}"
class="profile-sidebar__link">Klassenliste anzeigen</router-link> class="profile-sidebar__link">Klassenliste anzeigen
</router-link>
</div> </div>
</div> </div>
<div <div
class="profile-sidebar__item" class="profile-sidebar__item"
@click="closeSidebar"> @click="close">
<router-link <router-link
:to="{name:'join-class'}" :to="{name:'join-class'}"
data-cy="join-class-link" data-cy="join-class-link"
@ -57,12 +59,19 @@
export default { export default {
mixins: [sidebarMixin], mixins: [sidebarMixin],
components: { components: {
LogoutWidget, LogoutWidget,
ClassSelectionWidget, ClassSelectionWidget,
ProfileWidget, ProfileWidget,
Cross Cross
}, },
methods: {
close() {
this.closeSidebar('profile')
}
},
} }
</script> </script>

View File

@ -17,7 +17,9 @@ const writeLocalCache = cache => {
}, },
sidebar: { sidebar: {
__typename: 'Sidebar', __typename: 'Sidebar',
open: false open: false,
profile: false,
navigation: false
}, },
helloEmail: { helloEmail: {
__typename: 'HelloEmail', __typename: 'HelloEmail',

View File

@ -1,3 +1,3 @@
mutation($open: Boolean!) { mutation($sidebar: SidebarInput!) {
toggleSidebar(open: $open) @client toggleSidebar(sidebar: $sidebar) @client
} }

View File

@ -1,5 +1,7 @@
query Sidebar { query Sidebar {
sidebar @client { sidebar @client {
open open
profile
navigation
} }
} }

View File

@ -16,9 +16,14 @@ export const resolvers = {
cache.writeQuery({query: HELLO_EMAIL, data}); cache.writeQuery({query: HELLO_EMAIL, data});
return data.helloEmail; return data.helloEmail;
}, },
toggleSidebar: (_, {open}, {cache}) => { toggleSidebar: (_, {sidebar: {profile, navigation}}, {cache}) => {
const data = cache.readQuery({query: SIDEBAR}); const data = cache.readQuery({query: SIDEBAR});
data.sidebar.open = open; if (typeof profile !== 'undefined') {
data.sidebar.profile = profile;
}
if (typeof navigation !== 'undefined') {
data.sidebar.navigation = navigation;
}
cache.writeQuery({query: SIDEBAR, data}); cache.writeQuery({query: SIDEBAR, data});
return data.sidebar; return data.sidebar;
} }

View File

@ -1,6 +1,11 @@
import gql from 'graphql-tag'; import gql from 'graphql-tag';
export const typeDefs = gql` export const typeDefs = gql`
type SidebarInput {
navigation: Boolean
profile: Boolean
}
type ScrollPosition { type ScrollPosition {
scrollTo: String! scrollTo: String!
} }
@ -12,13 +17,13 @@ export const typeDefs = gql`
type Sidebar { type Sidebar {
open: Boolean! open: Boolean!
navigation: Boolean
profile: Boolean
} }
type Mutation { type Mutation {
scrollTo(scrollTo: String!): ScrollPosition scrollTo(scrollTo: String!): ScrollPosition
}
type Mutation {
helloEmail(email: String!): HelloEmail helloEmail(email: String!): HelloEmail
toggleSidebar(sidebar: SidebarInput!): Sidebar
} }
`; `;

View File

@ -2,12 +2,14 @@ import TOGGLE_SIDEBAR from '@/graphql/gql/local/mutations/toggleSidebar.gql';
export default { export default {
methods: { methods: {
openSidebar() { openSidebar(type) {
this.$nextTick(() => { // we don't want this to happen instantly, only almost instantly. Otherwise the click-outside-directive won't work this.$nextTick(() => { // we don't want this to happen instantly, only almost instantly. Otherwise the click-outside-directive won't work
this.$apollo.mutate({ this.$apollo.mutate({
mutation: TOGGLE_SIDEBAR, mutation: TOGGLE_SIDEBAR,
variables: { variables: {
open: true sidebar: {
[type]: true
}
} }
}); });
}); });

View File

@ -3,12 +3,14 @@ import TOGGLE_SIDEBAR from '@/graphql/gql/local/mutations/toggleSidebar.gql';
export default { export default {
methods: { methods: {
closeSidebar() { closeSidebar(type) {
if (this.sidebar.open) { if (this.sidebar[type]) {
this.$apollo.mutate({ this.$apollo.mutate({
mutation: TOGGLE_SIDEBAR, mutation: TOGGLE_SIDEBAR,
variables: { variables: {
open: false sidebar: {
[type]: false
}
} }
}); });
} }
@ -23,7 +25,8 @@ export default {
data: () => ({ data: () => ({
sidebar: { sidebar: {
open: false profile: false,
navigation: false
} }
}) })
} }