Skip to content

Commit

Permalink
Inplement about user property; Add "me" page
Browse files Browse the repository at this point in the history
Hide error element if empty
  • Loading branch information
simonwep committed Mar 17, 2019
1 parent 286aa54 commit 9a89459
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 39 deletions.
13 changes: 13 additions & 0 deletions src/router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import PasswordReset from '../vue/pages/forgotpassword/ForgotPassword';
import ViewPost from '../vue/pages/viewpost/ViewPost';
import ViewUser from '../vue/pages/viewuser/ViewUser';
import Search from '../vue/pages/search/Search';
import Me from '../vue/pages/me/Me';

/**
* Responsible for trying to authenticate the user via a existing api-key.
Expand Down Expand Up @@ -154,6 +155,18 @@ export default [
path: '/search',
component: Search
},
{
path: '/me',
component: Me,
beforeEnter(to, from, next) {
setTitle('Me');

/**
* Check if user is logged in.
*/
authenticate().then(() => next()).catch(() => next('/login'));
}
},

// 404 catcher
{path: '*', redirect: '/'}
Expand Down
4 changes: 4 additions & 0 deletions src/scss/components/_general.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
&:not(:empty){
margin-bottom: 0.75em;
}

&:empty {
display: none;
}
}
23 changes: 13 additions & 10 deletions src/store/modules/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ export const auth = {
query: {
operation: 'login',
vars: {username, password},
fields: ['apikey', 'email', 'permissions', 'id', 'fullname']
fields: ['apikey', 'email', 'permissions', 'id', 'fullname', 'about']
}
}).then(({errors, data}) => {

// Check for errors and, if present, return the message of the first one
if (errors && errors.length) {
throw errors[0].message;
} else {
const {apikey, id, email, fullname, permissions} = data.login;
const {apikey, id, email, fullname, permissions, about} = data.login;

state.apikey = apikey;
state.user = {
email,
id,
permissions,
username,
fullname
fullname,
about
};

// Save apikey and return loaded user
Expand All @@ -81,7 +82,7 @@ export const auth = {
query: {
operation: 'login',
vars: {apikey},
fields: ['email', 'permissions', 'id', 'fullname', 'username']
fields: ['email', 'permissions', 'id', 'fullname', 'username', 'about']
}
}).then(({errors, data}) => {

Expand All @@ -94,13 +95,14 @@ export const auth = {
} else {

// Save to current state
const {id, email, fullname, username, permissions} = data.login;
const {id, email, fullname, username, permissions, about} = data.login;
state.user = {
email,
id,
permissions,
username,
fullname
fullname,
about
};

return state.user;
Expand Down Expand Up @@ -142,16 +144,16 @@ export const auth = {
* @param fullname New Fullname
* @param password New Password
*/
async updateCredentials({state}, {email, fullname, password}) {
async updateProfileData({state}, {about, email, fullname, password}) {
return this.dispatch('graphql', {
query: {
operation: 'updateUser',
vars: {
email, fullname, password,
email, fullname, password, about,
id: state.user.id,
apikey: state.apikey
},
fields: ['email', 'fullname', 'permissions']
fields: ['email', 'fullname', 'permissions', 'about']
}
}).then(({errors, data}) => {

Expand All @@ -161,9 +163,10 @@ export const auth = {
} else {

// Update changes in current state
const {email, fullname, permissions} = data.updateUser;
const {email, fullname, permissions, about} = data.updateUser;
state.user = {
...state.user,
about,
email,
fullname,
permissions
Expand Down
4 changes: 2 additions & 2 deletions src/store/modules/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const users = {
query: {
operation: 'getAllUsers',
vars: {apikey},
fields: ['id', 'username', 'fullname', 'permissions', 'deactivated', 'email']
fields: ['id', 'username', 'fullname', 'permissions', 'deactivated', 'email', 'about']
}
}).then(({errors, data: {getAllUsers}}) => {

Expand Down Expand Up @@ -50,7 +50,7 @@ export const users = {
query: {
operation: 'user',
vars: {id, apikey},
fields: ['id', 'username', 'fullname', 'permissions', 'deactivated', 'email']
fields: ['id', 'username', 'fullname', 'permissions', 'deactivated', 'email', 'about']
}
}).then(({errors, data: {user}}) => {

Expand Down
1 change: 0 additions & 1 deletion src/vue/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
textarea,
input,
[contenteditable] {
font-family: $font-family;
outline: none;
border: none;
background: transparent;
Expand Down
8 changes: 7 additions & 1 deletion src/vue/Header.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div :class="{header: 1, visible: $store.state.page.lastScrollDirection === 'up'}">
<div :class="{header: 1, visible: $store.state.page.lastScrollDirection !== 'down'}">

<div class="bar">

Expand All @@ -26,9 +26,11 @@

<!-- Visible as not-logged-in user -->
<router-link to="/">Home</router-link>

<router-link v-if="!user"
to="/login">Login
</router-link>

<router-link v-if="!user"
to="/register">Register
</router-link>
Expand All @@ -47,11 +49,15 @@
<router-link v-if="user"
to="/settings">Settings
</router-link>

<router-link v-if="user"
to="/login"
@click.native="logout">Logout
</router-link>

<router-link v-if="user"
to="me">Profile</router-link>

</div>
</div>

Expand Down
79 changes: 79 additions & 0 deletions src/vue/pages/me/Me.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<template>
<div class="settings small">

<h1>{{ auth.user.fullname }} aka. {{ auth.user.username }}</h1>

<text-area-input-field ref="inAbout"
:no-border="true"

placeholder="Enter a short bio..."/>

<p class="error">{{ errorMsg }}</p>

<button class="button-primary" @click="submit">Update</button>
</div>
</template>

<script>
// UI Components
import TextInputField from '../../ui/TextInputField';
import TextAreaInputField from '../../ui/TextAreaInputField';
// Vuex stuff
import {mapState} from 'vuex';
export default {
components: {TextInputField, TextAreaInputField},
data() {
return {
errorMsg: ''
};
},
computed: {
...mapState(['auth'])
},
mounted() {
this.$refs.inAbout.value = this.auth.user.about || '';
},
methods: {
submit() {
const {inAbout} = this.$refs;
this.errorMsg = '';
// Fire auth
this.$store.dispatch('auth/updateProfileData', {
about: inAbout.value || null
}).catch(reason => {
this.errorMsg = reason;
});
}
}
};
</script>

<style lang="scss" scoped>
.settings {
.error {
margin-top: 2em;
}
> button {
width: 100%;
margin-top: 0.25em;
}
.text-area-input-field {
font-family: $font-family-article-stack;
}
}
</style>
8 changes: 4 additions & 4 deletions src/vue/pages/settings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<script>
// UI Components
import TextInputField from '../../ui/TextInputField';
import TextInputField from '../../ui/TextInputField';
// Vuex stuff
import {mapState} from 'vuex';
Expand Down Expand Up @@ -76,7 +76,7 @@
}
// Fire auth
this.$store.dispatch('auth/updateCredentials', {
this.$store.dispatch('auth/updateProfileData', {
fullname: inFullName.value || null,
email: inEmail.value || null,
password: inPassword.value || null
Expand All @@ -99,12 +99,12 @@
.settings {
.error {
margin-top: 2em;
margin-top: 1em;
}
> button {
width: 100%;
margin-top: 0.25em;
margin-top: 1.5em;
}
.text-input-field {
Expand Down
23 changes: 3 additions & 20 deletions src/vue/pages/viewpost/ViewPost.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,36 +181,19 @@
}
> span {
position: relative;
font-size: 0.8em;
font-weight: 500;
background: rgba($palette-slate-gray, 0.07);
background: rgba($palette-slate-gray, 0.05);
color: rgba($palette-slate-gray, 0.75);
margin: 0 0.5em 0.5em 0;
border-radius: 0.15em;
padding: 0.55em 0.85em 0.5em;
cursor: pointer;
transition: all 0.3s;
overflow: hidden;
z-index: 1;
&::before {
@include pseudo();
@include position(0, 0, auto, 0);
@include size(100%, 0);
background: $palette-sweet-red;
transition: all 0.3s;
z-index: -1;
opacity: 0;
}
&:hover {
color: white;
&::before {
height: 100%;
opacity: 1;
}
background: rgba($palette-slate-gray, 0.1);
color: rgba($palette-slate-gray, 0.85);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/vue/ui/TextAreaInputField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@keyup.enter="$emit('submit')"></textarea>

<!-- Colored border to show focus -->
<span :class="{border: 1, active: focused}"></span>
<span v-if="!noBorder" :class="{border: 1, active: focused}"></span>

</div>
</template>
Expand All @@ -26,6 +26,7 @@
export default {
props: {
placeholder: {type: String, required: true},
noBorder: {type: Boolean, default: false},
password: {type: Boolean, default: false},
autofocus: {type: Boolean, default: false}
},
Expand Down

0 comments on commit 9a89459

Please sign in to comment.