Skip to content

Commit

Permalink
Validate actions
Browse files Browse the repository at this point in the history
  • Loading branch information
p3rcypj committed Apr 23, 2024
1 parent db8f0ce commit 59a59a4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/data/repositories/InstanceDhisRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async function transformFile(blob: Blob, mime: string): Promise<Blob> {
});
} else if (process.env.NODE_ENV === "development" && mime === "image/gif") {
try {
const ffmpeg = createFFmpeg({ corePath: "https://unpkg.com/@ffmpeg/core/dist/ffmpeg-core.js" });
const ffmpeg = createFFmpeg({ corePath: "https://unpkg.com/@ffmpeg/core@0.12.6" });

await ffmpeg.load();
ffmpeg.FS("writeFile", "file.gif", await fetchFile(blob));
Expand All @@ -151,6 +151,7 @@ async function transformFile(blob: Blob, mime: string): Promise<Blob> {
const data = ffmpeg.FS("readFile", "file.mp4");
return new Blob([data.buffer], { type: "video/mp4" });
} catch (error: any) {
console.error(error);
return blob;
}
}
Expand Down
24 changes: 10 additions & 14 deletions src/domain/usecases/ImportActionsUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ export class ImportActionsUseCase implements UseCase {

public async execute(files: File[]): Promise<PersistedAction[]> {
const items = await this.importExportClient.import<PersistedAction>(files);
if (
items.every(async action => {
if (action.type !== "page") return true;
const landing = await this.landingRepository.getById(action.launchPageId);
if (!landing) return false;
if (landing.actions.some(actionId => actionId === action.id)) return false;
return true;
})
)
return this.actionRepository.save(items);
else
throw Error(
i18n.t("Unable to import actions. Some landing page action is referencing the landing itself.")
);
const nodes = await this.landingRepository.getAll();
const valid = items.every(action => {
if (action.type !== "page") return true;
const landing = nodes.find(node => node.id === action.launchPageId);
if (!landing) return false;
return !landing.actions.some(actionId => actionId === action.id);
});

if (valid) return this.actionRepository.save(items);
else return Promise.reject(i18n.t("Unable to import actions. Some action is referencing an invalid page."));
}
}
4 changes: 2 additions & 2 deletions src/domain/usecases/UpdateActionUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export class UpdateActionUseCase implements UseCase {
public async execute(action: PartialAction): Promise<void> {
if (action.type !== "page") return this.actionRepository.update(action);
const landing = await this.landingRepository.getById(action.launchPageId);
if (!landing) throw Error(i18n.t("Landing page not found"));
if (!landing) return Promise.reject(i18n.t("Landing page not found"));
if (landing.actions.some(actionId => actionId === action.id))
throw Error(i18n.t("Landing page cannot have an action to launch the landing page itself"));
return Promise.reject(i18n.t("Landing page cannot have an action to launch the landing page itself"));
else return this.actionRepository.update(action);
}
}
6 changes: 4 additions & 2 deletions src/webapp/components/action-list-table/ActionListTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export const ActionListTable: React.FC<ActionListTableProps> = props => {
} else {
loading.show(true, i18n.t("Importing action(s)"));
try {
const actions = await compositionRoot.actions.import(files);
snackbar.success(i18n.t("Imported {{n}} actions", { n: actions.length }));
await compositionRoot.actions
.import(files)
.then(actions => snackbar.success(i18n.t("Imported {{n}} actions", { n: actions.length })))
.catch(snackbar.error);
await refreshRows();
} catch (err: any) {
snackbar.error((err && err.message) || err.toString());
Expand Down
7 changes: 4 additions & 3 deletions src/webapp/pages/action-detail/ActionDetailPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfirmationDialog, ConfirmationDialogProps } from "@eyeseetea/d2-ui-components";
import { ConfirmationDialog, ConfirmationDialogProps, useSnackbar } from "@eyeseetea/d2-ui-components";
import _ from "lodash";
import React, { useCallback, useEffect, useState } from "react";
import styled from "styled-components";
Expand Down Expand Up @@ -30,6 +30,7 @@ export const ActionDetailPage: React.FC<ActionDetailPageProps> = ({ mode }) => {
const { compositionRoot, reload } = useAppContext();
const { id } = useParams();
const navigate = useNavigate();
const snackbar = useSnackbar();

const [stateAction, updateStateAction] = useState<PartialAction>(defaultAction);

Expand All @@ -51,9 +52,9 @@ export const ActionDetailPage: React.FC<ActionDetailPageProps> = ({ mode }) => {
}, [navigate]);

const saveAction = useCallback(async () => {
await compositionRoot.actions.update({ ...stateAction, id: _.kebabCase(stateAction.id) });
await compositionRoot.actions.update({ ...stateAction, id: _.kebabCase(stateAction.id) }).catch(snackbar.error);
await reload();
}, [stateAction, compositionRoot, reload]);
}, [stateAction, compositionRoot, reload, snackbar]);

const onChange = useCallback((update: Parameters<typeof updateStateAction>[0]) => {
updateStateAction(update);
Expand Down

0 comments on commit 59a59a4

Please sign in to comment.