Skip to content

Commit

Permalink
feat: prepare audit select for baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Hacene committed Dec 11, 2024
1 parent 067f0dd commit f73904f
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 5 deletions.
3 changes: 2 additions & 1 deletion frontend/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -986,5 +986,6 @@
"addFearedEvent": "Add feared event",
"addEbiosRMstudy": "Add Ebios RM Study",
"noAuthor": "No author assigned",
"noReviewer": "No reviewer assigned"
"noReviewer": "No reviewer assigned",
"selectAudit": "Select audit"
}
66 changes: 66 additions & 0 deletions frontend/src/lib/components/Modals/UpdateModal.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<script lang="ts">
// Props
/** Exposes parent props to this component. */
export let parent: any;
// Stores
import type { ModelInfo } from '$lib/utils/types';
import type { ModalStore } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
const modalStore: ModalStore = getModalStore();
export let form: SuperValidated<AnyZodObject>;
export let model: ModelInfo;
export let invalidateAll = true; // set to false to keep form data using muliple forms on a page
export let formAction = '?/update';
export let context = 'default';
export let object: Record<string, any> = {};
let closeModal = true;
export let suggestions: { [key: string]: any } = {};
export let selectOptions: Record<string, any> = {};
export let foreignKeys: Record<string, any> = {};
// Base Classes
const cBase = 'card p-4 w-modal shadow-xl space-y-4';
const cHeader = 'text-2xl font-bold';
export let debug = false;
import ModelForm from '$lib/components/Forms/ModelForm.svelte';
import type { SuperValidated } from 'sveltekit-superforms';
import type { AnyZodObject } from 'zod';
</script>

{#if $modalStore[0]}
<div class="modal-example-form {cBase}">
<div class="flex items-center justify-between">
<header class={cHeader} data-testid="modal-title">
{$modalStore[0].title ?? '(title missing)'}
</header>
<div
role="button"
tabindex="0"
class="flex items-center hover:text-primary-500 cursor-pointer"
on:click={parent.onClose}
on:keydown={parent.onClose}
>
<i class="fa-solid fa-xmark" />
</div>
</div>
<ModelForm
{form}
{object}
{suggestions}
{parent}
action={formAction}
{invalidateAll}
{model}
{closeModal}
{context}
caching={true}
selectOptions={selectOptions}
foreignKeys={foreignKeys}
{debug}
/>
</div>
{/if}
10 changes: 7 additions & 3 deletions frontend/src/lib/components/ModelTable/ModelTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,13 @@
{#if pagination && rowsPerPage}
<RowsPerPage {handler} />
{/if}
{#if canCreateObject}
<slot name="addButton" />
{/if}
<div class="flex space-x-2 items-center">
<slot name="optButton" />
{#if canCreateObject}
<slot name="addButton" />
{/if}
</div>

</header>
<!-- Table -->
<table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
const deleteForm = await superValidate(zod(schema));
const URLModel = 'compliance-assessments';
const createSchema = modelSchema(URLModel);
const updateSchema = modelSchema('ebios-rm')
const initialData = {
ebios_rm_studies: [params.id]
};
const updatedModel: ModelInfo = getModelInfo('ebios-rm');
const createForm = await superValidate(initialData, zod(createSchema), { errors: false });
const objectEndpoint = `${BASE_API_URL}/${updatedModel.endpointUrl}/${params.id}/object/`;
const objectResponse = await fetch(objectEndpoint);
const object = await objectResponse.json();
const updateForm = await superValidate(object, zod(updateSchema), { errors: false });
const model: ModelInfo = getModelInfo(URLModel);
const foreignKeyFields = urlParamModelForeignKeyFields(URLModel);
const updateForeignKeyFields = urlParamModelForeignKeyFields('ebios-rm');
const selectFields = model.selectFields;

const foreignKeys: Record<string, any> = {};
Expand All @@ -46,6 +53,25 @@ export const load: PageServerLoad = async ({ params, fetch }) => {

model['foreignKeys'] = foreignKeys;

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

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

updatedModel['foreignKeys'] = updateForeignKeys

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

if (selectFields) {
Expand Down Expand Up @@ -91,7 +117,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
meta: data // metaData
};

return { createForm, deleteForm, model, URLModel, table };
return { createForm, deleteForm, model, URLModel, table, updateForm, updatedModel, object };
};

export const actions: Actions = {
Expand All @@ -106,5 +132,8 @@ export const actions: Actions = {
},
delete: async (event) => {
return defaultDeleteFormAction({ event, urlModel: 'compliance-assessments' });
},
update: async (event) => {
return defaultWriteFormAction({ event, urlModel: 'ebios-rm', action: 'edit' });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte';
import { checkConstraints } from '$lib/utils/crud';
import * as m from '$paraglide/messages.js';
import UpdateModal from '$lib/components/Modals/UpdateModal.svelte';
const modalStore: ModalStore = getModalStore();
Expand Down Expand Up @@ -50,9 +51,58 @@
}
modalStore.trigger(modal);
}
function modalUpdateForm(): void {
let modalComponent: ModalComponent = {
ref: UpdateModal,
props: {
form: data.updateForm,
model: data.updatedModel,
object: data.object,
foreignKeys: data.updatedModel.foreignKeys,
}
};
let modal: ModalSettings = {
type: 'component',
component: modalComponent,
// Data
title: m.selectAudit()
};
if (
checkConstraints(
data.updateForm.constraints,
Object.fromEntries(
Object.entries(data.updatedModel.foreignKeys).filter(([key]) => key !== 'risk_matrix')
)
).length > 0
) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + data.updatedModel.localName).toLowerCase(),
value: checkConstraints(data.updateForm.constraints, data.updatedModel.foreignKeys)
};
}
modalStore.trigger(modal);
}
</script>

<ModelTable source={data.table} deleteForm={data.deleteForm} {URLModel}>
<div slot="optButton">
<span class="inline-flex overflow-hidden rounded-md border bg-white shadow-sm">
<button
class="inline-block border-e p-3 btn-mini-secondary w-12 focus:relative"
data-testid="opt-button"
title={m.selectAudit()}
on:click={modalUpdateForm}
><i class="fa-solid fa-hand-pointer"></i>
</button>
</span>
</div>
<div slot="addButton">
<span class="inline-flex overflow-hidden rounded-md border bg-white shadow-sm">
<button
Expand Down

0 comments on commit f73904f

Please sign in to comment.