-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCreateWiki.vue
More file actions
216 lines (203 loc) · 6.08 KB
/
CreateWiki.vue
File metadata and controls
216 lines (203 loc) · 6.08 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<template>
<v-form @submit="createWiki">
<SiteDetailsCreateWikiWizardStep
v-show="step === Steps.SITE_DETAILS"
:title="title"
:inFlight="inFlight"
v-model="siteDetails"
:error="error"
:SUBDOMAIN_SUFFIX="SUBDOMAIN_SUFFIX"
:CNAME_RECORD="CNAME_RECORD"
:errorMessages="errorMessages"
@next-step="goToStep(Steps.AUDIENCE_AND_PURPOSE)"
/>
<AudienceAndPurposeWizardStep
v-show="step === Steps.AUDIENCE_AND_PURPOSE"
:title="title"
:inFlight="inFlight"
:dismissable="false"
v-model="audienceAndPurpose"
@previous-step="goToStep(Steps.SITE_DETAILS)"
@next-step="goToStep(Steps.TEMPORALITY)"
/>
<TemporalityCreateWikiWizardStep
v-show="step === Steps.TEMPORALITY"
:title="title"
:inFlight="inFlight"
:error="error"
:dismissable="false"
v-model="temporality"
@previous-step="goToStep(Steps.AUDIENCE_AND_PURPOSE)"
@submit="createWiki"
/>
</v-form>
</template>
<script>
import config from '~/config'
import SiteDetailsCreateWikiWizardStep from './SiteDetailsCreateWikiWizardStep.vue'
import AudienceAndPurposeWizardStep from './AudienceAndPurposeWizardStep.vue'
import TemporalityCreateWikiWizardStep from './TemporalityCreateWikiWizardStep.vue'
const Steps = Object.freeze({
SITE_DETAILS: 'SiteDetails',
AUDIENCE_AND_PURPOSE: 'AudienceAndPurpose',
TEMPORALITY: 'Temporality'
});
export default {
name: 'CreateWiki',
components: {
SiteDetailsCreateWikiWizardStep,
AudienceAndPurposeWizardStep,
TemporalityCreateWikiWizardStep
},
props: [
'title'
],
computed: {
currentUser: function () {
return this.$store.getters.currentUser
}
},
data () {
return {
siteDetails: {
sitename: '',
domainRadioChoice: 'sub',
subdomain: '',
domain: '',
username: ''
},
audienceAndPurpose: {
purpose: '',
otherPurpose: '',
audience: '',
otherAudience: ''
},
temporality: {
temporality: '',
otherTemporality: ''
},
hasError: false,
error: [],
inFlight: false,
SUBDOMAIN_SUFFIX: config.SUBDOMAIN_SUFFIX,
CNAME_RECORD: config.CNAME_RECORD,
errorMessages: {
domainTaken: 'The domain has already been taken.',
domainFormat: 'The subdomain must be at least five characters long and may contain only lowercase Latin letters (a-z), digits (0-9) and hyphens (-).'
},
Steps,
step: Steps.SITE_DETAILS
}
},
created () {
this.checkCurrentLogin()
},
updated () {
this.checkCurrentLogin()
},
methods: {
goToStep (stepName) {
this.step = stepName
},
createWiki (evt) {
if (evt) {
evt.preventDefault()
}
this.inFlight = true
this.hasError = false
this.error = []
if (this.hasError) {
this.inFlight = false
return
}
// Figure out the actual domain to submit to the api!
let domainToSubmit = ''
if (this.siteDetails.domainRadioChoice === 'sub') {
domainToSubmit = this.siteDetails.subdomain + this.SUBDOMAIN_SUFFIX
}
if (this.siteDetails.domainRadioChoice === 'own') {
domainToSubmit = this.siteDetails.domain
}
const profileJSObject = {
purpose: this.audienceAndPurpose.purpose,
...(this.audienceAndPurpose.otherPurpose && { purpose_other: this.audienceAndPurpose.otherPurpose }),
...(this.audienceAndPurpose.audience && { audience: this.audienceAndPurpose.audience }),
...(this.audienceAndPurpose.otherAudience && { audience_other: this.audienceAndPurpose.otherAudience }),
temporality: this.temporality.temporality,
...(this.temporality.otherTemporality && { temporality_other: this.temporality.otherTemporality })
}
const profileJsonString = JSON.stringify(profileJSObject)
this.$api.createWiki(
{
domain: domainToSubmit,
sitename: this.siteDetails.sitename,
username: this.siteDetails.username,
profile: profileJsonString
}
)
.then(wikiDetails => this.createSuccess(wikiDetails))
.catch(errors => this.createFail(errors))
},
createSuccess (wikiDetails) {
this.hasError = false
this.error = []
// this.$router.replace(this.$route.query.redirect || '/wikis/manage/' + req.data.data.id)
this.$router.replace('/wikis/manage/' + wikiDetails.id)
},
createFail (errors) {
// Probably we want to go back to the first step that has an error in this case.
this.error = []
// all these errors are shown on the first step.
this.goToStep(Steps.SITE_DETAILS)
if (errors.sitename) {
this.hasError = true
this.error.sitename = errors.sitename[0]
}
if (errors.domain) {
this.hasError = true
if (errors.domain[0] === 'The domain has already been taken.') {
this.error.siteaddress = this.errorMessages.domainTaken
} else {
this.error.siteaddress = this.errorMessages.domainFormat
}
}
if (errors.username) {
this.hasError = true
this.error.username = errors.username[0]
}
if (errors.tooManyWikis) {
this.hasError = true
this.displayGenericError(errors.message)
}
if (errors.dbNotReady) {
this.hasError = true
this.displayGenericError('No databases ready, please report this!')
}
// Otherwise show a general error state
if (!this.hasError) {
this.hasError = true
this.displayGenericError('Creation failed!')
}
this.inFlight = false
},
displayGenericError (message) {
this.error.sitename = message
this.error.siteaddress = message
this.error.username = message
},
checkCurrentLogin () {
if (!this.currentUser) {
this.$router.replace(this.$route.query.redirect || '/')
}
}
}
}
</script>
<style scoped>
>>> .v-messages__message {
margin-bottom: 10px;
}
.v-tooltip__content {
max-width: 448px;
}
</style>