Skip to content

Commit

Permalink
✨ feat: implement non-blocking download into UI
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Oct 5, 2023
1 parent 27defe9 commit 9c2dee3
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 305 deletions.
2 changes: 2 additions & 0 deletions admin/src/modules/alerts/constants/events.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export const UPLOAD_EVENT = "upload";
export const UPLOAD_FINISHED_EVENT = "upload:finished";
export const DOWNLOAD_EVENT = "download";
export const DOWNLOAD_FINISHED_EVENT = "download:finished";
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AlertsService from "../../alerts/services/alerts-service";
import { DOWNLOAD_EVENT, DOWNLOAD_FINISHED_EVENT } from "../../alerts/constants/events";

export default class DownloadAlertsService extends AlertsService {
onDownload(callback) {
this.on(DOWNLOAD_EVENT, callback);
}

onDownloadFinished(callback) {
this.on(DOWNLOAD_FINISHED_EVENT, callback);
}
}
40 changes: 35 additions & 5 deletions admin/src/pages/Download/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import PluginSettingsService from "../../modules/plugin-settings/services/plugin
import { PLUGIN_ROUTES } from "../../modules/@common/utils/redirect-to-plugin-route";
import LanguagesSelector from "../../modules/@common/components/LanguagesSelector";
import ProjectService from "../../modules/@common/services/project-service";
import DownloadAlertsService from "../../modules/localazy-download/services/download-alerts-service";

const downloadAlertsService = new DownloadAlertsService();
downloadAlertsService.subscribe();

function Download(props) {
const { t } = useTranslation();
Expand All @@ -36,7 +40,10 @@ function Download(props) {
const [localesIncompatible, setLocalesIncompatible] = useState(false);
const [showDownloadFinishedModal, setshowDownloadFinishedModal] =
useState(false);
const [downloadResult, setDownloadResult] = useState({});
const [downloadResult, setDownloadResult] = useState({
success: false,
report: [],
});
const [isDownloading, setIsDownloading] = useState(false);

/**
Expand Down Expand Up @@ -72,8 +79,34 @@ function Download(props) {

const onDownloadClick = async () => {
setIsDownloading(true);
setshowDownloadFinishedModal(false);
setDownloadResult({
success: false,
report: [],
});
const result = await LocalazyDownloadService.download();
setDownloadResult(result);
const { streamIdentifier } = result;
downloadAlertsService.setStreamIdentifier(streamIdentifier);
downloadAlertsService.onDownload((data) => {
setDownloadResult((old) => ({
success: data.success,
report: [
...old.report || [],
data.message,
],
}));
});
downloadAlertsService.onDownloadFinished((data) => {
setDownloadResult((old) => ({
success: data.success,
report: [
...old.report || [],
data.message,
],
}));
setIsDownloading(false);
setshowDownloadFinishedModal(true);
});

// track download
ProductAnalyticsService.trackDownloadToStrapi(
Expand All @@ -83,9 +116,6 @@ function Download(props) {
"Target Languages Codes": "all",
}
);

setIsDownloading(false);
setshowDownloadFinishedModal(true);
};

useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions server/constants/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
UPLOAD_EVENT: "upload",
UPLOAD_FINISHED_EVENT: "upload:finished",
DOWNLOAD_EVENT: "download",
DOWNLOAD_FINISHED_EVENT: "download:finished",
};
Loading

0 comments on commit 9c2dee3

Please sign in to comment.