Fixed dynamic operator loading not finishing for some operators.

This commit is contained in:
Felix Kaspar 2024-05-16 19:25:54 +02:00
parent efd4bdc493
commit b1bb8e0f02
2 changed files with 17 additions and 14 deletions

View File

@ -15,7 +15,7 @@ function Dynamic() {
const operators = listOperatorNames(); const operators = listOperatorNames();
const activeOperator = useRef<typeof Operator>(); const activeOperator = useRef<typeof Operator>();
function selectionChanged(s: BaseSyntheticEvent) { async function selectionChanged(s: BaseSyntheticEvent) {
const selectedValue = s.target.value; const selectedValue = s.target.value;
console.log("Selection changed to", selectedValue); console.log("Selection changed to", selectedValue);
if(selectedValue == "none") { if(selectedValue == "none") {
@ -23,20 +23,23 @@ function Dynamic() {
return; return;
} }
i18next.loadNamespaces(selectedValue, (err, t) => { console.log("Loading namespaces for", selectedValue);
await i18next.loadNamespaces(selectedValue, (err, t) => {
if (err) throw err; if (err) throw err;
});
console.log("Loading namespaces done");
const LoadingModule = import(`@stirling-pdf/shared-operations/src/functions/${selectedValue}`) as Promise<{ [key: string]: typeof Operator }>; console.log("Loading modules for", selectedValue);
LoadingModule.then((Module) => { const LoadingModule = import(`@stirling-pdf/shared-operations/src/functions/${selectedValue}`) as Promise<{ [key: string]: typeof Operator }>;
const Operator = Module[capitalizeFirstLetter(selectedValue)]; LoadingModule.then((Module) => {
const description = Operator.schema.describe(); const Operator = Module[capitalizeFirstLetter(selectedValue)];
console.log(Operator.schema); const description = Operator.schema.describe();
console.log(description); console.log(Operator.schema);
console.log(description);
activeOperator.current = Operator; activeOperator.current = Operator;
// This will update children // This will update children
setSchemaDescription(description); setSchemaDescription(description);
});
}); });
} }
@ -75,7 +78,7 @@ function Dynamic() {
const validationResults = activeOperator.current.schema.validate({input: inputs, values: action.values}); const validationResults = activeOperator.current.schema.validate({input: inputs, values: action.values});
if(validationResults.error) { if(validationResults.error) {
console.log({error: "Validation failed", details: validationResults.error.message}); console.error({error: "Validation failed", details: validationResults.error.message}, validationResults.error.stack);
} }
else { else {
action.values = validationResults.value.values; action.values = validationResults.value.values;

View File

@ -8,7 +8,7 @@ export const JoiPDFFileSchema = Joi.custom((value: Express.Multer.File[] /* <- a
else { // File(s) else { // File(s)
const firstWrongFile = value.find(f => f.mimetype != "application/pdf") const firstWrongFile = value.find(f => f.mimetype != "application/pdf")
if(firstWrongFile) if(firstWrongFile)
throw new Error(`at least one of the files provided doesn't seem to be a PDF. Got ${firstWrongFile.mimetype} instead.`); throw new Error(`at least one of the files provided doesn't seem to be a PDF. Got the file ${JSON.stringify(firstWrongFile)} instead.`);
return PdfFile.fromMulterFiles(value); return PdfFile.fromMulterFiles(value);
} }