Skip to content

Commit

Permalink
fix: risk assessment modal in ebios rm
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Hacene committed Dec 19, 2024
1 parent 185312d commit 32d4b8d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { defaultWriteFormAction } from '$lib/utils/actions';
import { BASE_API_URL } from '$lib/utils/constants';
import { getModelInfo } from '$lib/utils/crud';
import {
getModelInfo,
urlParamModelForeignKeyFields,
urlParamModelSelectFields
} from '$lib/utils/crud';
import { modelSchema } from '$lib/utils/schemas';
import type { ModelInfo } from '$lib/utils/types';
import { type Actions } from '@sveltejs/kit';
Expand Down Expand Up @@ -28,8 +32,51 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
const createRiskAnalysisForm = await superValidate(initialData, zod(createSchema), {
errors: false
});
const riskModel = getModelInfo('risk-assessments');
const foreignKeyFields = urlParamModelForeignKeyFields(riskModel.urlModel);
const selectFields = urlParamModelSelectFields(riskModel.urlModel);

return { createRiskAnalysisForm, model: getModelInfo('risk-assessments') };
const foreignKeys: Record<string, any> = {};

for (const keyField of foreignKeyFields) {
const queryParams = keyField.urlParams ? `?${keyField.urlParams}` : '';
const keyModel = getModelInfo(keyField.urlModel);
const url = keyModel.endpointUrl
? `${BASE_API_URL}/${keyModel.endpointUrl}/${queryParams}`
: `${BASE_API_URL}/${keyField.urlModel}/${queryParams}`;
const response = await fetch(url);
if (response.ok) {
foreignKeys[keyField.field] = await response.json().then((data) => data.results);
} else {
console.error(`Failed to fetch data for ${keyField.field}: ${response.statusText}`);
}
}

riskModel['foreignKeys'] = foreignKeys;

const selectOptions: Record<string, any> = {};

for (const selectField of selectFields) {
if (selectField.detail) continue;
const url = riskModel.endpointUrl
? `${BASE_API_URL}/${riskModel.endpointUrl}/${selectField.field}/`
: `${BASE_API_URL}/${riskModel.urlModel}/${selectField.field}/`;
const response = await fetch(url);
if (response.ok) {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
console.error(`Failed to fetch data for ${selectField.field}: ${response.statusText}`);
}
}

riskModel['selectOptions'] = selectOptions;

return { createRiskAnalysisForm, riskModel };
};

export const actions: Actions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,18 @@
ref: CreateModal,
props: {
form: data.createRiskAnalysisForm,
model: data.model
model: data.riskModel
}
};
let modal: ModalSettings = {
type: 'component',
component: modalComponent,
// Data
title: safeTranslate('add-' + data.model.localName)
title: safeTranslate('add-' + data.riskModel.localName)
};
if (
checkConstraints(data.createRiskAnalysisForm.constraints, data.model.foreignKeys).length > 0
checkConstraints(data.createRiskAnalysisForm.constraints, data.riskModel.foreignKeys).length >
0
) {
modalComponent = {
ref: MissingConstraintsModal
Expand All @@ -142,8 +143,8 @@
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + data.model.localName).toLowerCase(),
value: checkConstraints(data.createRiskAnalysisForm.constraints, data.model.foreignKeys)
body: safeTranslate('add-' + data.riskModel.localName).toLowerCase(),
value: checkConstraints(data.createRiskAnalysisForm.constraints, data.riskModel.foreignKeys)
};
}
modalStore.trigger(modal);
Expand Down

0 comments on commit 32d4b8d

Please sign in to comment.