2019-01-21 15:08:22 +01:00
< template >
2022-07-12 10:55:28 +02:00
< section class = "container mx-auto" >
2022-08-26 16:08:58 +02:00
< h1 > { { t ( "Create a new group" ) } } < / h1 >
2022-07-12 10:55:28 +02:00
< o-notification
variant = "danger"
v - for = "(value, index) in errors"
: key = "index"
>
2020-08-27 11:53:24 +02:00
{ { value } }
2022-07-12 10:55:28 +02:00
< / o-notification >
2019-09-02 10:50:00 +02:00
2020-08-27 11:53:24 +02:00
< form @submit.prevent ="createGroup" >
2022-08-26 16:08:58 +02:00
< o-field : label = "t('Group display name')" label -for = " group -display -name " >
2022-07-12 10:55:28 +02:00
< o-input
2021-09-07 17:52:34 +02:00
aria - required = "true"
required
v - model = "group.name"
id = "group-display-name"
/ >
2022-07-12 10:55:28 +02:00
< / o-field >
2019-09-02 10:50:00 +02:00
2020-08-27 11:53:24 +02:00
< div class = "field" >
2021-09-07 17:52:34 +02:00
< label class = "label" for = "group-preferred-username" > { {
2022-08-26 16:08:58 +02:00
t ( "Federated Group Name" )
2021-09-07 17:52:34 +02:00
} } < / label >
2020-08-27 11:53:24 +02:00
< div class = "field-body" >
2022-07-12 10:55:28 +02:00
< o-field
2022-03-24 16:36:30 +01:00
: message = "preferredUsernameErrors[0]"
: type = "preferredUsernameErrors[1]"
2020-11-17 19:14:55 +01:00
>
2022-07-12 10:55:28 +02:00
< o-input
2020-11-17 19:14:55 +01:00
ref = "preferredUsernameInput"
aria - required = "true"
required
expanded
v - model = "group.preferredUsername"
pattern = "[a-z0-9_]+"
2021-09-07 17:52:34 +02:00
id = "group-preferred-username"
2020-11-17 19:14:55 +01:00
: useHtml5Validation = "true"
: validation - message = "
group . preferredUsername
2022-08-26 16:08:58 +02:00
? t (
2020-11-30 10:24:11 +01:00
'Only alphanumeric lowercased characters and underscores are supported.'
)
2020-11-17 19:14:55 +01:00
: null
"
/ >
2020-08-27 11:53:24 +02:00
< p class = "control" >
< span class = "button is-static" > @ { { host } } < / span >
< / p >
2022-07-12 10:55:28 +02:00
< / o-field >
2020-08-27 11:53:24 +02:00
< / div >
2022-07-12 10:55:28 +02:00
< i18n-t
v - if = "currentActor"
keypath = "This is like your federated username ({username}) for groups. It will allow the group to be found on the federation, and is guaranteed to be unique."
>
< template # username >
< code >
{ { usernameWithDomain ( currentActor , true ) } }
< / code >
< / template >
< / i18n-t >
2020-08-27 11:53:24 +02:00
< / div >
2022-07-12 10:55:28 +02:00
< o-field
2022-08-26 16:08:58 +02:00
: label = "t('Description')"
2022-03-24 15:25:10 +01:00
label - for = "group-summary"
2022-03-24 16:36:30 +01:00
: message = "summaryErrors[0]"
: type = "summaryErrors[1]"
2022-03-24 15:25:10 +01:00
>
2022-07-12 10:55:28 +02:00
< o-input v-model = "group.summary" type="textarea" id="group-summary" / >
< / o-field >
2019-09-02 10:50:00 +02:00
< div >
2022-08-26 16:08:58 +02:00
< b > { { t ( "Avatar" ) } } < / b >
2021-04-12 10:43:04 +02:00
< picture-upload
2022-08-26 16:08:58 +02:00
: textFallback = "t('Avatar')"
2021-04-12 10:43:04 +02:00
v - model = "avatarFile"
: maxSize = "avatarMaxSize"
/ >
2019-09-02 10:50:00 +02:00
< / div >
< div >
2022-08-26 16:08:58 +02:00
< b > { { t ( "Banner" ) } } < / b >
2021-04-12 10:43:04 +02:00
< picture-upload
2022-08-26 16:08:58 +02:00
: textFallback = "t('Banner')"
2021-04-12 10:43:04 +02:00
v - model = "bannerFile"
: maxSize = "bannerMaxSize"
/ >
2019-09-02 10:50:00 +02:00
< / div >
2022-08-26 16:08:58 +02:00
< o-button variant = "primary" native -type = " submit " >
{ { t ( "Create my group" ) } }
< / o-button >
2020-08-27 11:53:24 +02:00
< / form >
2020-02-18 08:57:00 +01:00
< / section >
2019-01-21 15:08:22 +01:00
< / template >
2022-07-12 10:55:28 +02:00
< script lang = "ts" setup >
import { Group , usernameWithDomain , displayName } from "@/types/actor" ;
2020-08-31 12:40:30 +02:00
import RouteName from "../../router/name" ;
2020-08-27 11:53:24 +02:00
import { convertToUsername } from "../../utils/username" ;
2020-11-27 19:27:44 +01:00
import PictureUpload from "../../components/PictureUpload.vue" ;
2022-03-24 15:25:10 +01:00
import { ErrorResponse } from "@/types/errors.model" ;
2021-05-12 18:10:07 +02:00
import { ServerParseError } from "@apollo/client/link/http" ;
2022-07-12 10:55:28 +02:00
import { useCurrentActorClient } from "@/composition/apollo/actor" ;
import { computed , inject , reactive , ref , watch } from "vue" ;
import { useRouter } from "vue-router" ;
import { useI18n } from "vue-i18n" ;
import { useCreateGroup } from "@/composition/apollo/group" ;
import {
useAvatarMaxSize ,
useBannerMaxSize ,
useHost ,
} from "@/composition/config" ;
import { Notifier } from "@/plugins/notifier" ;
2022-08-26 16:08:58 +02:00
import { useHead } from "@vueuse/head" ;
2022-07-12 10:55:28 +02:00
const { currentActor } = useCurrentActorClient ( ) ;
const { t } = useI18n ( { useScope : "global" } ) ;
useHead ( {
title : computed ( ( ) => t ( "Create a new group" ) ) ,
} ) ;
const group = ref ( new Group ( ) ) ;
const avatarFile = ref < File | null > ( null ) ;
const bannerFile = ref < File | null > ( null ) ;
const errors = ref < string [ ] > ( [ ] ) ;
const fieldErrors = reactive < Record < string , string | undefined > > ( {
preferred _username : undefined ,
summary : undefined ,
} ) ;
const router = useRouter ( ) ;
const host = useHost ( ) ;
const avatarMaxSize = useAvatarMaxSize ( ) ;
const bannerMaxSize = useBannerMaxSize ( ) ;
const notifier = inject < Notifier > ( "notifier" ) ;
2022-08-26 16:08:58 +02:00
watch (
( ) => group . value . name ,
( newGroupName ) => {
group . value . preferredUsername = convertToUsername ( newGroupName ) ;
}
) ;
2019-09-02 10:50:00 +02:00
2022-08-26 16:08:58 +02:00
const buildVariables = computed ( ( ) => {
2022-07-12 10:55:28 +02:00
let avatarObj = { } ;
let bannerObj = { } ;
2020-08-27 11:53:24 +02:00
2022-07-12 10:55:28 +02:00
const groupBasic = {
preferredUsername : group . value . preferredUsername ,
name : group . value . name ,
summary : group . value . summary ,
2022-03-24 16:36:30 +01:00
} ;
2022-03-24 15:25:10 +01:00
2022-07-12 10:55:28 +02:00
if ( avatarFile . value ) {
avatarObj = {
avatar : {
media : {
name : avatarFile . value ? . name ,
alt : ` ${ group . value . preferredUsername } 's avatar ` ,
file : avatarFile . value ,
2019-09-02 10:50:00 +02:00
} ,
2022-07-12 10:55:28 +02:00
} ,
} ;
2020-08-27 11:53:24 +02:00
}
2022-07-12 10:55:28 +02:00
if ( bannerFile . value ) {
bannerObj = {
banner : {
media : {
name : bannerFile . value ? . name ,
alt : ` ${ group . value . preferredUsername } 's banner ` ,
file : bannerFile . value ,
2019-09-02 10:50:00 +02:00
} ,
2022-07-12 10:55:28 +02:00
} ,
2020-02-18 08:57:00 +01:00
} ;
2019-09-02 10:50:00 +02:00
}
2022-07-12 10:55:28 +02:00
return {
... groupBasic ,
... avatarObj ,
... bannerObj ,
} ;
2022-08-26 16:08:58 +02:00
} ) ;
2021-04-12 10:43:04 +02:00
2022-07-12 10:55:28 +02:00
const handleError = ( err : ErrorResponse ) => {
if ( err ? . networkError ? . name === "ServerParseError" ) {
const error = err ? . networkError as ServerParseError ;
if ( error ? . response ? . status === 413 ) {
errors . value . push (
t (
"Unable to create the group. One of the pictures may be too heavy."
) as string
) ;
2021-04-12 10:43:04 +02:00
}
2022-07-12 10:55:28 +02:00
}
err . graphQLErrors ? . forEach ( ( error ) => {
if ( error . field ) {
if ( Array . isArray ( error . message ) ) {
fieldErrors [ error . field ] = error . message [ 0 ] ;
2022-03-24 15:25:10 +01:00
} else {
2022-07-12 10:55:28 +02:00
fieldErrors [ error . field ] = error . message ;
2022-03-24 15:25:10 +01:00
}
2022-07-12 10:55:28 +02:00
} else {
errors . value . push ( error . message ) ;
}
} ) ;
} ;
const summaryErrors = computed ( ( ) => {
const message = fieldErrors . summary ? fieldErrors . summary : undefined ;
2022-08-26 16:08:58 +02:00
const type = fieldErrors . summary ? "danger" : undefined ;
2022-07-12 10:55:28 +02:00
return [ message , type ] ;
} ) ;
const preferredUsernameErrors = computed ( ( ) => {
const message = fieldErrors . preferred _username
? fieldErrors . preferred _username
: t (
"Only alphanumeric lowercased characters and underscores are supported."
) ;
2022-08-26 16:08:58 +02:00
const type = fieldErrors . preferred _username ? "danger" : undefined ;
2022-07-12 10:55:28 +02:00
return [ message , type ] ;
} ) ;
2022-08-26 16:08:58 +02:00
const { onDone , onError , mutate } = useCreateGroup ( ) ;
onDone ( ( ) => {
notifier ? . success (
t ( "Group {displayName} created" , {
displayName : displayName ( group . value ) ,
} )
) ;
router . push ( {
name : RouteName . GROUP ,
params : { preferredUsername : usernameWithDomain ( group . value ) } ,
} ) ;
} ) ;
onError ( ( err ) => handleError ( err as unknown as ErrorResponse ) ) ;
const createGroup = async ( ) : Promise < void > => {
errors . value = [ ] ;
fieldErrors . preferred _username = undefined ;
fieldErrors . summary = undefined ;
mutate ( buildVariables . value ) ;
} ;
2019-01-21 15:08:22 +01:00
< / script >
< style >
. markdown - render h1 {
font - size : 2 em ;
}
< / style >