|
1 | 1 | <script> |
2 | | - import Breadcrumb from '$lib/common/Breadcrumb.svelte'; |
3 | | - import HeadTitle from '$lib/common/HeadTitle.svelte'; |
| 2 | + import { onMount } from 'svelte'; |
4 | 3 | import { _ } from 'svelte-i18n'; |
| 4 | + import Swal from 'sweetalert2'; |
5 | 5 | import { |
6 | 6 | Card, |
7 | 7 | CardBody, |
|
13 | 13 | NavLink, |
14 | 14 | Row, |
15 | 15 | TabContent, |
16 | | - TabPane |
| 16 | + TabPane, |
| 17 | + Button |
17 | 18 | } from '@sveltestrap/sveltestrap'; |
18 | | - import { onMount } from 'svelte'; |
| 19 | + import Breadcrumb from '$lib/common/Breadcrumb.svelte'; |
| 20 | + import HeadTitle from '$lib/common/HeadTitle.svelte'; |
19 | 21 | import { getSettings, getSettingDetail } from '$lib/services/setting-service'; |
20 | 22 | import { JSONEditor } from 'svelte-jsoneditor'; |
| 23 | + import { refreshAgents } from '$lib/services/agent-service'; |
| 24 | + import LoadingToComplete from '$lib/common/LoadingToComplete.svelte'; |
| 25 | + |
| 26 | + const duration = 3000; |
| 27 | + let isLoading = false; |
| 28 | + let isComplete = false; |
| 29 | + let isError = false; |
| 30 | + let successText = ''; |
| 31 | + let errorText = ''; |
21 | 32 |
|
22 | | - let customActiveTab = '1'; |
| 33 | + let selectedTab = '1'; |
23 | 34 | /** @type {string[]} */ |
24 | 35 | let settings = []; |
25 | 36 |
|
26 | | - let content = { |
27 | | - json: { |
28 | | - } |
29 | | - }; |
| 37 | + let content = { json: {} }; |
30 | 38 |
|
31 | 39 | onMount(async () => { |
32 | 40 | settings = await getSettings(); |
33 | | - customActiveTab = settings[0]; |
34 | | - handleGetSettingDetail(settings[0]); |
| 41 | + selectedTab = settings[0]; |
| 42 | + handleGetSettingDetail(selectedTab); |
35 | 43 | }); |
36 | 44 |
|
37 | 45 | /** |
38 | | - * |
39 | 46 | * @param {string} tab |
40 | 47 | */ |
41 | 48 | async function handleGetSettingDetail(tab) { |
42 | | - customActiveTab = tab; |
| 49 | + selectedTab = tab; |
43 | 50 | const detail = await getSettingDetail(tab); |
44 | 51 | content = { |
45 | 52 | json: detail |
46 | 53 | }; |
47 | 54 | } |
| 55 | +
|
| 56 | + function readyToRefresh() { |
| 57 | + // @ts-ignore |
| 58 | + Swal.fire({ |
| 59 | + title: 'Are you sure?', |
| 60 | + text: "You will migrate all agents data to mongoDb.", |
| 61 | + icon: 'warning', |
| 62 | + showCancelButton: true, |
| 63 | + confirmButtonText: 'Yes', |
| 64 | + cancelButtonText: 'No' |
| 65 | + }).then((result) => { |
| 66 | + if (result.value) { |
| 67 | + refreshAgentData(); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | +
|
| 72 | + const refreshAgentData = () => { |
| 73 | + isLoading = true; |
| 74 | + refreshAgents().then(res => { |
| 75 | + isComplete = true; |
| 76 | + isLoading = false; |
| 77 | + successText = res; |
| 78 | + setTimeout(() => { |
| 79 | + isComplete = false; |
| 80 | + successText = ''; |
| 81 | + }, duration); |
| 82 | + }).catch(err => { |
| 83 | + isLoading = false; |
| 84 | + isComplete = false; |
| 85 | + isError = true; |
| 86 | + errorText = 'Failed to migrate agents.'; |
| 87 | + setTimeout(() => { |
| 88 | + isError = false; |
| 89 | + errorText = ''; |
| 90 | + }, duration); |
| 91 | + }); |
| 92 | + }; |
48 | 93 | </script> |
49 | 94 |
|
50 | 95 | <HeadTitle title="{$_('Settings')}" /> |
51 | | - |
52 | 96 | <Breadcrumb title="{$_('Settings')}" pagetitle="{$_('Detail')}" /> |
| 97 | +<LoadingToComplete |
| 98 | + isLoading={isLoading} |
| 99 | + isComplete={isComplete} |
| 100 | + isError={isError} |
| 101 | + successText={successText} |
| 102 | + errorText={errorText} |
| 103 | +/> |
53 | 104 |
|
54 | 105 | <Card> |
55 | 106 | <CardBody> |
|
62 | 113 | <NavLink |
63 | 114 | style="cursor: pointer" |
64 | 115 | on:click={() => handleGetSettingDetail(tab)} |
65 | | - active={customActiveTab == tab} |
| 116 | + active={selectedTab == tab} |
66 | 117 | > |
67 | 118 | <span class="d-block d-sm-none"> |
68 | 119 | <i class="fas fa-home" /> |
|
73 | 124 | {/each} |
74 | 125 | </Nav> |
75 | 126 |
|
76 | | - <TabContent activeTab={customActiveTab} class="p-3 text-muted"> |
| 127 | + <TabContent class="p-3 text-muted"> |
77 | 128 | {#each settings as tab} |
78 | | - <TabPane tabId={tab} class={customActiveTab == tab ? 'active' : ''}> |
| 129 | + <TabPane tabId={tab} class={selectedTab == tab ? 'active' : ''}> |
79 | 130 | <Row> |
80 | 131 | <Col sm="12"> |
81 | 132 | <CardText class="mb-0"> |
82 | 133 | <div class="my-json-editor"> |
83 | | - <JSONEditor bind:content expand={() => { return true;}}/> |
| 134 | + <JSONEditor bind:content /> |
84 | 135 | </div> |
85 | 136 | </CardText> |
86 | 137 | </Col> |
|
91 | 142 | </CardBody> |
92 | 143 | </Card> |
93 | 144 |
|
| 145 | + |
| 146 | +<Card> |
| 147 | + <CardBody> |
| 148 | + <CardTitle class="h4">{$_('Migrate agents from file repository to MongoDB')}</CardTitle> |
| 149 | + <p class="card-title-desc"></p> |
| 150 | + |
| 151 | + <Button color="primary" on:click={() => readyToRefresh()} disabled={isLoading}> |
| 152 | + <i class="bx bx-copy" /> {$_('Start Migration')} |
| 153 | + </Button> |
| 154 | + </CardBody> |
| 155 | +</Card> |
| 156 | + |
94 | 157 | <style> |
95 | 158 | .my-json-editor { |
96 | 159 | /* define a custom theme color */ |
|
0 commit comments