-
Notifications
You must be signed in to change notification settings - Fork 634
/
SelectAddressForm.vue
223 lines (208 loc) · 6.12 KB
/
SelectAddressForm.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
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
217
218
219
220
221
222
223
<template>
<KModal
:title="$tr('header')"
:submitText="$tr('submitButtonLabel')"
:cancelText="$tr('cancelButtonLabel')"
size="medium"
:submitDisabled="submitDisabled"
@submit="handleSubmit"
@cancel="$emit('cancel')"
>
<UiAlert
v-if="uiAlertProps"
v-show="showUiAlerts"
:type="uiAlertProps.type"
:dismissible="false"
>
{{ uiAlertProps.text }}
<KButton
v-if="requestsFailed"
appearance="basic-link"
:text="$tr('refreshAddressesButtonLabel')"
@click="refreshAddressList"
/>
</UiAlert>
<KButton
v-show="!newAddressButtonDisabled"
class="new-address-button"
:text="$tr('newAddressButtonLabel')"
appearance="basic-link"
@click="$emit('click_add_address')"
/>
<template v-for="(a, idx) in addresses">
<div :key="`div-${idx}`">
<KRadioButton
:key="idx"
v-model="selectedAddressId"
class="radio-button"
:value="a.id"
:label="a.device_name"
:description="a.base_url"
:disabled="!a.available || !a.hasContent"
/>
<KButton
:key="`forget-${idx}`"
:text="$tr('forgetAddressButtonLabel')"
appearance="basic-link"
@click="removeAddress(a.id)"
/>
</div>
</template>
</KModal>
</template>
<script>
import { mapGetters, mapState } from 'vuex';
import find from 'lodash/find';
import KButton from 'kolibri.coreVue.components.KButton';
import KModal from 'kolibri.coreVue.components.KModal';
import KRadioButton from 'kolibri.coreVue.components.KRadioButton';
import UiAlert from 'keen-ui/src/UiAlert';
import { deleteAddress, fetchAddresses } from './api';
const Stages = {
FETCHING_ADDRESSES: 'FETCHING_ADDRESSES',
FETCHING_SUCCESSFUL: 'FETCHING_SUCCESSFUL',
FETCHING_FAILED: 'FETCHING_FAILED',
DELETING_ADDRESS: 'DELETING_ADDRESS',
DELETING_SUCCESSFUL: 'DELETING_SUCCESSFUL',
DELETING_FAILED: 'DELETING_FAILED',
};
export default {
name: 'SelectAddressForm',
components: {
KButton,
KModal,
KRadioButton,
UiAlert,
},
props: {},
data() {
return {
addresses: [],
selectedAddressId: '',
showUiAlerts: false,
stage: '',
};
},
computed: {
...mapGetters('manageContent/wizard', ['isImportingMore']),
...mapState('manageContent/wizard', ['transferredChannel']),
submitDisabled() {
return (
this.selectedAddressId === '' ||
this.stage === Stages.FETCHING_ADDRESSES ||
this.stage === Stages.DELETING_ADDRESS
);
},
newAddressButtonDisabled() {
return this.stage === Stages.FETCHING_ADDRESSES;
},
requestsSucessful() {
return (
this.stage === Stages.FETCHING_SUCCESSFUL || this.stage === Stages.DELETING_SUCCESSFUL
);
},
requestsFailed() {
return this.stage === Stages.FETCHING_FAILED || this.stage === Stages.DELETING_FAILED;
},
uiAlertProps() {
if (this.stage === Stages.FETCHING_ADDRESSES) {
return {
text: this.$tr('fetchingAddressesText'),
type: 'info',
};
}
if (this.requestsSucessful && this.addresses.length === 0) {
return {
text: this.$tr('noAddressText'),
type: 'info',
};
}
if (this.stage === Stages.FETCHING_FAILED) {
return {
text: this.$tr('fetchingFailedText'),
type: 'error',
};
}
if (this.stage === Stages.DELETING_FAILED) {
return {
text: this.$tr('deletingFailedText'),
type: 'error',
};
}
return null;
},
},
beforeMount() {
return this.refreshAddressList();
},
mounted() {
// Wait a little bit of time before showing UI alerts so there is no flash
// if data comes back quickly
setTimeout(() => {
this.showUiAlerts = true;
}, 100);
},
methods: {
refreshAddressList() {
this.stage = Stages.FETCHING_ADDRESSES;
this.addresses = [];
return fetchAddresses(this.isImportingMore ? this.transferredChannel.id : '')
.then(addresses => {
this.addresses = addresses;
this.resetSelectedAddress();
this.stage = Stages.FETCHING_SUCCESSFUL;
})
.catch(() => {
this.stage = Stages.FETCHING_FAILED;
});
},
resetSelectedAddress() {
const availableAddress = find(this.addresses, { available: true });
if (availableAddress) {
this.selectedAddressId = availableAddress.id;
} else {
this.selectedAddressId = '';
}
},
removeAddress(id) {
this.stage = Stages.DELETING_ADDRESS;
return deleteAddress(id)
.then(() => {
this.addresses = this.addresses.filter(a => a.id !== id);
this.resetSelectedAddress(this.addresses);
this.stage = Stages.DELETING_SUCCESSFUL;
this.$emit('removed_address');
})
.catch(() => {
this.stage = Stages.DELETING_FAILED;
});
},
handleSubmit() {
if (this.selectedAddressId) {
this.$emit('submit', find(this.addresses, { id: this.selectedAddressId }));
}
},
},
$trs: {
cancelButtonLabel: 'Cancel',
deletingFailedText: 'There was a problem removing this address',
fetchingAddressesText: 'Looking for available addresses…',
fetchingFailedText: 'There was a problem getting the available addresses',
forgetAddressButtonLabel: 'Forget',
header: 'Select network address',
newAddressButtonLabel: 'Add new address',
noAddressText: 'There are no addresses yet',
refreshAddressesButtonLabel: 'Refresh addresses',
submitButtonLabel: 'Continue',
},
};
</script>
<style lang="scss" scoped>
.new-address-button {
margin-bottom: 16px;
}
.radio-button {
display: inline-block;
width: 75%;
}
</style>