Skip to content

Commit

Permalink
✨ feat: extract upload alerts interceptor to a separate service
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Oct 5, 2023
1 parent c18e535 commit b0ce91c
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 27 deletions.
1 change: 1 addition & 0 deletions admin/src/modules/alerts/constants/channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LOCALAZY_PLUGIN_CHANNEL = "localazy-plugin";
2 changes: 2 additions & 0 deletions admin/src/modules/alerts/constants/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const UPLOAD_EVENT = "upload";
export const UPLOAD_FINISHED_EVENT = "upload:finished";
25 changes: 25 additions & 0 deletions admin/src/modules/alerts/services/alerts-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import SocketIoClient from "socket.io-client";
import { LOCALAZY_PLUGIN_CHANNEL } from "../constants/channels";


export default class AlertsService {
_client;

_streamIdentifier;

constructor() {
this._client = SocketIoClient.connect(process.env.STRAPI_ADMIN_BACKEND_URL);
}

setStreamIdentifier(streamIdentifier) {
this._streamIdentifier = streamIdentifier;
}

subscribe(channel = null) {
this._client.emit("subscribe", channel || LOCALAZY_PLUGIN_CHANNEL);
}

on(event, callback) {
this._client.on(`${event}:${this._streamIdentifier}`, callback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AlertsService from "../../alerts/services/alerts-service";
import { UPLOAD_EVENT, UPLOAD_FINISHED_EVENT } from "../../alerts/constants/events";

export default class UploadAlertsService extends AlertsService {
onUpload(callback) {
this.on(UPLOAD_EVENT, callback);
}

onUploadFinished(callback) {
this.on(UPLOAD_FINISHED_EVENT, callback);
}
}
13 changes: 6 additions & 7 deletions admin/src/pages/Upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Button } from "@strapi/design-system/Button";
import UploadIcon from "@strapi/icons/Upload";
import { Box } from "@strapi/design-system/Box";
import { Alert } from "@strapi/design-system/Alert";
import SocketIoClient from "socket.io-client";
import Loader from "../../modules/@common/components/PluginPageLoader";
import LocalazyUploadService from "../../modules/localazy-upload/services/localazy-upload-service";
import areLocalesCompatible from "../../modules/@common/utils/are-locales-compatible";
Expand All @@ -25,10 +24,12 @@ import redirectToPluginRoute, {
import { getLocalazyIdentity } from "../../state/localazy-identity";
import ProductAnalyticsService from "../../modules/@common/services/product-analytics-service";
import PluginSettingsService from "../../modules/plugin-settings/services/plugin-settings-service";
import UploadAlertsService from "../../modules/localazy-upload/services/upload-alerts-service";

import "../../i18n";

const socket = SocketIoClient.connect(process.env.STRAPI_ADMIN_BACKEND_URL);
const uploadAlertsService = new UploadAlertsService();
uploadAlertsService.subscribe();

function Upload(props) {
const { t } = useTranslation();
Expand Down Expand Up @@ -64,10 +65,8 @@ function Upload(props) {
});
const result = await LocalazyUploadService.upload();
const { streamIdentifier } = result;

// TODO: refactor to external service
socket.emit("subscribe", `localazy-plugin`);
socket.on(`upload:${streamIdentifier}`, (data) => {
uploadAlertsService.setStreamIdentifier(streamIdentifier);
uploadAlertsService.onUpload((data) => {
setUploadResult((old) => ({
success: data.success,
report: [
Expand All @@ -76,7 +75,7 @@ function Upload(props) {
],
}));
});
socket.on(`upload:finish:${streamIdentifier}`, (data) => {
uploadAlertsService.onUploadFinished((data) => {
setUploadResult((old) => ({
success: data.success,
report: [
Expand Down
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions server/constants/channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
LOCALAZY_PLUGIN_CHANNEL: "localazy-plugin",
};
4 changes: 4 additions & 0 deletions server/constants/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
UPLOAD_EVENT: "upload",
UPLOAD_FINISHED_EVENT: "upload:finished",
};
16 changes: 9 additions & 7 deletions server/controllers/localazy-transfer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const omitDeep = require("../utils/omit-deep");
const RequestInitiatorHelper = require('../utils/request-initiator-helper');
const PluginSettingsServiceHelper = require('../services/helpers/plugin-settings-service-helper');
const generateRandomId = require('../utils/generate-random-id');
const { LOCALAZY_PLUGIN_CHANNEL } = require('../constants/channels');
const { UPLOAD_EVENT, UPLOAD_FINISHED_EVENT } = require('../constants/events');

const getFilteredLanguagesCodesForDownload = async (languagesCodes) => {
const pluginSettingsServiceHelper = new PluginSettingsServiceHelper(strapi);
Expand Down Expand Up @@ -51,7 +53,7 @@ module.exports = {
const streamIdentifier = generateRandomId();

const func = async () => {
strapi.StrapIO.emitRaw('localazy-plugin', `upload:${streamIdentifier}`, {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: 'Upload started',
});

Expand Down Expand Up @@ -81,7 +83,7 @@ module.exports = {
if (!contentTransferSetup.has_setup) {
const message = "Content transfer setup is not set up.";
success = false;
strapi.StrapIO.emitRaw('localazy-plugin', `upload:finish:${streamIdentifier}`, {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message,
});
Expand Down Expand Up @@ -115,7 +117,7 @@ module.exports = {

if (!isCollectionTransferEnabled(setup, collectionName)) {
const message = `Collection ${collectionName} transfer is disabled.`;
strapi.StrapIO.emitRaw('localazy-plugin', `upload:${streamIdentifier}`, {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
strapi.log.info(message);
Expand All @@ -127,7 +129,7 @@ module.exports = {
const pickPaths = getPickPathsWithComponents(currentTransferSetupModel);
if (!pickPaths.length) {
const message = `No fields for collection ${collectionName} transfer are enabled.`;
strapi.StrapIO.emitRaw('localazy-plugin', `upload:${streamIdentifier}`, {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
strapi.log.warn(message);
Expand Down Expand Up @@ -193,14 +195,14 @@ module.exports = {
// Use `deprecate: "file"` if there is one chunk of transferred data only!
const hasMoreTransferFilesChunks = importFile.length > 1;
const uploadConfig = !hasMoreTransferFilesChunks ? { deprecate: "file" } : {};
strapi.StrapIO.emitRaw('localazy-plugin', `upload:${streamIdentifier}`, {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: "Uploading collections to Localazy...",
});
await LocalazyUploadService.upload(
importFile,
uploadConfig
);
strapi.StrapIO.emitRaw('localazy-plugin', `upload:finish:${streamIdentifier}`, {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message: "Upload finished",
});
Expand All @@ -217,7 +219,7 @@ module.exports = {

} catch (e) {
strapi.log.error(e.message);
strapi.StrapIO.emitRaw('localazy-plugin', `upload:finish:${streamIdentifier}`, {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message: e.message,
});
Expand Down

0 comments on commit b0ce91c

Please sign in to comment.