Skip to content

Commit

Permalink
handle promise and prompt success
Browse files Browse the repository at this point in the history
  • Loading branch information
qcgm1978 committed Aug 10, 2023
1 parent ad34c9b commit 0adcdc0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
23 changes: 17 additions & 6 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,23 @@ ipcMain.handle("upload", async (event, { MongoDB_URL, data }) => {
// eslint-disable-next-line no-debugger
// debugger;
const client = new MongoClient(MongoDB_URL);

client.connect();

const collection = client.db("test").collection("dialogs");
// const chunks = chunkify(data); // 将大对象拆分成块
collection.insertOne(data);
return client
.connect()
.then(() => {
// eslint-disable-next-line no-debugger
// debugger;
const collection = client.db("test").collection("dialogs");
// const chunks = chunkify(data); // 将大对象拆分成块
return collection.insertOne(data);
})
.then(() => {
// eslint-disable-next-line no-debugger
// debugger;
return true;
})
.catch(() => {
return false;
});
});
ipcMain.handle("download", async (event, MongoDB_URL) => {
// eslint-disable-next-line no-debugger
Expand Down
26 changes: 23 additions & 3 deletions src/components/ChatSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@
</v-btn>
</v-list-item>
<ConfirmModal ref="confirmModal" />
<v-snackbar
v-model="snackbar.show"
:timeout="snackbar.timeout"
:color="snackbar.color"
>
{{ snackbar.text }}
</v-snackbar>
</template>

<script setup>
import { ref } from "vue";
import { ref, reactive } from "vue";
import { useStore } from "vuex";
import i18n from "@/i18n";
const electron = window.require("electron");
Expand All @@ -71,7 +78,12 @@ const confirmModal = ref();
const store = useStore();
const jsonData = ref(null);
const MongoDB_URL = ref(store.state.MongoDB_URL);

const snackbar = reactive({
show: false,
text: "",
timeout: 1500,
color: "success",
});
const setMongoDBURL = (url) => {
store.commit("setMongoDBURL", url);
};
Expand All @@ -82,10 +94,18 @@ async function upload() {
);
if (result) {
const data = JSON.parse(JSON.stringify(localStorage, null, 2));
await ipcRenderer.invoke("upload", {
// don't know why there's _id prop in localStorage that would lead to duplicate key error
delete data._id;
const is_success = await ipcRenderer.invoke("upload", {
MongoDB_URL: MongoDB_URL.value,
data,
});
if (is_success) {
snackbar.text = i18n.global.t("proxy.saveSuccess");
snackbar.color = "success";
snackbar.timeout = 1000;
snackbar.show = true;
}
}
}
async function download() {
Expand Down

0 comments on commit 0adcdc0

Please sign in to comment.