|
20 | 20 | hover:bg-pink-400 hover:text-black" |
21 | 21 | type="submit" |
22 | 22 | value="Perform the magic!" |
23 | | - @click.prevent="fetch" |
| 23 | + @click.prevent="handleSubmit" |
24 | 24 | /> |
25 | 25 | </form> |
26 | 26 | <div v-if="output" class="bg-yellow-300 p-4" @click="copyThis"> |
|
40 | 40 | </div> |
41 | 41 | </div> |
42 | 42 | </template> |
43 | | -<script> |
44 | | -export default { |
45 | | - data() { |
46 | | - return { |
47 | | - statements: '', |
48 | | - output: '', |
49 | | - errorMsg: '', |
50 | | - fetching: false, |
| 43 | + |
| 44 | +<script setup> |
| 45 | +import { ref } from 'vue' |
| 46 | +import { useNuxtApp } from '#app' |
| 47 | +
|
| 48 | +const statements = ref('') |
| 49 | +const output = ref('') |
| 50 | +const errorMsg = ref('') |
| 51 | +const fetching = ref(false) |
| 52 | +
|
| 53 | +const handleSubmit = async () => { |
| 54 | + if (!statements.value) { |
| 55 | + errorMsg.value = "Please enter statements. e.g.\r\nINSERT INTO content(`id`, `title`, `text`)\r\nVALUES\r\n(1, 'title', 'text'),\r\n(2, 'title', 'text');" |
| 56 | + setTimeout(() => { |
| 57 | + errorMsg.value = '' |
| 58 | + }, 5000) |
| 59 | + return |
| 60 | + } |
| 61 | +
|
| 62 | + output.value = 'Fetching...' |
| 63 | + fetching.value = true |
| 64 | + errorMsg.value = '' |
| 65 | +
|
| 66 | + try { |
| 67 | + const formData = new FormData() |
| 68 | + formData.append('statements', statements.value) |
| 69 | +
|
| 70 | + const { data, error } = await useFetch('https://www.james-nock.co.uk/tools/laravelseedergenerator.php', { |
| 71 | + method: 'POST', |
| 72 | + body: formData, |
| 73 | + }) |
| 74 | +
|
| 75 | + if (error.value) { |
| 76 | + throw error.value |
51 | 77 | } |
52 | | - }, |
53 | | - methods: { |
54 | | - prevent(event) { |
55 | | - event.stopPropagation() |
56 | | - }, |
57 | | - async fetch() { |
58 | | - if (!this.statements) { |
59 | | - this.errorMsg = "Please enter statements. e.g.\r\nINSERT INTO content(`id`, `title`, `text`)\r\nVALUES\r\n(1, 'title', 'text'),\r\n(2, 'title', 'text');" |
60 | | - setTimeout(() => { |
61 | | - this.errorMsg = '' |
62 | | - }, 5000) |
63 | | - return |
64 | | - } |
65 | | - this.output = 'Fetching...' |
66 | | - this.fetching = true |
67 | | - this.errorMsg = '' |
68 | | - const formData = new FormData() |
69 | | - formData.append('statements', this.statements) |
70 | | - await this.$axios({ |
71 | | - method: 'post', |
72 | | - url: 'https://www.james-nock.co.uk/tools/laravelseedergenerator.php', |
73 | | - data: formData, |
74 | | - headers: { 'Content-Type': 'multipart/form-data' }, |
75 | | - }) |
76 | | - .then((res) => { |
77 | | - if (!res.data.error) { |
78 | | - const blob = new Blob([res.data]); |
79 | | - const url = window.URL.createObjectURL(blob); |
80 | | - const a = document.createElement('a'); |
81 | | - a.href = url; |
82 | | - const filename = this.statements.split(';')[0].split('(')[0].replace(/INSERT\s?INTO\s?/g, '') + 'Seeder.php'; |
83 | | - a.setAttribute('download', filename[0].toUpperCase() + filename.slice(1)); |
84 | | - document.body.appendChild(a); |
85 | | - a.click(); |
86 | | - this.output = 'File downloading...' |
87 | | - setTimeout(() => { |
88 | | - window.URL.revokeObjectURL(url); |
89 | | - document.body.removeChild(a); |
90 | | - this.fetching = false |
91 | | - this.output = '' |
92 | | - }, 0); |
93 | | - } else { |
94 | | - this.output = '' |
95 | | - this.errorMsg = res.data.message |
96 | | - } |
97 | | - this.fetching = false |
98 | | - }) |
99 | | - }, |
100 | | - copyThis(event) { |
101 | | - this.$nuxt.$emit('copyThis', event.target) |
102 | | - }, |
103 | | - }, |
| 78 | +
|
| 79 | + if (!data.value.error) { |
| 80 | + // Create blob from response data |
| 81 | + const blob = new Blob([data.value]) |
| 82 | + const url = window.URL.createObjectURL(blob) |
| 83 | + const a = document.createElement('a') |
| 84 | + a.href = url |
| 85 | + const filename = statements.value.split(';')[0].split('(')[0].replace(/INSERT\s?INTO\s?/g, '') + 'Seeder.php' |
| 86 | + a.setAttribute('download', filename[0].toUpperCase() + filename.slice(1)) |
| 87 | + document.body.appendChild(a) |
| 88 | + a.click() |
| 89 | + output.value = 'File downloading...' |
| 90 | + setTimeout(() => { |
| 91 | + window.URL.revokeObjectURL(url) |
| 92 | + document.body.removeChild(a) |
| 93 | + fetching.value = false |
| 94 | + output.value = '' |
| 95 | + }, 0) |
| 96 | + } else { |
| 97 | + output.value = '' |
| 98 | + errorMsg.value = data.value.message |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + errorMsg.value = 'An error occurred while processing your request.' |
| 102 | + output.value = '' |
| 103 | + console.error('Error:', error) |
| 104 | + } finally { |
| 105 | + fetching.value = false |
| 106 | + } |
| 107 | +} |
| 108 | +
|
| 109 | +const copyThis = (event) => { |
| 110 | + const { $bus } = useNuxtApp() |
| 111 | + $bus.emit('copyThis', event.target) |
104 | 112 | } |
105 | 113 | </script> |
0 commit comments