Skip to content

Commit

Permalink
feat: stage and attributes updates completed
Browse files Browse the repository at this point in the history
  • Loading branch information
devansh-webkul committed Aug 21, 2024
1 parent a7a5767 commit 320ea11
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 14 deletions.
60 changes: 60 additions & 0 deletions packages/Webkul/Admin/src/Http/Controllers/Lead/LeadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Webkul\Admin\Http\Requests\MassUpdateRequest;
use Webkul\Admin\Http\Resources\LeadResource;
use Webkul\Admin\Http\Resources\StageResource;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Lead\Repositories\LeadRepository;
use Webkul\Lead\Repositories\PipelineRepository;
use Webkul\Lead\Repositories\ProductRepository;
Expand All @@ -33,6 +34,7 @@ class LeadController extends Controller
*/
public function __construct(
protected UserRepository $userRepository,
protected AttributeRepository $attributeRepository,
protected SourceRepository $sourceRepository,
protected TypeRepository $typeRepository,
protected PipelineRepository $pipelineRepository,
Expand Down Expand Up @@ -331,6 +333,64 @@ public function update(LeadForm $request, int $id): RedirectResponse|JsonRespons
}
}

/**
* Update the lead attributes.
*/
public function updateAttributes(int $id)
{
$data = request()->all();

$attributes = $this->attributeRepository->findWhere([
'entity_type' => 'leads',
['code', 'NOTIN', ['title', 'description']],
])
->pluck('code')
->toArray();

Event::dispatch('lead.update.before', $id);

$lead = $this->leadRepository->update($data, $id, $attributes);

Event::dispatch('lead.update.after', $lead);

return response()->json([
'message' => trans('admin::app.leads.update-success'),
]);
}

/**
* Update the lead stage.
*/
public function updateStage(int $id)
{
$this->validate(request(), [
'lead_pipeline_stage_id' => 'required',
]);

$lead = $this->leadRepository->findOrFail($id);

$stage = $lead->pipeline->stages()
->where('id', request()->input('lead_pipeline_stage_id'))
->firstOrFail();

Event::dispatch('lead.update.before', $id);

$lead = $this->leadRepository->update(
[
'entity_type' => 'leads',
'lead_pipeline_stage_id' => $stage->id,
],
$id,
['lead_pipeline_stage_id']
);

Event::dispatch('lead.update.after', $lead);

return response()->json([
'message' => trans('admin::app.leads.update-success'),
]);
}

/**
* Search person results.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ class="rounded-xl bg-gray-200 px-3 py-1 text-xs font-medium dark:bg-gray-800"
<div class="rounded-xl bg-gray-200 px-3 py-1 text-xs font-medium dark:bg-gray-800 dark:text-white">
@{{ element.formatted_lead_value }}
</div>

<div class="rounded-xl bg-gray-200 px-3 py-1 text-xs font-medium dark:bg-gray-800 dark:text-white">
@{{ element.source.name }}
</div>

<div class="rounded-xl bg-gray-200 px-3 py-1 text-xs font-medium dark:bg-gray-800 dark:text-white">
@{{ element.type.name }}
</div>
Expand Down Expand Up @@ -376,7 +376,7 @@ class="rounded-xl bg-gray-200 px-3 py-1 text-xs font-medium dark:bg-gray-800"
this.stageLeads[stage.id].leads.meta.total = this.stageLeads[stage.id].leads.meta.total + 1;
this.$axios
.put("{{ route('admin.leads.update', 'replace') }}".replace('replace', event.added.element.id), {
.put("{{ route('admin.leads.stage.update', 'replace') }}".replace('replace', event.added.element.id), {
'lead_pipeline_stage_id': stage.id
})
.then(response => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class="icon-edit dark:hover:bg-gray-00 rounded-md p-1 text-2xl transition-all ho
['code', 'NOTIN', ['title', 'description']]
])"
:entity="$lead"
:url="route('admin.leads.update', $lead->id)"
:url="route('admin.leads.attributes.update', $lead->id)"
/>
</form>
</x-admin::form>
</div>

{!! view_render_event('admin.leads.view.attributes.before', ['lead' => $lead]) !!}
{!! view_render_event('admin.leads.view.attributes.before', ['lead' => $lead]) !!}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class="primary-button"
data() {
return {
isUpdating: false,
currentStage: @json($lead->stage),
nextStage: null,
Expand All @@ -162,14 +162,14 @@ class="primary-button"
let params = {
'lead_pipeline_stage_id': this.nextStage.id
};
if (this.nextStage.code == 'won') {
params.lead_value = this.nextStage.lead_value;
params.closed_at = this.nextStage.closed_at;
} else if (this.nextStage.code == 'lost') {
params.lost_reason = this.nextStage.lost_reason;
params.closed_at = this.nextStage.closed_at;
}
Expand All @@ -186,15 +186,16 @@ class="primary-button"
this.isUpdating = true;
let self = this;
this.$axios.put("{{ route('admin.leads.update', $lead->id) }}", params ?? {
'lead_pipeline_stage_id': stage.id
})
this.$axios
.put("{{ route('admin.leads.stage.update', $lead->id) }}", params ?? {
'lead_pipeline_stage_id': stage.id
})
.then (function(response) {
self.isUpdating = false;
self.currentStage = stage;
self.$emitter.emit('add-flash', { type: 'success', message: response.data.message });
})
.catch (function (error) {
Expand All @@ -206,4 +207,4 @@ class="primary-button"
}
});
</script>
@endPushOnce
@endPushOnce
4 changes: 4 additions & 0 deletions packages/Webkul/Admin/src/Routes/Admin/leads-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

Route::put('edit/{id}', 'update')->name('admin.leads.update');

Route::put('attributes/edit/{id}', 'updateAttributes')->name('admin.leads.attributes.update');

Route::put('stage/edit/{id}', 'updateStage')->name('admin.leads.stage.update');

Route::get('search', 'search')->name('admin.leads.search');

Route::delete('{id}', 'destroy')->name('admin.leads.delete');
Expand Down

0 comments on commit 320ea11

Please sign in to comment.