Newer
Older
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<template>
<div v-if="submittedMutation">
<div class="ui positive message">
<div class="header"><translate :translate-context="'Content/Library/Paragraph'">Your edit was successfully submitted.</translate></div>
</div>
<edit-card :obj="submittedMutation" :current-state="currentState" />
<button class="ui button" @click.prevent="submittedMutation = null">
<translate :translate-context="'Content/Library/Button.Label'">
Submit another edit
</translate>
</button>
</div>
<div v-else>
<edit-list :filters="editListFilters" :url="mutationsUrl" :obj="object" :currentState="currentState">
<div slot="title">
<template v-if="showPendingReview">
<translate :translate-context="'Content/Library/Paragraph'">
Recent edits awaiting review
</translate>
<button class="ui tiny basic right floated button" @click.prevent="showPendingReview = false">
<translate :translate-context="'Content/Library/Button.Label'">
Show all edits
</translate>
</button>
</template>
<template v-else>
<translate :translate-context="'Content/Library/Paragraph'">
Recent edits
</translate>
<button class="ui tiny basic right floated button" @click.prevent="showPendingReview = true">
<translate :translate-context="'Content/Library/Button.Label'">
Retrict to unreviewed edits
</translate>
</button>
</template>
</div>
<empty-state slot="empty-state">
<translate :translate-context="'Content/Library/Paragraph'">
Suggest a change using the form below.
</translate>
</empty-state>
</edit-list>
<form class="ui form" @submit.prevent="submit()">
<div class="ui hidden divider"></div>
<div v-if="errors.length > 0" class="ui negative message">
<div class="header"><translate :translate-context="'Content/Library/Error message.Title'">Error while submitting edit</translate></div>
<ul class="list">
<li v-for="error in errors">{{ error }}</li>
</ul>
</div>
<div v-if="!canEdit" class="ui message">
<translate :translate-context="'Content/Library/Paragraph'">
You don't have the permission to edit this object, but you can suggest changes. Once submitted, suggestions will be reviewed before approval.
</translate>
</div>
<div v-if="values" v-for="fieldConfig in config.fields" :key="fieldConfig.id" class="ui field">
<template v-if="fieldConfig.type === 'text'">
<label :for="fieldConfig.id">{{ fieldConfig.label }}</label>
<input :type="fieldConfig.inputType || 'text'" v-model="values[fieldConfig.id]" :required="fieldConfig.required" :name="fieldConfig.id" :id="fieldConfig.id">
</template>
<div v-if="values[fieldConfig.id] != initialValues[fieldConfig.id]">
<button class="ui tiny basic right floated reset button" form="noop" @click.prevent="values[fieldConfig.id] = initialValues[fieldConfig.id]">
<i class="undo icon"></i>
<translate :translate-context="'Content/Library/Button.Label'" :translate-params="{value: initialValues[fieldConfig.id]}">Reset to initial value: %{ value }</translate>
</button>
</div>
</div>
<div class="field">
<label for="summary"><translate :translate-context="'*/*/*'">Summary (optional)</translate></label>
<textarea name="change-summary" v-model="summary" id="change-summary" rows="3" :placeholder="labels.summaryPlaceholder"></textarea>
</div>
<router-link
class="ui left floated button"
v-if="objectType === 'track'"
:to="{name: 'library.tracks.detail', params: {id: object.id }}"
>
<translate :translate-context="'Content/*/Button.Label'">Cancel</translate>
</router-link>
<button :class="['ui', {'loading': isLoading}, 'right', 'floated', 'green', 'button']" type="submit" :disabled="isLoading || !mutationPayload">
<translate v-if="canEdit" key="1" :translate-context="'Content/Library/Button.Label/Verb'">Submit and apply edit</translate>
<translate v-else key="2" :translate-context="'Content/Library/Button.Label/Verb'">Submit suggestion</translate>
</button>
</form>
</div>
</div>
</template>
<script>
import _ from '@/lodash'
import axios from "axios"
import EditList from '@/components/library/EditList'
import EditCard from '@/components/library/EditCard'
import edits from '@/edits'
export default {
props: ["objectType", "object"],
components: {
EditList,
EditCard
},
data() {
return {
isLoading: false,
errors: [],
values: {},
initialValues: {},
summary: '',
submittedMutation: null,
showPendingReview: true,
}
},
created () {
this.setValues()
},
computed: {
configs: edits.getConfigs,
config: edits.getConfig,
currentState: edits.getCurrentState,
canEdit: edits.getCanEdit,
labels () {
return {
summaryPlaceholder: this.$pgettext('*/*/Placeholder', 'A short summary describing your changes.'),
}
},
mutationsUrl () {
if (this.objectType === 'track') {
return `tracks/${this.object.id}/mutations/`
}
},
mutationPayload () {
let self = this
let changedFields = this.config.fields.filter(f => {
return self.values[f.id] != self.initialValues[f.id]
})
if (changedFields.length === 0) {
return null
}
let payload = {
type: 'update',
payload: {},
summary: this.summary,
}
changedFields.forEach((f) => {
payload.payload[f.id] = self.values[f.id]
})
return payload
},
editListFilters () {
if (this.showPendingReview) {
return {is_approved: 'null'}
} else {
return {}
}
},
},
methods: {
setValues () {
let self = this
this.config.fields.forEach(f => {
self.$set(self.values, f.id, f.getValue(self.object))
self.$set(self.initialValues, f.id, self.values[f.id])
})
},
submit() {
let self = this
self.isLoading = true
self.errors = []
let payload = _.clone(this.mutationPayload || {})
if (this.canEdit) {
payload.is_approved = true
}
return axios.post(this.mutationsUrl, payload).then(
response => {
self.isLoading = false
self.submittedMutation = response.data
},
error => {
self.errors = error.backendErrors
self.isLoading = false
}
)
}
}
}
</script>
<style>
.reset.button {
margin-top: 0.5em;
}
</style>