Add identity pickers on event creation & join
Also it now saves current actor in localStorage and initalizes it in Apollo Cache (just like user stuff). This allows not relying on loggedPerson query anymore. Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
59
js/src/views/Account/IdentityPicker.vue
Normal file
59
js/src/views/Account/IdentityPicker.vue
Normal file
@@ -0,0 +1,59 @@
|
||||
<template>
|
||||
<div class="identity-picker">
|
||||
<img class="image" v-if="currentIdentity.avatar" :src="currentIdentity.avatar.url" :alt="currentIdentity.avatar.alt"/> {{ currentIdentity.name || `@${currentIdentity.preferredUsername}` }}
|
||||
<b-button type="is-text" @click="isComponentModalActive = true"><translate>Change</translate></b-button>
|
||||
<b-modal :active.sync="isComponentModalActive" has-modal-card>
|
||||
<div class="modal-card">
|
||||
<header class="modal-card-head">
|
||||
<p class="modal-card-title">Pick an identity</p>
|
||||
</header>
|
||||
<section class="modal-card-body">
|
||||
<div class="list is-hoverable">
|
||||
<a class="list-item" v-for="identity in identities" :class="{ 'is-active': identity.id === currentIdentity.id }" @click="changeCurrentIdentity(identity)">
|
||||
<div class="media">
|
||||
<img class="media-left image" v-if="identity.avatar" :src="identity.avatar.url" />
|
||||
<div class="media-content">
|
||||
<h3>@{{ identity.preferredUsername }}</h3>
|
||||
<small>{{ identity.name }}</small>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</b-modal>
|
||||
</div>
|
||||
</template>
|
||||
<script lang="ts">
|
||||
import { Component, Prop, Vue } from 'vue-property-decorator';
|
||||
import { IActor } from '@/types/actor';
|
||||
import { IDENTITIES } from '@/graphql/actor';
|
||||
|
||||
@Component({
|
||||
apollo: {
|
||||
identities: {
|
||||
query: IDENTITIES,
|
||||
},
|
||||
},
|
||||
})
|
||||
export default class IdentityPicker extends Vue {
|
||||
@Prop() value!: IActor;
|
||||
isComponentModalActive: boolean = false;
|
||||
identities: IActor[] = [];
|
||||
currentIdentity: IActor = this.value;
|
||||
|
||||
changeCurrentIdentity(identity: IActor) {
|
||||
this.currentIdentity = identity;
|
||||
this.$emit('input', identity);
|
||||
this.isComponentModalActive = false;
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
<style lang="scss">
|
||||
.identity-picker img.image {
|
||||
display: inline;
|
||||
height: 1.5em;
|
||||
vertical-align: text-bottom;
|
||||
}
|
||||
</style>
|
||||
@@ -1,9 +1,9 @@
|
||||
<template>
|
||||
<section class="container">
|
||||
<div v-if="loggedPerson">
|
||||
<div v-if="currentActor">
|
||||
<div class="header">
|
||||
<figure v-if="loggedPerson.banner" class="image is-3by1">
|
||||
<img :src="loggedPerson.banner.url" alt="banner">
|
||||
<figure v-if="currentActor.banner" class="image is-3by1">
|
||||
<img :src="currentActor.banner.url" alt="banner">
|
||||
</figure>
|
||||
</div>
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
</style>
|
||||
|
||||
<script lang="ts">
|
||||
import { LOGGED_PERSON } from '@/graphql/actor';
|
||||
import { CURRENT_ACTOR_CLIENT } from '@/graphql/actor';
|
||||
import { Component, Vue, Watch } from 'vue-property-decorator';
|
||||
import EventCard from '@/components/Event/EventCard.vue';
|
||||
import { IPerson } from '@/types/actor';
|
||||
@@ -42,17 +42,18 @@ import Identities from '@/components/Account/Identities.vue';
|
||||
EventCard,
|
||||
Identities,
|
||||
},
|
||||
apollo: {
|
||||
currentActor: {
|
||||
query: CURRENT_ACTOR_CLIENT,
|
||||
},
|
||||
},
|
||||
})
|
||||
export default class MyAccount extends Vue {
|
||||
loggedPerson: IPerson | null = null;
|
||||
currentActor!: IPerson;
|
||||
currentIdentityName: string | null = null;
|
||||
|
||||
@Watch('$route.params.identityName', { immediate: true })
|
||||
async onIdentityParamChanged (val: string) {
|
||||
if (!this.loggedPerson) {
|
||||
this.loggedPerson = await this.loadLoggedPerson();
|
||||
}
|
||||
|
||||
await this.redirectIfNoIdentitySelected(val);
|
||||
|
||||
this.currentIdentityName = val;
|
||||
@@ -61,18 +62,10 @@ export default class MyAccount extends Vue {
|
||||
private async redirectIfNoIdentitySelected (identityParam?: string) {
|
||||
if (!!identityParam) return;
|
||||
|
||||
if (!!this.loggedPerson) {
|
||||
this.$router.push({ params: { identityName: this.loggedPerson.preferredUsername } });
|
||||
if (!!this.currentActor) {
|
||||
await this.$router.push({ params: { identityName: this.currentActor.preferredUsername } });
|
||||
}
|
||||
}
|
||||
|
||||
private async loadLoggedPerson () {
|
||||
const result = await this.$apollo.query({
|
||||
query: LOGGED_PERSON,
|
||||
});
|
||||
|
||||
return result.data.loggedPerson as IPerson;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="scss">
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
</a>
|
||||
</b-dropdown-item>
|
||||
</b-dropdown>
|
||||
<a class="button" v-if="loggedPerson.id === person.id" @click="createToken">
|
||||
<a class="button" v-if="currentActor.id === person.id" @click="createToken">
|
||||
<translate>Create token</translate>
|
||||
</a>
|
||||
</div>
|
||||
@@ -79,7 +79,7 @@
|
||||
<a
|
||||
class="button"
|
||||
@click="deleteProfile()"
|
||||
v-if="loggedPerson && loggedPerson.id === person.id"
|
||||
v-if="currentActor && currentActor.id === person.id"
|
||||
>
|
||||
<translate>Delete</translate>
|
||||
</a>
|
||||
@@ -91,7 +91,7 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { FETCH_PERSON, LOGGED_PERSON } from '@/graphql/actor';
|
||||
import { FETCH_PERSON, CURRENT_ACTOR_CLIENT } from '@/graphql/actor';
|
||||
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
|
||||
import EventCard from '@/components/Event/EventCard.vue';
|
||||
import { MOBILIZON_INSTANCE_HOST } from '@/api/_entrypoint';
|
||||
@@ -108,8 +108,8 @@ import { CREATE_FEED_TOKEN_ACTOR } from '@/graphql/feed_tokens';
|
||||
};
|
||||
},
|
||||
},
|
||||
loggedPerson: {
|
||||
query: LOGGED_PERSON,
|
||||
currentActor: {
|
||||
query: CURRENT_ACTOR_CLIENT,
|
||||
},
|
||||
},
|
||||
components: {
|
||||
@@ -120,6 +120,7 @@ export default class MyAccount extends Vue {
|
||||
@Prop({ type: String, required: true }) name!: string;
|
||||
|
||||
person!: IPerson;
|
||||
currentActor!: IPerson;
|
||||
|
||||
// call again the method if the route changes
|
||||
@Watch('$route')
|
||||
|
||||
@@ -25,6 +25,16 @@
|
||||
<b-input type="textarea" aria-required="false" v-model="identity.summary"/>
|
||||
</b-field>
|
||||
|
||||
<b-notification
|
||||
type="is-danger"
|
||||
has-icon
|
||||
aria-close-label="Close notification"
|
||||
role="alert"
|
||||
v-for="error in errors"
|
||||
>
|
||||
{{ error }}
|
||||
</b-notification>
|
||||
|
||||
<b-field class="submit">
|
||||
<div class="control">
|
||||
<button v-translate type="button" class="button is-primary" @click="submit()">
|
||||
@@ -70,19 +80,32 @@
|
||||
|
||||
<script lang="ts">
|
||||
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
|
||||
import { CREATE_PERSON, DELETE_PERSON, FETCH_PERSON, IDENTITIES, LOGGED_PERSON, UPDATE_PERSON } from '../../../graphql/actor';
|
||||
import {
|
||||
CREATE_PERSON,
|
||||
CURRENT_ACTOR_CLIENT,
|
||||
DELETE_PERSON,
|
||||
FETCH_PERSON,
|
||||
IDENTITIES,
|
||||
UPDATE_PERSON,
|
||||
} from '@/graphql/actor';
|
||||
import { IPerson, Person } from '@/types/actor';
|
||||
import PictureUpload from '@/components/PictureUpload.vue';
|
||||
import { MOBILIZON_INSTANCE_HOST } from '@/api/_entrypoint';
|
||||
import { Dialog } from 'buefy/dist/components/dialog';
|
||||
import { RouteName } from '@/router';
|
||||
import { buildFileFromIPicture, buildFileVariable } from '@/utils/image';
|
||||
import { changeIdentity, saveActorData } from '@/utils/auth';
|
||||
|
||||
@Component({
|
||||
components: {
|
||||
PictureUpload,
|
||||
Dialog,
|
||||
},
|
||||
apollo: {
|
||||
currentActor: {
|
||||
query: CURRENT_ACTOR_CLIENT,
|
||||
},
|
||||
},
|
||||
})
|
||||
export default class EditIdentity extends Vue {
|
||||
@Prop({ type: Boolean }) isUpdate!: boolean;
|
||||
@@ -94,7 +117,7 @@ export default class EditIdentity extends Vue {
|
||||
identity = new Person();
|
||||
|
||||
private oldDisplayName: string | null = null;
|
||||
private loggedPerson: IPerson | null = null;
|
||||
private currentActor: IPerson | null = null;
|
||||
|
||||
@Watch('isUpdate')
|
||||
async isUpdateChanged () {
|
||||
@@ -134,6 +157,9 @@ export default class EditIdentity extends Vue {
|
||||
this.oldDisplayName = newDisplayName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an identity
|
||||
*/
|
||||
async deleteIdentity() {
|
||||
try {
|
||||
await this.$apollo.mutate({
|
||||
@@ -150,17 +176,15 @@ export default class EditIdentity extends Vue {
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
this.$notifier.success(
|
||||
this.$gettextInterpolate('Identity %{displayName} deleted', { displayName: this.identity.displayName() }),
|
||||
);
|
||||
|
||||
await this.loadLoggedPersonIfNeeded();
|
||||
|
||||
// Refresh the loaded person if we deleted the default identity
|
||||
if (this.loggedPerson && this.identity.id === this.loggedPerson.id) {
|
||||
this.loggedPerson = null;
|
||||
await this.loadLoggedPersonIfNeeded(true);
|
||||
/**
|
||||
* If we just deleted the current identity, we need to change it to the next one
|
||||
*/
|
||||
const data = this.$apollo.provider.defaultClient.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES });
|
||||
if (data) {
|
||||
await this.maybeUpdateCurrentActorCache(data.identities[0]);
|
||||
}
|
||||
|
||||
await this.redirectIfNoIdentitySelected();
|
||||
@@ -181,6 +205,7 @@ export default class EditIdentity extends Vue {
|
||||
const index = data.identities.findIndex(i => i.id === this.identity.id);
|
||||
|
||||
this.$set(data.identities, index, updatePerson);
|
||||
this.maybeUpdateCurrentActorCache(updatePerson);
|
||||
|
||||
store.writeQuery({ query: IDENTITIES, data });
|
||||
}
|
||||
@@ -211,11 +236,11 @@ export default class EditIdentity extends Vue {
|
||||
},
|
||||
});
|
||||
|
||||
this.$router.push({ name: RouteName.UPDATE_IDENTITY, params: { identityName: this.identity.preferredUsername } });
|
||||
|
||||
this.$notifier.success(
|
||||
this.$gettextInterpolate('Identity %{displayName} created', { displayName: this.identity.displayName() }),
|
||||
this.$gettextInterpolate('Identity %{displayName} created', { displayName: this.identity.displayName() }),
|
||||
);
|
||||
|
||||
await this.$router.push({ name: RouteName.UPDATE_IDENTITY, params: { identityName: this.identity.preferredUsername } });
|
||||
} catch (err) {
|
||||
this.handleError(err);
|
||||
}
|
||||
@@ -263,9 +288,11 @@ export default class EditIdentity extends Vue {
|
||||
private handleError(err: any) {
|
||||
console.error(err);
|
||||
|
||||
err.graphQLErrors.forEach(({ message }) => {
|
||||
this.errors.push(message);
|
||||
});
|
||||
if (err.graphQLErrors !== undefined) {
|
||||
err.graphQLErrors.forEach(({ message }) => {
|
||||
this.$notifier.error(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private convertToUsername(value: string | null) {
|
||||
@@ -287,20 +314,29 @@ export default class EditIdentity extends Vue {
|
||||
|
||||
await this.loadLoggedPersonIfNeeded();
|
||||
|
||||
if (!!this.loggedPerson) {
|
||||
this.$router.push({ params: { identityName: this.loggedPerson.preferredUsername } });
|
||||
if (!!this.currentActor) {
|
||||
await this.$router.push({ params: { identityName: this.currentActor.preferredUsername } });
|
||||
}
|
||||
}
|
||||
|
||||
private async maybeUpdateCurrentActorCache(identity: IPerson) {
|
||||
if (this.currentActor) {
|
||||
if (this.currentActor.preferredUsername === this.identity.preferredUsername) {
|
||||
await changeIdentity(this.$apollo.provider.defaultClient, identity);
|
||||
}
|
||||
this.currentActor = identity;
|
||||
}
|
||||
}
|
||||
|
||||
private async loadLoggedPersonIfNeeded (bypassCache = false) {
|
||||
if (this.loggedPerson) return;
|
||||
if (this.currentActor) return;
|
||||
|
||||
const result = await this.$apollo.query({
|
||||
query: LOGGED_PERSON,
|
||||
query: CURRENT_ACTOR_CLIENT,
|
||||
fetchPolicy: bypassCache ? 'network-only' : undefined,
|
||||
});
|
||||
|
||||
this.loggedPerson = result.data.loggedPerson;
|
||||
this.currentActor = result.data.currentActor;
|
||||
}
|
||||
|
||||
private resetFields () {
|
||||
|
||||
Reference in New Issue
Block a user