/
SolutionCard.vue
237 lines (207 loc) · 8.22 KB
/
SolutionCard.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<template>
<div>
<div
class="solution-toggle"
@click="toggleSolutions"
:class="{
'solution-toggle-show': isHidingSolutions,
}"
>
<a v-if="isHidingSolutions" class="link-solution" target="_blank"
><LightbulbIcon class="text-xs mr-1" /> Show solutions</a
>
<a v-else class="link-solution" target="_blank">Hide solutions</a>
</div>
<div
ref="solutionCard"
class="solution"
:class="{
'solution-hidden': isHidingSolutions,
}"
>
<div class="solution-main">
<div class="solution-background mx-0">
<svg
class="hidden absolute right-0 h-full | md:block"
x="0px"
y="0px"
viewBox="0 0 299 452"
>
<g style="opacity: 0.075">
<polygon
style="fill:rgb(63,63,63)"
points="298.1,451.9 150.9,451.9 21,226.9 298.1,227.1"
/>
<polygon
style="fill:rgb(151,151,151)"
points="298.1,227.1 21,226.9 150.9,1.9 298.1,1.9"
/>
</g>
</svg>
</div>
<div class="p-12">
<div class="solution-content ml-0">
<h2 class="solution-title">{{ solution.title }}</h2>
<div
v-if="solution.description"
v-html="markdown(solution.description)"
></div>
<div v-if="solution.is_runnable">
<p v-html="markdown(solution.action_description)"></p>
<p v-if="canExecuteSolutions === null" class="py-4 text-sm italic">
Loading...
</p>
<div class="mt-4">
<button
v-if="
solution.is_runnable &&
canExecuteSolutions === true &&
executionSuccessful === null
"
@click="execute"
class="button-secondary button-lg bg-tint-300 hover:bg-tint-400"
>
{{ solution.run_button_text }}
</button>
<p v-if="executionSuccessful">
<strong class="font-semibold"
>The solution was executed succesfully.</strong
>
<a href="#" @click.prevent="refresh" class="link-solution"
>Refresh now.</a
>
</p>
<p v-if="executionSuccessful === false">
Something went wrong when executing the solution. Please try
refresh the page and try again.
</p>
</div>
</div>
<div
class="mt-8 grid justify-start"
v-if="Object.entries(solution.links).length > 0"
>
<div class="border-t-2 border-gray-700 opacity-25 " />
<div class="pt-2 grid cols-auto-1fr gapx-4 gapy-2 text-sm">
<label class="font-semibold uppercase tracking-wider"
>Read more</label
>
<ul>
<li v-for="(link, label) in solution.links" :key="label">
<a :href="link" class="link-solution" target="_blank">{{
label
}}</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
const MarkdownIt = require('markdown-it')();
const cookieName = 'hide_solutions';
let animationTimeout = null;
export default {
inject: ['config'],
props: {
solution: { required: true },
},
data() {
return {
isHidingSolutions: this.hasHideSolutionsCookie(),
canExecuteSolutions: null,
executionSuccessful: null,
};
},
computed: {
healthCheckEndpoint() {
return this.solution.execute_endpoint.replace('execute-solution', 'health-check');
},
},
created() {
this.configureRunnableSolutions();
},
mounted() {
if (this.isHidingSolutions) {
this.$refs.solutionCard.classList.add('solution-hidden');
}
},
methods: {
configureRunnableSolutions() {
if (!this.config.enableRunnableSolutions) {
this.canExecuteSolutions = false;
return;
}
this.checkExecutionEndpoint();
},
markdown(string) {
return MarkdownIt.render(string);
},
async checkExecutionEndpoint() {
try {
const healthCheck = await (await fetch(this.healthCheckEndpoint)).json();
this.canExecuteSolutions = healthCheck.can_execute_commands;
} catch (error) {
this.canExecuteSolutions = false;
}
},
async execute() {
try {
const response = await fetch(this.solution.execute_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
solution: this.solution.class,
parameters: this.solution.run_parameters,
}),
});
this.executionSuccessful = response.status === 200;
} catch (error) {
console.error(error);
this.executionSuccessful = false;
}
},
refresh() {
location.reload();
},
getUrlLabel(url) {
const tempElement = document.createElement('a');
tempElement.href = url;
return tempElement.hostname;
},
toggleSolutions() {
if (!this.isHidingSolutions) {
this.$refs.solutionCard.classList.add('solution-hiding');
animationTimeout = window.setTimeout(() => {
this.$refs.solutionCard.classList.remove('solution-hiding');
this.toggleHidingSolutions();
}, 100);
} else {
window.clearTimeout(animationTimeout);
this.toggleHidingSolutions();
}
},
toggleHidingSolutions() {
if (this.isHidingSolutions) {
document.cookie = `${cookieName}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;`;
this.isHidingSolutions = false;
return;
}
const expires = new Date();
expires.setTime(expires.getTime() + 365 * 24 * 60 * 60 * 1000);
document.cookie = `${cookieName}=true;expires=${expires.toUTCString()};path=/;`;
this.isHidingSolutions = true;
},
hasHideSolutionsCookie() {
return document.cookie.includes(cookieName);
},
},
};
</script>