Skip to content

Commit

Permalink
Add valueType parameter to SelectField interface
Browse files Browse the repository at this point in the history
This allows to parse the value of a select option if it is a number
  • Loading branch information
nas-tabchiche committed Dec 5, 2024
1 parent 981bb34 commit 4db984a
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 20 deletions.
4 changes: 3 additions & 1 deletion frontend/src/lib/utils/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ interface Field {
interface SelectField {
field: string;
detail?: boolean;
valueType?: 'string' | 'number';
}

export interface ModelMapEntry {
Expand Down Expand Up @@ -620,7 +621,8 @@ export const URL_MODEL_MAP: ModelMap = {
{ field: 'ebios_rm_study', urlModel: 'ebios-rm' },
{ field: 'assets', urlModel: 'assets' },
{ field: 'qualifications', urlModel: 'qualifications' }
]
],
selectFields: [{ field: 'gravity', valueType: 'number', detail: true }]
}
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/utils/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const loadDetail = async ({ event, model, id }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ export const load: LayoutServerLoad = async (event) => {

if (selectFields) {
for (const selectField of selectFields) {
const url = `${BASE_API_URL}/${event.params.model}/${
const url = `${BASE_API_URL}/${model.endpointUrl ?? event.params.model}/${
selectField.detail ? event.params.id + '/' : ''
}${selectField.field}/`;
const response = await event.fetch(url);
if (response.ok) {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const load: PageServerLoad = async (event) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const load: LayoutServerLoad = async ({ fetch, params }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const load: LayoutServerLoad = async ({ fetch, params }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down Expand Up @@ -152,7 +152,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
measureSelectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/routes/(app)/(internal)/settings/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const load: PageServerLoad = async ({ fetch }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand All @@ -47,7 +47,7 @@ export const load: PageServerLoad = async ({ fetch }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const load: LayoutServerLoad = async (event) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const load = (async ({ fetch, params }) => {
selectOptions[selectField.field] = await response.json().then((data) =>
Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}))
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const load = (async ({ fetch, params }) => {
if (data) {
selectOptions[selectField.field] = Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}));
}
})
Expand All @@ -98,7 +98,7 @@ export const load = (async ({ fetch, params }) => {
if (data) {
measureSelectOptions[selectField.field] = Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}));
} else {
console.error(`Failed to fetch data for ${selectField.field}: ${response.statusText}`);
Expand Down Expand Up @@ -169,7 +169,7 @@ export const load = (async ({ fetch, params }) => {
if (data) {
evidenceSelectOptions[selectField.field] = Object.entries(data).map(([key, value]) => ({
label: value,
value: key
value: selectField.valueType === 'number' ? parseInt(key) : key
}));
}
})
Expand Down

0 comments on commit 4db984a

Please sign in to comment.