-
-
Notifications
You must be signed in to change notification settings - Fork 568
/
Copy pathPublishForm.vue
156 lines (137 loc) · 4.62 KB
/
PublishForm.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<template>
<div>
<header class="mb-6">
<breadcrumb :url="breadcrumbUrl" :title="__('Roles & Permissions')" />
<div class="flex items-center justify-between">
<h1 v-text="__(initialTitle) || __('Create Role')" />
<button type="submit" class="btn-primary" @click="save">{{ __('Save') }}</button>
</div>
</header>
<div class="card configure-tab publish-fields mb-6 p-0 @container">
<form-group
handle="title"
class="border-b dark:border-dark-900"
:display="__('Title')"
:errors="errors.title"
:instructions="__('messages.role_title_instructions')"
v-model="title"
:focus="true"
/>
<form-group
class="border-b dark:border-dark-900"
fieldtype="slug"
handle="handle"
:display="__('Handle')"
:instructions="__('messages.role_handle_instructions')"
:errors="errors.title"
v-model="handle"
/>
<div class="p-6 pt-0 text-xs text-red-500" v-if="initialHandle && handle != initialHandle">
{{ __('messages.role_change_handle_warning') }}
</div>
<form-group
v-if="canAssignSuper"
class="toggle-fieldtype"
fieldtype="toggle"
handle="super"
:display="__('permissions.super')"
:instructions="__('permissions.super_desc')"
v-model="isSuper"
/>
</div>
<div v-if="!isSuper">
<div class="content mt-6" v-for="group in permissions" :key="group.handle">
<h2 class="mb-2 mt-10 text-base">{{ group.label }}</h2>
<role-permission-tree class="card p-0" :depth="1" :initial-permissions="group.permissions" />
</div>
</div>
</div>
</template>
<script>
import HasElevatedSession from '@statamic/mixins/HasElevatedSession.js';
const checked = function (permissions) {
return permissions.reduce((carry, permission) => {
if (!permission.checked) return carry;
return [...carry, permission.value, ...checked(permission.children)];
}, []);
};
export default {
mixins: [HasElevatedSession],
props: {
initialTitle: String,
initialHandle: String,
initialPermissions: Array,
initialSuper: Boolean,
canAssignSuper: Boolean,
action: String,
method: String,
breadcrumbUrl: String,
indexUrl: String,
},
data() {
return {
error: null,
errors: {},
title: this.initialTitle,
handle: this.initialHandle,
permissions: this.initialPermissions,
isSuper: this.initialSuper,
};
},
watch: {
title: function (display) {
this.handle = snake_case(display);
},
},
computed: {
hasErrors() {
return this.error || Object.keys(this.errors).length;
},
payload() {
return {
title: this.title,
handle: this.handle,
super: this.isSuper,
permissions: this.checkedPermissions,
};
},
checkedPermissions() {
return this.permissions.reduce((carry, group) => {
return [...carry, ...checked(group.permissions)];
}, []);
},
},
methods: {
clearErrors() {
this.error = null;
this.errors = {};
},
save() {
this.requireElevatedSession().then(() => this.performSaveAction());
},
performSaveAction() {
this.clearErrors();
this.$axios[this.method](this.action, this.payload)
.then((response) => {
window.location = response.data.redirect;
})
.catch((e) => {
if (e.response && e.response.status === 422) {
const { message, errors } = e.response.data;
this.error = message;
this.errors = errors;
this.$toast.error(message);
} else {
this.$toast.error(__('Unable to save role'));
}
});
},
},
mounted() {
this.$keys.bindGlobal(['mod+s'], (e) => {
e.preventDefault();
this.save();
});
},
};
</script>