PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / frontend / src / composables / useFormExport.ts

useFormExport.ts in The Innovative Form Builder – IvyForms 1.0, at frontend/src/composables/useFormExport.ts

82 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { ref } from 'vue'
2 import { useApiClient } from '@/composables/useApiClient'
3 import { useLabels } from '@/composables/useLabels'
4
5 export function useFormExport() {
6 const { getLabel } = useLabels()
7 const { request } = useApiClient()
8 const isExporting = ref(false)
9 const exportError = ref<string | null>(null)
10
11 /**
12 * Export single or multiple forms
13 * @param formIds - Array of form IDs to export
14 */
15 const exportForms = async (formIds: number[], formName?: string) => {
16 isExporting.value = true
17 exportError.value = null
18
19 try {
20 const { data, error } = await request('forms/export', {
21 method: 'POST',
22 data: {
23 formIds,
24 },
25 })
26
27 if (error) {
28 exportError.value = error || getLabel('failed_to_export_forms')
29 return false
30 }
31
32 // API returns: { message: "OK", data: { success: true, data: {...}, message: "..." } }
33 const response = data?.data
34
35 if (response && response.success) {
36 // Create download link
37 const exportData = response.data
38 const dataStr = JSON.stringify(exportData, null, 2)
39 const dataBlob = new Blob([dataStr], { type: 'application/json' })
40 const url = URL.createObjectURL(dataBlob)
41
42 // Create download link
43 const link = document.createElement('a')
44 link.href = url
45
46 // Generate filename
47 const safeName = formName
48 ? formName
49 .trim()
50 .replace(/[^a-z0-9_-]/gi, '-')
51 .replace(/-+/g, '-')
52 .replace(/^-|-$/g, '')
53 : 'ivyforms-export'
54 link.download =
55 formIds.length === 1 ? `${safeName}.json` : `ivyforms-export-${formIds.length}-forms.json`
56
57 document.body.appendChild(link)
58 link.click()
59 document.body.removeChild(link)
60 URL.revokeObjectURL(url)
61
62 return true
63 } else {
64 exportError.value = response?.message || getLabel('failed_to_export_forms')
65 return false
66 }
67 } catch (error) {
68 console.error('Export error:', error)
69 exportError.value = getLabel('failed_to_export_forms')
70 return false
71 } finally {
72 isExporting.value = false
73 }
74 }
75
76 return {
77 isExporting,
78 exportError,
79 exportForms,
80 }
81 }
82