Skip to content

Commit

Permalink
feat: encapsulating logic in saveConnection and display error message…
Browse files Browse the repository at this point in the history
… in ui
  • Loading branch information
ChenChunShenG19 committed Dec 22, 2024
1 parent 96b0c90 commit 6ae15a1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/store/connectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ export const useConnectionStore = defineStore('connectionStore', {

return await client.get(con.indexName ?? undefined, 'format=json');
},
async saveConnection(connection: Connection) {
console.log('Saving connection:', connection);
async saveConnection(connection: Connection): Promise<{ success: boolean; message: string }> {
try {
const newConnection = {
...connection,
Expand All @@ -134,10 +133,10 @@ export const useConnectionStore = defineStore('connectionStore', {
}

await storeApi.set('connections', pureObject(this.connections));
return newConnection;
return { success: true, message: 'Connection saved successfully' };
} catch (error) {
console.error('Error saving connection:', error);
throw error;
return { success: false, message: error instanceof Error ? error.message : 'Unknown error' };
}
},
async removeConnection(connection: Connection) {
Expand Down
22 changes: 6 additions & 16 deletions src/views/connect/components/dynamodb-connect-dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,15 @@ const testConnect = async () => {
};
const saveConnect = async () => {
try {
saveLoading.value = true;
await connectionStore.saveConnection(formData.value);
saveLoading.value = true;
const result = await connectionStore.saveConnection(formData.value);
if (result.success) {
message.success(lang.t('connection.saveSuccess'));
closeModal();
} catch (error) {
if (!(error instanceof Error && error.message.includes('__TAURI_IPC__'))) {
if (error instanceof Error) {
message.error(error.message);
} else {
message.error(lang.t('connection.unknownError'));
}
} else {
message.success(lang.t('connection.saveSuccess'));
closeModal();
}
} finally {
saveLoading.value = false;
} else {
message.error(result.message);
}
saveLoading.value = false;
};
defineExpose({ showMedal });
Expand Down

0 comments on commit 6ae15a1

Please sign in to comment.