-
-
Notifications
You must be signed in to change notification settings - Fork 568
/
Copy pathEditForm.vue
149 lines (123 loc) · 4.37 KB
/
EditForm.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
<template>
<publish-container
ref="container"
:name="name"
:blueprint="blueprint"
v-model="currentValues"
reference="collection"
:meta="meta"
:errors="errors"
v-slot="{ setFieldValue, setFieldMeta }"
>
<div>
<breadcrumbs v-if="breadcrumbs" :crumbs="breadcrumbs" />
<div class="flex items-center mb-6">
<h1 class="flex-1">{{ title }}</h1>
<div class="rtl:mr-4 ltr:ml-4 rtl:text-right ltr:text-left" :class="{ 'btn-group': hasSaveAsOptions }">
<button
class="btn-primary rtl:pr-4 ltr:pl-4"
:class="{ 'disabled': !isDirty }"
:disabled="!isDirty"
@click="save"
v-text="__('Save')" />
<dropdown-list v-if="hasSaveAsOptions" class="rtl:mr-0 ltr:ml-0">
<template #trigger>
<button class="btn-primary rtl:rounded-r-none ltr:rounded-l-none flex items-center">
<svg-icon name="micro/chevron-down-xs" class="w-2" />
</button>
</template>
<h6 class="p-2">{{ __('Save to') }}...</h6>
<dropdown-item v-for="option in saveAsOptions" :key="option.url" @click="saveAs(option.url)">
<div class="flex items-start rtl:pl-4 ltr:pr-4">
<svg-icon :name="option.icon" class="text-gray shrink-0 rtl:ml-2 ltr:mr-2 w-4 group-hover:text-white" />
<span class="whitespace-normal">{{ option.label }}</span>
</div>
</dropdown-item>
</dropdown-list>
</div>
</div>
<publish-tabs
@updated="setFieldValue"
@meta-updated="setFieldMeta"
:enable-sidebar="hasSidebar"
:read-only="readOnly" />
</div>
</publish-container>
</template>
<script>
export default {
props: {
blueprint: { required: true, type: Object },
meta: { required: true, type: Object },
values: { required: true, type: Object },
title: { required: true, type: String },
name: { type: String, default: 'base' },
breadcrumbs: Array,
action: String,
readOnly: { type: Boolean, default: false },
reloadOnSave: { type: Boolean, default: false },
saveAsOptions: { type: Array, default: () => [] },
},
data() {
return {
saving: false,
currentValues: this.values,
error: null,
errors: {},
hasSidebar: this.blueprint.tabs.map(tab => tab.handle).includes('sidebar'),
}
},
computed: {
hasSaveAsOptions() {
return this.saveAsOptions.length;
},
isDirty() {
return this.$dirty.has(this.name);
}
},
methods: {
clearErrors() {
this.error = null;
this.errors = {};
},
save() {
this.saveAs(this.action);
},
saveAs(url) {
this.saving = true;
this.clearErrors();
this.$axios
.patch(url, this.currentValues)
.then(() => {
this.$refs.container.saved();
this.$nextTick(() => location.reload());
})
.catch(e => this.handleAxiosError(e));
},
handleAxiosError(e) {
this.saving = false;
if (e.response && e.response.status === 422) {
const { message, errors } = e.response.data;
this.error = message;
this.errors = errors;
this.$toast.error(message);
} else {
const message = data_get(e, 'response.data.message');
this.$toast.error(message || e);
console.log(e);
}
},
},
created() {
this.$keys.bindGlobal(['mod+s'], e => {
e.preventDefault();
this.save();
});
},
watch: {
saving(saving) {
this.$progress.loading('preferences-edit-form', saving);
}
},
}
</script>