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

refactor: Replace Plugin class with PluginBaseClass in JavaScript guides #1569

Merged
merged 2 commits into from
Nov 19, 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
2 changes: 2 additions & 0 deletions .wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1742,3 +1742,5 @@ youtube
zlib
zsh
zstd
zipignore
XMLHttpRequest
32 changes: 21 additions & 11 deletions guides/plugins/plugins/framework/store-api/add-store-api-route.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,18 +379,28 @@

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
import HttpClient from 'src/service/http-client.service';

export default class ExamplePlugin extends Plugin {
init() {
this._client = new HttpClient();
}

requestCustomRoute() {
this._client.post('/example', { limit: 10, offset: 0}, (response) => {
alert(response);
const { PluginBaseClass } = window;

export default class ExamplePlugin extends PluginBaseClass {
async requestCustomRoute() {
const response = await fetch('/example', {

Check warning on line 386 in guides/plugins/plugins/framework/store-api/add-store-api-route.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/framework/store-api/add-store-api-route.md#L386

Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/plugins/plugins/framework/store-api/add-store-api-route.md:386:46: Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
limit: 10,
offset: 0,
}),
});

if (!response.ok) {
throw new Error('Request failed');
}

const data = await response.json();

console.log(data);
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Once we have installed all the dependencies and registered the package in the bu

```javascript
// <plugin root>/src/Resources/app/storefront/src/example.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;

// Import logger
import { log } from 'missionlog';
Expand All @@ -68,7 +68,7 @@ log.init({ initializer: 'INFO' }, (level, tag, msg, params) => {
});

// The plugin skeleton
export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
console.log('init');

Expand Down
12 changes: 6 additions & 6 deletions guides/plugins/plugins/storefront/add-custom-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Inside this file create and export an ExamplePlugin class that extends the base

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
}
```

Expand All @@ -41,9 +41,9 @@ Each plugin has to implement the `init()` method. This method will be called whe

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
window.addEventListener('scroll', this.onScroll.bind(this));
}
Expand Down Expand Up @@ -146,9 +146,9 @@ You can configure your plugins from inside the templates via data-options. First

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
static options = {
/**
* Specifies the text that is prompted to the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import HttpClient from 'src/service/http-client.service';
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;

export default class AjaxPlugin extends Plugin {
export default class AjaxLoadPlugin extends PluginBaseClass {
init() {
// initialize the HttpClient
this._client = new HttpClient();

// get references to the dom elements
this.button = this.el.children['ajax-button'];
this.textdiv = this.el.children['ajax-display'];

// register the events
this._registerEvents();
}

Expand All @@ -108,18 +102,22 @@
this.button.onclick = this._fetch.bind(this);
}

_fetch() {
// make the network request and call the `_setContent` function as a callback
this._client.get('/example', this._setContent.bind(this), 'application/json', true)
}

_setContent(data) {
// parse the response and set the `textdiv.innerHTML` to the timestamp
this.textdiv.innerHTML = JSON.parse(data).timestamp;
async _fetch() {
const response = await fetch('/example');

Check warning on line 106 in guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md#L106

Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md:106:46: Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION
const data = await response.json();
this.textdiv.innerHTML = data.timestamp;
}
}
```

and register it in the `main.js`

```javascript
import AjaxLoadPlugin from './example-plugin/example-plugin.plugin';

window.PluginManager.register('AjaxLoadPlugin', AjaxLoadPlugin, '[data-ajax-helper]');

Check warning on line 118 in guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md#L118

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` PluginManager` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md:118:7: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` PluginManager`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
```

## Adding the Template

The only thing that is now left, is to provide a template for the Storefront plugin to hook into:
Expand Down
36 changes: 8 additions & 28 deletions guides/plugins/plugins/storefront/fetching-data-with-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,26 @@

## Fetching data

At first, we need to import the `HttpClient` to use it in our JavaScript plugin. We also create a new instance of the `HttpClient` and assigned it to a variable in our `ExamplePlugin`.
We will use the standard [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to gather additional data. The fetch API is a modern replacement for the old `XMLHttpRequest` object. It is a promise-based API that allows you to make network requests similar to XMLHttpRequest (XHR).

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
import HttpClient from 'src/service/http-client.service';
const { PluginBaseClass } = window;

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
this._client = new HttpClient();
}
}
```

To fetch data from the API, we now can use the `get` method of the `HttpClient` to invoke a get request.

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
import HttpClient from 'src/service/http-client.service';

export default class ExamplePlugin extends Plugin {
init() {
this._client = new HttpClient();

this.fetchData();
}

// ...

fetchData() {
this._client.get('/widgets/checkout/info', this.handleData);
}
async fetchData() {
const response = await fetch('/widgets/checkout/info');

Check warning on line 36 in guides/plugins/plugins/storefront/fetching-data-with-javascript.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/storefront/fetching-data-with-javascript.md#L36

Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/plugins/plugins/storefront/fetching-data-with-javascript.md:36:60: Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION
const data = await response.text();

handleData(response) {
console.log(response);
console.log(data);
}
}
```

The `get` method takes three arguments. The first one is the `url` which we want to call. In the example we are going to fetch a widget which contains some HTML. The second parameter is a `callback` function. It will be invoked when the API call was done. In the example below we pass in the `handleData` method of our plugin. The callback function then receives the `response` of the API call. We can now use this in our plugin to display the widget in the DOM, for example.

The third parameter of the `get` method is the `contentType` which will be sent in the request header of the API call. It is optional and by default set to be `application/json`.
In this example, we fetch the data from the `/widgets/checkout/info` endpoint. The `fetch` method returns a promise that resolves to the `Response` object representing the response to the request. We then use the `text` method of the `Response` object to get the response body as text.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ As already mentioned, this guide will not explain how to create a JavaScript plu

```javascript
// <plugin root>/src/Resources/app/storefront/src/events-plugin/events-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;

export default class EventsPlugin extends Plugin {
export default class EventsPlugin extends PluginBaseClass {
init() {
}
}
Expand All @@ -43,9 +43,9 @@ Now that you possibly found your event, it's time to register to it and execute

```javascript
// <plugin root>/src/Resources/app/storefront/src/events-plugin/events-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;

export default class EventsPlugin extends Plugin {
export default class EventsPlugin extends PluginBaseClass {
init() {
const plugin = window.PluginManager.getPluginInstanceFromElement(document.querySelector('[data-cookie-permission]'), 'CookiePermission');
plugin.$emitter.subscribe('hideCookieBar', this.onHideCookieBar);
Expand Down
26 changes: 13 additions & 13 deletions guides/plugins/plugins/storefront/using-a-modal-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ Now let's get started with the modal window. First we have to import the `Pseudo

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;
import PseudoModalUtil from 'src/utility/modal-extension/pseudo-modal.util';

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
// ...
}
Expand All @@ -133,10 +133,10 @@ We also call the method `open()` to make it visible.

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;
import PseudoModalUtil from 'src/utility/modal-extension/pseudo-modal.util';

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
this.openModal();
}
Expand Down Expand Up @@ -177,10 +177,10 @@ Of course, the content can also be generated via an API and inserted via AJAX re

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;
import PseudoModalUtil from 'src/utility/modal-extension/pseudo-modal.util';

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
// declaring some basic content
const content = `
Expand Down Expand Up @@ -213,10 +213,10 @@ The `open()` method of the `PseudoModalUtil` class supports a callback function

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;
import PseudoModalUtil from 'src/utility/modal-extension/pseudo-modal.util';

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
// declaring some basic content
const content = `
Expand Down Expand Up @@ -250,10 +250,10 @@ Here is an example how to use it:

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;
import PseudoModalUtil from 'src/utility/modal-extension/pseudo-modal.util';

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
// declaring some basic content
const content = `
Expand Down Expand Up @@ -302,10 +302,10 @@ The constructor method of `PseudoModalUtil` provides optional configuration. If

```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
import Plugin from 'src/plugin-system/plugin.class';
const { PluginBaseClass } = window;
import PseudoModalUtil from 'src/utility/modal-extension/pseudo-modal.util';

export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
// declaring some basic content
const content = `
Expand Down Expand Up @@ -342,7 +342,7 @@ Here is an example which shows how to override the CSS class names.
```javascript
// <plugin root>/src/Resources/app/storefront/src/example-plugin/example-plugin.plugin.js
// ...
export default class ExamplePlugin extends Plugin {
export default class ExamplePlugin extends PluginBaseClass {
init() {
// ...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ Here are some examples of not allowed folders and files:
* .tar
* .tar.gz
* .zip
* .zipognore
* .zipignore
* composer.lock
* package.json
* package-lock.json
Expand Down
Loading