This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/routes/profileEditor.vue

339 lines
12 KiB
Vue
Raw Normal View History

2020-10-24 12:50:08 -07:00
<template>
<MustLogin v-if="!$user()"/>
<div v-else>
2020-10-24 12:50:08 -07:00
<div class="mb-3 d-flex justify-content-between flex-column flex-md-row">
<h2 class="text-nowrap">
<Avatar :user="$user()"/>
@{{ $user().username }}
</h2>
<div>
<nuxt-link :to="`/@${$user().username}`" class="btn btn-outline-primary btn-sm">
<Icon v="id-card"/>
<T>profile.show</T>
</nuxt-link>
</div>
</div>
<form @submit.prevent="save" :class="[saving ? 'saving' : '']">
2021-01-12 11:06:59 -08:00
<div v-if="$isGranted('users')" class="border border-primary rounded p-4">
<h3 class="h4 mb-3">
<Icon v="user-cog"/>
Admin section
</h3>
<p class="small text-muted mb-0">
This will be shown on the Team page.
If you leave it empty, you won't show up there (for this language version).
You can use a different display name in different language versions.
</p>
<div class="form-group">
<label for="teamName">Team page display name:</label>
<input class="form-control" name="teamName" maxlength="36" v-model="teamName"/>
</div>
<hr/>
<p class="small text-muted mb-0">
If you feel that you've contributed to this language version enough to get credited in the footer
(not saying how much that is, that's on you to decide 😉),
then add your name and areas here (in the local language!).
The team as a whole will be credited in the footer either way.
</p>
<div class="form-group">
<label for="footerName">Footer display name:</label>
<input class="form-control" name="footerName" maxlength="36" v-model="footerName"/>
</div>
<div class="form-group">
<label for="footerAreas">Areas responsible for / contributing to:</label>
<ListInput v-model="footerAreas"/>
</div>
</div>
<section>
<OpinionLegend/>
</section>
2020-10-24 12:50:08 -07:00
<div class="form-group">
<h3 class="h4">
<Icon v="signature"/>
<T>profile.names</T>
</h3>
<OpinionListInput v-model="names"/>
</div>
<div class="form-group">
<h3 class="h4">
<Icon v="link"/>
<T>profile.pronouns</T>
</h3>
2020-10-26 16:42:42 -07:00
<p class="small text-muted mb-0">
2020-10-24 12:50:08 -07:00
<T>profile.pronounsInfo</T>
</p>
2020-11-05 05:06:47 -08:00
<OpinionListInput v-model="pronouns" :validation="validatePronoun"/>
2020-10-24 12:50:08 -07:00
</div>
<div class="form-group">
<h3 class="h4">
<Icon v="comment-edit"/>
<T>profile.description</T>
</h3>
2020-11-05 05:06:47 -08:00
<textarea class="form-control form-control-sm" v-model="description" maxlength="256" rows="4"/>
2020-10-24 12:50:08 -07:00
</div>
2020-10-26 16:42:42 -07:00
<div class="form-group">
<h3 class="h4">
<Icon v="flag"/>
<T>profile.flags</T>
</h3>
<p class="small text-muted mb-0">
<T>profile.flagsInfo</T>
</p>
<ButtonList v-model="flags" :options="allFlags" v-slot="s">
<Flag :name="s.desc.split('|')[0]" :alt="s.desc.split('|')[1]" :img="`/flags/${s.v}.png`"/>
2020-10-26 16:42:42 -07:00
</ButtonList>
</div>
<details class="form-group border rounded" :open="Object.keys(customFlags).length > 0">
2021-04-08 05:37:31 -07:00
<summary class="px-3 py-2">
2021-04-05 08:03:13 -07:00
<T>profile.flagsCustom</T>
</summary>
<div class="border-top">
<ImageWidgetRich v-model="customFlags"/>
</div>
</details>
2021-04-08 05:37:31 -07:00
<Answer question="flags" small/>
2020-10-24 12:50:08 -07:00
<div class="form-group">
<h3 class="h4">
<Icon v="link"/>
<T>profile.links</T>
</h3>
<ListInput v-model="links" v-slot="s">
<input v-model="s.val" type="url" class="form-control" @keyup="s.update(s.val)" required/>
</ListInput>
</div>
<div class="form-group">
<h3 class="h4">
<Icon v="birthday-cake"/>
<T>profile.birthday</T>
</h3>
2020-10-26 16:42:42 -07:00
<p class="small text-muted mb-0">
2020-10-24 12:50:08 -07:00
<T>profile.birthdayInfo</T>
</p>
2020-11-05 05:06:47 -08:00
<input type="date" class="form-control form-control-sm" v-model="birthday"/>
2020-10-24 12:50:08 -07:00
</div>
<div class="form-group">
<h3 class="h4">
<Icon v="scroll-old"/>
<T>profile.words</T>
</h3>
<template v-for="i in [0, 1, 2, 3]">
<h4 class="h5">
<T>profile.column</T> {{i + 1}}
</h4>
2020-10-25 04:30:55 -07:00
<OpinionListInput v-model="words[i]" group="words"/>
2020-10-24 12:50:08 -07:00
</template>
</div>
2021-01-22 14:54:24 -08:00
<button class="btn btn-primary w-100" type="submit">
2020-10-24 12:50:08 -07:00
<Icon v="save"/>
<T>profile.editor.save</T>
</button>
</form>
</div>
</template>
<script>
import {head, dictToList, listToDict, buildList, buildDict} from "../src/helpers";
import { pronouns } from "~/src/data";
import { buildPronoun } from "../src/buildPronoun";
2020-10-24 13:58:34 -07:00
import config from '../data/config.suml';
2020-10-28 08:22:29 -07:00
const defaultWords = config.profile.defaultWords.map(c => buildList(function* () {
2020-10-24 13:58:34 -07:00
for (let word of c) {
yield {key: word, value: 0};
}
}))
2020-10-24 12:50:08 -07:00
export default {
data() {
return {
saving: false,
};
},
async asyncData({ app, store }) {
if (!store.state.user) {
return {};
}
2020-11-15 05:13:14 -08:00
const profiles = await app.$axios.$get(`/profile/get/${encodeURIComponent(store.state.user.username)}`, { headers: {
2020-10-24 12:50:08 -07:00
authorization: 'Bearer ' + store.state.token,
} });
for (let locale in profiles) {
if (!profiles.hasOwnProperty(locale)) {
continue;
}
if (locale === app.context.env.LOCALE) {
const profile = profiles[locale];
return {
names: dictToList(profile.names),
pronouns: dictToList(profile.pronouns),
description: profile.description,
birthday: profile.birthday,
2020-10-24 16:37:03 -07:00
links: Object.keys(profile.links).length ? profile.links : [],
2020-10-26 16:42:42 -07:00
flags: profile.flags,
2021-04-05 08:03:13 -07:00
customFlags: profile.customFlags,
2020-10-24 12:50:08 -07:00
words: profile.words.map(x => dictToList(x)),
2021-01-12 11:06:59 -08:00
teamName: profile.teamName,
footerName: profile.footerName,
footerAreas: profile.footerAreas,
2020-10-24 12:50:08 -07:00
};
}
}
for (let locale in profiles) {
if (!profiles.hasOwnProperty(locale)) {
continue;
}
const profile = profiles[locale];
return {
names: dictToList(profile.names),
2020-10-24 16:39:27 -07:00
pronouns: [],
2020-10-24 12:50:08 -07:00
description: '',
birthday: profile.birthday,
2020-10-24 16:37:03 -07:00
links: Object.keys(profile.links).length ? profile.links : [],
flags: profile.flags.filter(f => !f.startsWith('-')),
2021-04-05 08:03:13 -07:00
customFlags: profile.customFlags,
2020-10-24 13:58:34 -07:00
words: defaultWords,
2021-01-12 11:06:59 -08:00
teamName: profile.teamName,
footerName: profile.footerName,
footerAreas: [],
2020-10-24 12:50:08 -07:00
};
}
return {
2020-10-24 16:39:27 -07:00
names: [],
pronouns: [],
2020-10-24 12:50:08 -07:00
description: '',
birthday: null,
2020-10-24 16:39:27 -07:00
links: [],
flags: [],
2021-04-05 08:03:13 -07:00
customFlags: {},
2020-10-24 13:58:34 -07:00
words: defaultWords,
2021-01-12 11:06:59 -08:00
teamName: '',
footerName: '',
footerAreas: [],
2020-10-24 12:50:08 -07:00
};
},
mounted() {
if (process.client && !this.$user()) {
window.sessionStorage.setItem('after-login', window.location.pathname);
this.$router.push('/' + this.config.user.route);
}
},
2020-10-24 12:50:08 -07:00
methods: {
async save() {
this.saving = true;
await this.$axios.$post(`/profile/save`, {
2020-10-24 12:50:08 -07:00
names: listToDict(this.names),
pronouns: listToDict(this.pronouns),
description: this.description,
birthday: this.birthday,
2020-10-26 16:42:42 -07:00
links: [...this.links],
flags: [...this.flags],
2021-04-05 08:03:13 -07:00
customFlags: {...this.customFlags},
2020-10-24 12:50:08 -07:00
words: this.words.map(x => listToDict(x)),
2021-01-12 11:06:59 -08:00
teamName: this.teamName,
footerName: this.footerName,
footerAreas: this.footerAreas,
2020-11-02 10:31:05 -08:00
});
2020-10-24 12:50:08 -07:00
this.saving = false;
this.$router.push(`/@${this.$user().username}`)
},
2020-12-08 14:14:40 -08:00
normalisePronoun(pronoun) {
return decodeURIComponent(
2020-11-24 15:54:02 -08:00
pronoun
2020-12-04 14:26:59 -08:00
.toLowerCase()
2020-12-05 13:01:12 -08:00
.trim()
2020-11-24 15:54:02 -08:00
.replace(new RegExp('^' + this.$base), '')
.replace(new RegExp('^' + this.$base.replace(/^https?:\/\//, '')), '')
.replace(new RegExp('^/'), '')
);
},
normaliseAndBuildPronoun(pronoun) {
return buildPronoun(pronouns, this.normalisePronoun(pronoun));
2020-12-08 14:14:40 -08:00
},
validatePronoun(pronoun) {
pronoun = this.normalisePronoun(pronoun);
return pronoun === this.config.pronouns.any || buildPronoun(pronouns, pronoun)
? null
: 'profile.pronounsNotFound'
2020-12-08 14:14:40 -08:00
},
},
computed: {
mainPronoun() {
let mainPronoun = buildPronoun(pronouns, this.config.profile.flags.defaultPronoun);
let mainOpinion = -1;
for (let {key: pronoun, value: opinion} of this.pronouns) {
if (opinion === 2) {
opinion = 0.5;
}
if (opinion > mainOpinion) {
const p = this.normaliseAndBuildPronoun(pronoun);
2020-12-08 14:14:40 -08:00
if (p) {
mainPronoun = p;
mainOpinion = opinion;
}
}
}
return mainPronoun;
2020-11-05 05:06:47 -08:00
},
allFlags() {
const that = this;
const flags = buildList(function*() {
for (let key in process.env.FLAGS) {
if (!process.env.FLAGS.hasOwnProperty(key)) { continue; }
yield [
key,
(key.startsWith('-') ? process.env.FLAGS[key] : that.$translateForPronoun(process.env.FLAGS[key], that.mainPronoun))
+ '|' + process.env.FLAGS[key]
];
}
});
flags.sort((a, b) => a[1].localeCompare(b[1]));
return buildDict(function *() {
for (let [key, display] of flags) {
yield [key, display];
}
});
},
2020-10-24 12:50:08 -07:00
},
head() {
return head({
title: this.$t('profile.editor.header'),
});
},
}
</script>
<style lang="scss" scoped>
.avatar {
width: 100%;
max-width: 5rem;
max-height: 5rem;
}
.saving {
opacity: .5;
}
</style>