-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
126 lines (124 loc) · 4.39 KB
/
App.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
<template>
<div>
<select v-model="type">
<option v-for="(type, index) in types" v-bind:value="index">{{ type.name | capitalize }}</option>
</select>
<input type="text" v-model="spz">
<div>{{ spz | uppercase }}</div>
<p v-if="error & errorEnum.UNMATCHED_LENGTH">Znaků musí být {{ types[type].maxLength }}</p>
<p v-if="error & errorEnum.WRONG_CHARS">SPZ může obsahovat pouze čísla a znaky abecedy bez diakritiky kromě O, Q, W a G.</p>
<p v-if="error & errorEnum.NO_NUM">SPZ musí obsahovat alespoň jedno číslo</p>
<p v-if="error & errorEnum.ALREADY_USED">Tato SPZ je již registrována</p>
<p v-if="error & errorEnum.API_OFFLINE">Omlouváme se, ale služba pro kontrolu duplicity a registraci SPZ je momentálně offline.<p>
<button v-if="error === 0 | error === 16" v-on:click="apiPost">Registrovat</button>
</div>
</template>
<style>
body{
background-color:#ff0000;
}
</style>
<script>
export default{
data(){
return{
spz:'',
type: 1,
error: 0,
types: {
1: {name: 'auto', maxLength: 8},
2: {name: 'motocykl', maxLength: 7},
3: {name: 'moped', maxLength: 5}
},
errorEnum: {
NO_NUM: 1,
UNMATCHED_LENGTH: 2,
WRONG_CHARS: 4,
ALREADY_USED: 8,
API_OFFLINE: 16,
}
}
},
watch: {
spz: function(spz) {
this.validator(spz);
this.apiGet(spz);
}
},
mounted(){
this.validator(this.spz);
},
methods: {
addError(error){
if((this.error & this.errorEnum[error]) === 0) {
this.error += this.errorEnum[error];
}
},
removeError(error){
if((this.error & this.errorEnum[error]) > 0) {
this.error -= this.errorEnum[error];
}
},
validator(spz){
if(spz.length != this.types[this.type].maxLength) {
this.addError('UNMATCHED_LENGTH');
} else {
this.removeError('UNMATCHED_LENGTH');
}
if(/([^\w]|_|[QOWG])/gi.test(spz)){
this.addError('WRONG_CHARS');
} else {
this.removeError('WRONG_CHARS');
}
if(!/\w*[\d]+\w*/.test(spz)){
this.addError('NO_NUM');
} else {
this.removeError('NO_NUM');
}
},
apiGet(spz){
console.log('Calling the API.');
this.$http.get('/api.php', {params: {spz: spz}}).then((response) => {
this.removeError('API_OFFLINE');
if(response.body > 0) {
this.addError('ALREADY_USED');
} else {
this.removeError('ALREADY_USED');
}
}, (response) => {
this.addError('API_OFFLINE');
});
},
apiPost(){
this.spz = this.spz.toUpperCase();
this.$http.post('/api.php',{spz: this.spz}).then((response) => {
this.removeError('API_OFFLINE');
console.log(response);
if(response.body == 1) {
alert('Registrováno.');
this.apiGet(this.spz);
}
}, (response) => {
console.log(response);
this.addError('API_OFFLINE');
});
}
},
filters: {
capitalize: function (value) {
if (!value) return '';
value = value.toString();
return value.charAt(0).toUpperCase() + value.slice(1)
},
uppercase: function (val) {
if (!val) return '';
val = val.toString();
return val.toUpperCase();
}
},
http: {
emulateJSON: true,
emulateHTTP: true
},
}
</script>