pocketbase/ui/src/components/settings/ImportPopup.svelte

152 lines
4.7 KiB
Svelte
Raw Normal View History

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";
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
$: if (Array.isArray(oldCollections) && Array.isArray(newCollections)) {
2022-08-10 18:22:27 +08:00
loadPairs();
}
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 = [];
// add modified and deleted (if deleteMissing is set)
for (const oldCollection of oldCollections) {
const newCollection = CommonHelper.findByKey(newCollections, "id", oldCollection.id) || null;
if (
(deleteMissing && !newCollection?.id) ||
(newCollection?.id &&
CommonHelper.hasCollectionChanges(oldCollection, newCollection, deleteMissing))
) {
pairs.push({
old: oldCollection,
new: newCollection,
});
}
}
// 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({
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) {
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)) {
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>
<OverlayPanel
bind:this={panel}
class="full-width-popup import-popup"
overlayClose={false}
2022-08-06 23:59:28 +08:00
escClose={!isImporting}
beforeHide={() => !isImporting}
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">
<button type="button" class="btn btn-transparent" on:click={hide} disabled={isImporting}>Close</button>
2022-08-06 13:03:34 +08:00
<button
type="button"
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>