2022-08-06 04:25:16 +08:00
|
|
|
<script>
|
2022-08-06 13:03:34 +08:00
|
|
|
import { createEventDispatcher } from "svelte";
|
|
|
|
import ApiClient from "@/utils/ApiClient";
|
2022-08-06 23:15:18 +08:00
|
|
|
import CommonHelper from "@/utils/CommonHelper";
|
2022-08-06 13:03:34 +08:00
|
|
|
import { addSuccessToast } from "@/stores/toasts";
|
2022-08-09 21:16:09 +08:00
|
|
|
import { confirm } from "@/stores/confirmation";
|
2022-08-10 18:22:27 +08:00
|
|
|
import OverlayPanel from "@/components/base/OverlayPanel.svelte";
|
|
|
|
import CollectionsDiffTable from "@/components/collections/CollectionsDiffTable.svelte";
|
2022-08-06 04:25:16 +08:00
|
|
|
|
2022-08-06 13:03:34 +08:00
|
|
|
const dispatch = createEventDispatcher();
|
2022-08-06 04:25:16 +08:00
|
|
|
|
|
|
|
let panel;
|
2022-08-06 13:03:34 +08:00
|
|
|
let oldCollections = [];
|
|
|
|
let newCollections = [];
|
2022-08-10 18:22:27 +08:00
|
|
|
let pairs = [];
|
|
|
|
let deleteMissing = false;
|
2022-08-06 13:03:34 +08:00
|
|
|
let isImporting = false;
|
2022-08-06 04:25:16 +08:00
|
|
|
|
2022-08-06 23:15:18 +08:00
|
|
|
$: if (Array.isArray(oldCollections) && Array.isArray(newCollections)) {
|
2022-08-10 18:22:27 +08:00
|
|
|
loadPairs();
|
2022-08-06 23:15:18 +08:00
|
|
|
}
|
|
|
|
|
2022-08-10 18:22:27 +08:00
|
|
|
export function show(oldCollectionsArg, newCollectionsArg, deleteMissingArg = false) {
|
|
|
|
oldCollections = oldCollectionsArg;
|
|
|
|
newCollections = newCollectionsArg;
|
|
|
|
deleteMissing = deleteMissingArg;
|
2022-08-06 04:25:16 +08:00
|
|
|
|
|
|
|
panel?.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hide() {
|
|
|
|
return panel?.hide();
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:22:27 +08:00
|
|
|
function loadPairs() {
|
|
|
|
pairs = [];
|
2022-08-06 23:15:18 +08:00
|
|
|
|
2022-08-10 21:16:59 +08:00
|
|
|
// add modified and deleted (if deleteMissing is set)
|
2022-08-06 23:15:18 +08:00
|
|
|
for (const oldCollection of oldCollections) {
|
|
|
|
const newCollection = CommonHelper.findByKey(newCollections, "id", oldCollection.id) || null;
|
2022-08-10 21:16:59 +08:00
|
|
|
if (
|
|
|
|
(deleteMissing && !newCollection?.id) ||
|
|
|
|
(newCollection?.id &&
|
|
|
|
CommonHelper.hasCollectionChanges(oldCollection, newCollection, deleteMissing))
|
|
|
|
) {
|
|
|
|
pairs.push({
|
|
|
|
old: oldCollection,
|
|
|
|
new: newCollection,
|
|
|
|
});
|
|
|
|
}
|
2022-08-06 23:15:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// add only new collections
|
|
|
|
for (const newCollection of newCollections) {
|
|
|
|
const oldCollection = CommonHelper.findByKey(oldCollections, "id", newCollection.id) || null;
|
|
|
|
if (!oldCollection?.id) {
|
2022-08-10 18:22:27 +08:00
|
|
|
pairs.push({
|
2022-08-06 23:15:18 +08:00
|
|
|
old: oldCollection,
|
|
|
|
new: newCollection,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:22:27 +08:00
|
|
|
function submitWithConfirm() {
|
|
|
|
// find deleted fields
|
|
|
|
const deletedFieldNames = [];
|
|
|
|
if (deleteMissing) {
|
|
|
|
for (const old of oldCollections) {
|
2022-08-10 21:16:59 +08:00
|
|
|
const imported = CommonHelper.findByKey(newCollections, "id", old.id);
|
2022-08-10 18:22:27 +08:00
|
|
|
if (!imported) {
|
|
|
|
// add all fields
|
|
|
|
deletedFieldNames.push(old.name + ".*");
|
|
|
|
} else {
|
|
|
|
// add only deleted fields
|
|
|
|
const schema = Array.isArray(old.schema) ? old.schema : [];
|
|
|
|
for (const field of schema) {
|
|
|
|
if (!CommonHelper.findByKey(imported.schema, "id", field.id)) {
|
2022-08-10 21:16:59 +08:00
|
|
|
deletedFieldNames.push(`${old.name}.${field.name} (${field.id})`);
|
2022-08-10 18:22:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-06 04:25:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:22:27 +08:00
|
|
|
if (deletedFieldNames.length) {
|
2022-08-09 21:16:09 +08:00
|
|
|
confirm(
|
2022-08-10 18:22:27 +08:00
|
|
|
`Do you really want to delete the following collection fields and their related records data:\n- ${deletedFieldNames.join(
|
2022-08-09 21:16:09 +08:00
|
|
|
"\n- "
|
2022-08-10 18:22:27 +08:00
|
|
|
)}`,
|
2022-08-09 21:16:09 +08:00
|
|
|
() => {
|
|
|
|
submit();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
submit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function submit() {
|
2022-08-06 13:03:34 +08:00
|
|
|
if (isImporting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isImporting = true;
|
|
|
|
|
|
|
|
try {
|
2022-08-10 18:22:27 +08:00
|
|
|
await ApiClient.collections.import(newCollections, deleteMissing);
|
|
|
|
addSuccessToast("Successfully imported collections configuration.");
|
2022-08-06 13:03:34 +08:00
|
|
|
dispatch("submit");
|
|
|
|
} catch (err) {
|
|
|
|
ApiClient.errorResponseHandler(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
isImporting = false;
|
2022-08-06 23:59:28 +08:00
|
|
|
|
|
|
|
hide();
|
2022-08-06 13:03:34 +08:00
|
|
|
}
|
2022-08-06 04:25:16 +08:00
|
|
|
</script>
|
|
|
|
|
2022-08-06 23:15:18 +08:00
|
|
|
<OverlayPanel
|
|
|
|
bind:this={panel}
|
2022-08-21 19:30:36 +08:00
|
|
|
class="full-width-popup import-popup"
|
2022-08-06 23:15:18 +08:00
|
|
|
overlayClose={false}
|
2022-08-06 23:59:28 +08:00
|
|
|
escClose={!isImporting}
|
|
|
|
beforeHide={() => !isImporting}
|
2022-08-06 23:15:18 +08:00
|
|
|
popup
|
|
|
|
on:show
|
|
|
|
on:hide
|
|
|
|
>
|
2022-08-06 04:25:16 +08:00
|
|
|
<svelte:fragment slot="header">
|
2022-08-06 13:03:34 +08:00
|
|
|
<h4 class="center txt-break">Side-by-side diff</h4>
|
2022-08-06 04:25:16 +08:00
|
|
|
</svelte:fragment>
|
|
|
|
|
2022-08-10 18:22:27 +08:00
|
|
|
{#each pairs as pair}
|
|
|
|
<CollectionsDiffTable collectionA={pair.old} collectionB={pair.new} {deleteMissing} />
|
|
|
|
{/each}
|
2022-08-06 04:25:16 +08:00
|
|
|
|
|
|
|
<svelte:fragment slot="footer">
|
2022-08-06 23:59:28 +08:00
|
|
|
<button type="button" class="btn btn-secondary" on:click={hide} disabled={isImporting}>Close</button>
|
2022-08-06 13:03:34 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
2022-08-06 23:15:18 +08:00
|
|
|
class="btn btn-expanded"
|
2022-08-06 13:03:34 +08:00
|
|
|
class:btn-loading={isImporting}
|
2022-08-06 23:59:28 +08:00
|
|
|
disabled={isImporting}
|
2022-08-09 21:16:09 +08:00
|
|
|
on:click={() => submitWithConfirm()}
|
2022-08-06 13:03:34 +08:00
|
|
|
>
|
|
|
|
<span class="txt">Confirm and import</span>
|
|
|
|
</button>
|
2022-08-06 04:25:16 +08:00
|
|
|
</svelte:fragment>
|
|
|
|
</OverlayPanel>
|