Skip to content

Commit

Permalink
Ability to get all alerts of a customer to check if customer is subsc…
Browse files Browse the repository at this point in the history
…ribed (#3)
  • Loading branch information
BobWez98 authored Aug 23, 2022
1 parent fbf7987 commit fda5548
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
yarn.lock
/node_modules
/vendor
composer.lock
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ You need to have the [Magento 2 Product Alert GraphQl module](https://github.com
```
composer require rapidez/product-alert
```
Add the scripts file to your `app.js`:
```
require('Vendor/rapidez/product-alert/resources/js/alerts')
```
And include the blade file where needed (`addtocart.blade.php`):
```
@include('rapidez-product-alert::subscribe-form', ['product_id' => $product->id])
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@babel/runtime": "^7.14.0"
}
}
14 changes: 14 additions & 0 deletions resources/js/alerts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
document.addEventListener('turbolinks:load', () => {
window.app.$on('logged-in', getAlerts);
window.app.$on('alerts-updated', getAlerts);
window.app.$on('logout', () => localStorage.removeItem('alerts'));

async function getAlerts() {
if (!localStorage.token) {
return;
}

window.axios.defaults.headers.common['Authorization'] = `Bearer ${localStorage.token}`;
await window.axios.post('/api/product-alerts').then((res) => localStorage.alerts = res.data)
}
});
4 changes: 3 additions & 1 deletion resources/views/subscribe-form.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<graphql-mutation v-if="user" v-cloak query="@include('rapidez-product-alert::graphql.product-alert-subscribe')" :variables='{product_id: {{ $product_id }}, email: user.email}' :notify="{message: '@lang('We will let you know once this product is back in stock')', type: 'success'}">
<graphql-mutation v-if="user" v-cloak query="@include('rapidez-product-alert::graphql.product-alert-subscribe')" :variables='{product_id: {{ $product_id }}, email: user.email}' :callback="() => {
window.app.$emit('alerts-updated')
}" :notify="{message: '@lang('We will let you know once this product is back in stock')', type: 'success'}">
<div slot-scope="{ mutate }">
<x-rapidez::button v-on:click="mutate" class="flex items-center" dusk="product-alert-subscribe">
<span class="mr-2">@lang('Notify me when this product is in stock')</span><x-heroicon-o-bell class="h-5 w-5 ml-auto"/>
Expand Down
6 changes: 6 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
use Rapidez\ProductAlert\Http\Controllers\AlertController;

Route::prefix('api')->group(function() {
Route::post('product-alerts', AlertController::class);
});
20 changes: 20 additions & 0 deletions src/Http/Controllers/AlertController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rapidez\ProductAlert\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Routing\Controller;

class AlertController extends Controller
{
public function __invoke(Request $request)
{
abort_if(!$request->header('authorization'), 401);
$customer = DB::table('oauth_token')->select('customer_id')->where('token', str_replace('Bearer ', '', $request->header('authorization')))->first();
abort_if(!$customer->customer_id, 401);
$alerts = DB::table('product_alert_stock')->select('product_id')->where('customer_id', $customer->customer_id)->get();

return $alerts->pluck('product_id')->toJson() ?? [];
}
}
3 changes: 2 additions & 1 deletion src/ProductAlertServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'rapidez-product-alert');


$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/rapidez-product-alert'),
], 'views');

$this->loadRoutesFrom(__DIR__.'/../routes/api.php');
}
}

0 comments on commit fda5548

Please sign in to comment.