Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add confirmation dialog before importing contacts #3759

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/components/ConfirmationDialog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<template>
<NcModal class="confirm-modal" @close="cancel">
<div class="confirm-modal">
<h2>{{ title }}</h2>
<slot />
<div class="confirm-modal__buttons">
<NcButton type="tertiary" :disabled="disabled" @click="cancel">
{{ t('contacts', 'Cancel') }}
</NcButton>
<NcButton v-if="resolve"
:disabled="disabled"
type="primary"
@click="confirm">
{{ confirmText }}
</NcButton>
</div>
</div>
</NcModal>
</template>

<script>
import { NcButton, NcModal } from '@nextcloud/vue'
import { translate as t } from '@nextcloud/l10n'

export default {
name: 'ConfirmationDialog',
components: {
NcButton,
NcModal,
},
props: {
title: {
type: String,
required: true,
},
resolve: {
type: Function,
required: true,
},
reject: {
type: Function,
required: true,
},
confirmText: {
type: String,
default: t('contacts', 'Confirm'),
},
disabled: {
type: Boolean,
default: undefined,
},
},
methods: {
confirm() {
this.resolve()
},
cancel() {
this.reject()
},
},
}
</script>

<style lang="scss" scoped>
.confirm-modal {
padding: 20px;

&__buttons {
display: flex;
justify-content: space-between;
}
}
</style>
36 changes: 33 additions & 3 deletions src/files-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import ConfirmationDialog from './components/ConfirmationDialog.vue'

import { generateUrl } from '@nextcloud/router'
import { translate as t } from '@nextcloud/l10n'
import { registerFileAction, FileAction, Permission, DefaultType } from '@nextcloud/files'
import { DefaultType, FileAction, Permission, registerFileAction } from '@nextcloud/files'
/* eslint-disable-next-line import/no-unresolved */
import ContactSvg from '@mdi/svg/svg/account-multiple.svg?raw'
import Vue from 'vue'

Vue.prototype.t = t

const mime = 'text/vcard'
const name = 'contacts-import'
Expand All @@ -44,8 +49,33 @@ if (nextcloudVersionIsGreaterThanOr28) {
},
iconSvgInline: () => ContactSvg,
async exec(file) {
window.location = generateUrl(`/apps/contacts/import?file=${file.path}`)
return true
let dialog
try {
// Open the confirmation dialog
const containerId = 'confirmation-' + Math.random().toString(16).slice(2)
const container = document.createElement('div')
container.id = containerId
document.body.appendChild(container)
await new Promise((resolve, reject) => {
const ImportConfirmationDialog = Vue.extend(ConfirmationDialog)
dialog = new ImportConfirmationDialog({
propsData: {
title: t('contacts', 'Are you sure you want to import this contact file?'),
resolve,
reject,
},
})
dialog.$mount(`#${containerId}`)
})

// Redirect to the import page if the user confirmed
window.location = generateUrl(`/apps/contacts/import?file=${file.path}`)
} catch (e) {
// Do nothing if the user cancels
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logging would be advisable here if anything but rejection is thrown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can do that with a follow up

}

// Destroy confirmation modal (div element is removed from the DOM by vue)
dialog.$destroy()
},
}))
} else {
Expand Down
Loading