Skip to content

Commit

Permalink
feat: allow adding custom bulk actions
Browse files Browse the repository at this point in the history
  • Loading branch information
shariquerik committed Feb 6, 2024
1 parent 41cfce7 commit f0122a2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 20 deletions.
2 changes: 2 additions & 0 deletions crm/api/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pypika import Criterion

from crm.api.views import get_views
from crm.fcrm.doctype.crm_form_script.crm_form_script import get_form_script


@frappe.whitelist()
Expand Down Expand Up @@ -239,6 +240,7 @@ def get_list_data(
"views": get_views(doctype),
"total_count": len(frappe.get_all(doctype, filters=filters)),
"row_count": len(data),
"form_script": get_form_script(doctype)
}


Expand Down
48 changes: 39 additions & 9 deletions frontend/src/components/ListViews/DealsListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@
</ListRows>
<ListSelectBanner>
<template #actions="{ selections, unselectAll }">
<Button variant="subtle" label="Edit" @click="editValues(selections, unselectAll)">
<template #prefix>
<EditIcon class="h-3 w-3" />
</template>
</Button>
<Dropdown
v-if="bulkActions.length"
:options="bulkActions(selections, unselectAll)"
>
<Button variant="ghost">
<template #icon>
<FeatherIcon name="more-horizontal" class="h-4 w-4" />
</template>
</Button>
</Dropdown>
</template>
</ListSelectBanner>
</ListView>
Expand All @@ -110,15 +115,14 @@
v-model:unselectAll="unselectAllAction"
doctype="CRM Deal"
:selectedValues="selectedValues"
@reload="emit('reload')"
@reload="list.reload()"
/>
</template>

<script setup>
import MultipleAvatar from '@/components/MultipleAvatar.vue'
import IndicatorIcon from '@/components/Icons/IndicatorIcon.vue'
import PhoneIcon from '@/components/Icons/PhoneIcon.vue'
import EditIcon from '@/components/Icons/EditIcon.vue'
import EditValueModal from '@/components/Modals/EditValueModal.vue'
import {
Avatar,
Expand All @@ -129,8 +133,10 @@ import {
ListRowItem,
ListSelectBanner,
ListFooter,
Dropdown,
} from 'frappe-ui'
import { ref, watch } from 'vue'
import { setupBulkActions } from '@/utils'
import { onMounted, ref, watch } from 'vue'
const props = defineProps({
rows: {
Expand All @@ -151,9 +157,10 @@ const props = defineProps({
},
})
const emit = defineEmits(['loadMore', 'updatePageCount', 'reload'])
const emit = defineEmits(['loadMore', 'updatePageCount'])
const pageLengthCount = defineModel()
const list = defineModel('list')
watch(pageLengthCount, (val, old_value) => {
if (val === old_value) return
Expand All @@ -169,4 +176,27 @@ function editValues(selections, unselectAll) {
showEditModal.value = true
unselectAllAction.value = unselectAll
}
const customBulkActions = ref([])
function bulkActions(selections, unselectAll) {
let actions = [
{
label: 'Edit',
onClick: () => editValues(selections, unselectAll),
},
]
customBulkActions.value.forEach((action) => {
actions.push({
label: action.label,
onClick: () => action.onClick(selections, unselectAll, list.value),
})
})
return actions
}
onMounted(() => {
setupBulkActions(list.value.data)
customBulkActions.value = list.value?.data?.bulkActions || []
})
</script>
48 changes: 39 additions & 9 deletions frontend/src/components/ListViews/LeadsListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@
</ListRows>
<ListSelectBanner>
<template #actions="{ selections, unselectAll }">
<Button variant="subtle" label="Edit" @click="editValues(selections, unselectAll)">
<template #prefix>
<EditIcon class="h-3 w-3" />
</template>
</Button>
<Dropdown
v-if="bulkActions.length"
:options="bulkActions(selections, unselectAll)"
>
<Button variant="ghost">
<template #icon>
<FeatherIcon name="more-horizontal" class="h-4 w-4" />
</template>
</Button>
</Dropdown>
</template>
</ListSelectBanner>
</ListView>
Expand All @@ -119,15 +124,14 @@
v-model:unselectAll="unselectAllAction"
doctype="CRM Lead"
:selectedValues="selectedValues"
@reload="emit('reload')"
@reload="list.reload()"
/>
</template>

<script setup>
import IndicatorIcon from '@/components/Icons/IndicatorIcon.vue'
import PhoneIcon from '@/components/Icons/PhoneIcon.vue'
import MultipleAvatar from '@/components/MultipleAvatar.vue'
import EditIcon from '@/components/Icons/EditIcon.vue'
import EditValueModal from '@/components/Modals/EditValueModal.vue'
import {
Avatar,
Expand All @@ -138,8 +142,10 @@ import {
ListSelectBanner,
ListRowItem,
ListFooter,
Dropdown,
} from 'frappe-ui'
import { ref, watch } from 'vue'
import { setupBulkActions } from '@/utils'
import { onMounted, ref, watch } from 'vue'
const props = defineProps({
rows: {
Expand All @@ -160,9 +166,10 @@ const props = defineProps({
},
})
const emit = defineEmits(['loadMore', 'updatePageCount', 'reload'])
const emit = defineEmits(['loadMore', 'updatePageCount'])
const pageLengthCount = defineModel()
const list = defineModel('list')
watch(pageLengthCount, (val, old_value) => {
if (val === old_value) return
Expand All @@ -178,4 +185,27 @@ function editValues(selections, unselectAll) {
showEditModal.value = true
unselectAllAction.value = unselectAll
}
const customBulkActions = ref([])
function bulkActions(selections, unselectAll) {
let actions = [
{
label: 'Edit',
onClick: () => editValues(selections, unselectAll),
},
]
customBulkActions.value.forEach((action) => {
actions.push({
label: action.label,
onClick: () => action.onClick(selections, unselectAll, list.value),
})
})
return actions
}
onMounted(() => {
setupBulkActions(list.value.data)
customBulkActions.value = list.value?.data?.bulkActions || []
})
</script>
2 changes: 1 addition & 1 deletion frontend/src/pages/Deals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<DealsListView
v-if="deals.data && rows.length"
v-model="deals.data.page_length_count"
v-model:list="deals"
:rows="rows"
:columns="deals.data.columns"
:options="{
Expand All @@ -26,7 +27,6 @@
}"
@loadMore="() => loadMore++"
@updatePageCount="(count) => (updatedPageCount = count)"
@reload="deals.reload()"
/>
<div v-else-if="deals.data" class="flex h-full items-center justify-center">
<div
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Leads.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<LeadsListView
v-if="leads.data && rows.length"
v-model="leads.data.page_length_count"
v-model:list="leads"
:rows="rows"
:columns="leads.data.columns"
:options="{
Expand All @@ -27,7 +28,6 @@
}"
@loadMore="() => loadMore++"
@updatePageCount="(count) => (updatedPageCount = count)"
@reload="leads.reload()"
/>
<div v-else-if="leads.data" class="flex h-full items-center justify-center">
<div
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export function setupCustomActions(data, obj) {
data._customActions = formScript?.actions || []
}

export function setupBulkActions(data, obj = {}) {
if (!data.form_script) return []
let script = new Function(data.form_script + '\nreturn setupForm')()
let formScript = script(obj)
data.bulkActions = formScript?.bulk_actions || []
}

export function errorMessage(title, message) {
createToast({
title: title || 'Error',
Expand Down

0 comments on commit f0122a2

Please sign in to comment.