Skip to content
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
53 changes: 42 additions & 11 deletions src/app/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
useWarningStore,
useYamlStore,
} from "../lib/store";
import { getYaml } from "../lib/utils";
import { getYaml, collectRemovedKeys } from "../lib/utils";
import linter from "../linter";
import publicCodeAdapter from "../publiccode-adapter";
import { toSemVerObject } from "../semver";
import { validator } from "../validator";
Expand All @@ -62,11 +63,11 @@ import { yamlLoadEventBus } from "./UploadPanel";

const validatorFn = async (values: PublicCode) => {
try {
const yaml = getYaml(values) ?? "";
const results = await validator({
publiccode: JSON.stringify(values),
publiccode: yaml,
baseURL: values.url,
});

return results;
} catch {
console.log("error validating");
Expand Down Expand Up @@ -348,11 +349,42 @@ export default function Editor() {
}
};

const processImported = async (raw: PublicCode) => {
try {
try { getValues(); } catch {}
const adapted = publicCodeAdapter({
publicCode: raw as PublicCode,
defaultValues: defaultValues as unknown as Partial<PublicCode>,
});
const sanitized = linter(adapted);
const removed = collectRemovedKeys(raw, sanitized);
if (removed.length > 0) {
const body = (
<List className="it-list">
{removed.map((k) => (
<ListItem key={k}>
<span className="text">{k}</span>
</ListItem>
))}
</List>
);
notify(
t("editor.form.validate.info.title"),
body,
{ state: "info", dismissable: true }
);
}
await setFormDataAfterImport(async () => adapted as PublicCode);
} catch (e) {
// fall back to standard flow on any error
await setFormDataAfterImport(async () => raw as PublicCode);
}
};

const loadFileYamlHandler = async (file: File) => {
resetFormHandler();
const fetchDataFn = () => fileImporter(file);

await setFormDataAfterImport(fetchDataFn);
const raw = await fileImporter(file);
await processImported(raw as PublicCode);
};

const loadRemoteYamlHandler = async (event: {
Expand All @@ -362,12 +394,11 @@ export default function Editor() {
resetFormHandler();
try {
console.log("loadRemoteYamlHandler", event);
const fetchDataFn =
const raw =
event.source === "gitlab"
? async () => await importFromGitlab(new URL(event.url))
: async () => await importStandard(new URL(event.url));

await setFormDataAfterImport(fetchDataFn);
? await importFromGitlab(new URL(event.url))
: await importStandard(new URL(event.url));
await processImported(raw as PublicCode);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
notify(t("editor.notvalidurl"), t("editor.notvalidurl"), {
Expand Down
28 changes: 28 additions & 0 deletions src/app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,32 @@ export function useIsMobile(breakpoint = 640) {
}, [breakpoint]);

return isMobile;
}

export function collectRemovedKeys(original: unknown, sanitized: unknown, prefix = ""): Array<string> {
const removed: Array<string> = [];

if (original && typeof original === "object" && !Array.isArray(original)) {
const originalObj = original as Record<string, unknown>;
const sanitizedObj = (sanitized && typeof sanitized === "object" && !Array.isArray(sanitized))
? (sanitized as Record<string, unknown>)
: {};

for (const key of Object.keys(originalObj)) {
const nextPrefix = prefix ? `${prefix}.${key}` : key;
if (!(key in sanitizedObj) || (sanitizedObj as Record<string, unknown>)[key] === undefined) {
removed.push(nextPrefix);
} else {
removed.push(
...collectRemovedKeys(
(originalObj as Record<string, unknown>)[key],
(sanitizedObj as Record<string, unknown>)[key],
nextPrefix
)
);
}
}
}

return removed;
}
3 changes: 3 additions & 0 deletions src/i18n/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"validate": {
"notification_title": "Validierungsfehler",
"notification_text": "Überprüfen Sie die Felder des Formulars",
"info": {
"title": "Unbekannte Felder entfernt"
},
"button": "Validieren"
},
"generate": "Generieren",
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
"title": "Validation errors",
"text": "Check form fields"
},
"info": {
"title": "Unknown fields removed"
},
"button": "Validate"
},
"generate": "Generate",
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
"title": "Validation errors",
"text": "Check form fields"
},
"info": {
"title": "Champs inconnus supprimés"
},
"button": "Validate"
},
"generate": "Generate",
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
"title": "Errori di validazione",
"text": "Controlla i campi della form"
},
"info": {
"title": "Campi sconosciuti rimossi"
},
"button": "Valida"
},
"generate": "Genera",
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/locales/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
"title": "Validatiefouten",
"text": "Controleer formuliervelden"
},
"info": {
"title": "Onbekende velden verwijderd"
},
"button": "Valideren"
},
"generate": "Genereren",
Expand Down
Loading