-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from jameel-institute/jidea-57-non-country-inp…
…uts-front-end Convert parameter metadata into a front-end form
- Loading branch information
Showing
28 changed files
with
1,973 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
import { | ||
cilArrowRight, | ||
cilArrowThickToLeft, | ||
cilBookmark, | ||
cilBug, | ||
cilChevronLeft, | ||
cilCloudDownload, | ||
cilGlobeAlt, | ||
cilHistory, | ||
cilIndustry, | ||
cilLink, | ||
cilMenu, | ||
cilNoteAdd, | ||
cilPlus, | ||
cilShareAlt, | ||
cilShieldAlt, | ||
} from "@coreui/icons"; | ||
|
||
export const iconsSet = { | ||
cilMenu, | ||
cilCloudDownload, | ||
cilPlus, | ||
cilArrowRight, | ||
cilArrowThickToLeft, | ||
cilBookmark, | ||
cilBug, | ||
cilChevronLeft, | ||
cilCloudDownload, | ||
cilGlobeAlt, | ||
cilHistory, | ||
cilShareAlt, | ||
cilIndustry, | ||
cilLink, | ||
cilMenu, | ||
cilNoteAdd, | ||
cilGlobeAlt, | ||
cilArrowThickToLeft, | ||
cilChevronLeft, | ||
cilPlus, | ||
cilShareAlt, | ||
cilShieldAlt, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<template> | ||
<div> | ||
<CForm | ||
v-if="props.metadata && formData" | ||
class="inputs" | ||
:data-test="JSON.stringify(formData)" | ||
@submit.prevent="submitForm" | ||
> | ||
<div | ||
v-for="(parameter) in props.metadata.parameters" | ||
:key="parameter.id" | ||
class="field-container" | ||
> | ||
<CCol v-if="renderAsRadios(parameter)" class="button-group-container"> | ||
<CRow> | ||
<ParameterIcon :parameter="parameter" /> | ||
<CFormLabel :for="parameter.id"> | ||
{{ parameter.label }} | ||
</CFormLabel> | ||
</CRow> | ||
<CRow> | ||
<CButtonGroup | ||
role="group" | ||
:aria-label="parameter.label" | ||
:size="largeScreen ? 'lg' : undefined" | ||
> | ||
<!-- This component's "v-model" prop type signature dictates we can't pass it a number. --> | ||
<CFormCheck | ||
v-for="(option) in parameter.options" | ||
:id="option.id" | ||
:key="option.id" | ||
v-model="formData[parameter.id] as string" | ||
type="radio" | ||
:button="{ color: 'primary', variant: 'outline' }" | ||
:name="parameter.id" | ||
autocomplete="off" | ||
:label="option.label" | ||
:value="option.id" | ||
/> | ||
</CButtonGroup> | ||
</CRow> | ||
</CCol> | ||
<div v-else-if="renderAsSelect(parameter)"> | ||
<ParameterIcon :parameter="parameter" /> | ||
<CFormLabel :for="parameter.id"> | ||
{{ parameter.label }} | ||
</CFormLabel> | ||
<select | ||
:id="parameter.id" | ||
v-model="formData[parameter.id]" | ||
:aria-label="parameter.label" | ||
class="form-select" :class="[largeScreen ? 'form-select-lg' : '']" | ||
> | ||
<option | ||
v-for="(option) in parameter.options" | ||
:key="option.id" | ||
:value="option.id" | ||
:selected="option.id === formData[parameter.id]" | ||
> | ||
{{ option.label }} | ||
</option> | ||
</select> | ||
</div> | ||
</div> | ||
<CButton | ||
id="run-button" | ||
color="primary" | ||
:size="largeScreen ? 'lg' : undefined" | ||
type="submit" | ||
> | ||
Run | ||
<CIcon | ||
icon="cilArrowRight" | ||
/> | ||
</CButton> | ||
</CForm> | ||
<CAlert v-else-if="props.metadataFetchStatus === 'error'" color="warning"> | ||
Failed to retrieve metadata from R API. {{ metadataFetchError }} | ||
</CAlert> | ||
<CSpinner v-else-if="props.metadataFetchStatus === 'pending'" /> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import type { FetchError } from "ofetch"; | ||
import { CIcon } from "@coreui/icons-vue"; | ||
import type { Metadata, Parameter } from "@/types/daedalusApiResponseTypes"; | ||
import { ParameterType } from "@/types/daedalusApiResponseTypes"; | ||
import type { AsyncDataRequestStatus } from "#app"; | ||
const props = defineProps<{ | ||
metadata: Metadata | undefined | ||
metadataFetchStatus: AsyncDataRequestStatus | ||
metadataFetchError: FetchError | null | ||
}>(); | ||
const formData = ref( | ||
// Create a new object with keys set to the id values of the metadata.parameters array of objects, and all values set to default values. | ||
props.metadata?.parameters.reduce((acc, { id, defaultOption, options }) => { | ||
acc[id] = defaultOption || options[0].id; | ||
return acc; | ||
}, {} as { [key: string]: string | number }), | ||
); | ||
const optionsAreTerse = (parameter: Parameter) => { | ||
const eachOptionIsASingleWord = parameter.options.every((option) => { | ||
return !option.label.includes(" "); | ||
}); | ||
return parameter.options.length <= 5 && eachOptionIsASingleWord; | ||
}; | ||
const renderAsSelect = (parameter: Parameter) => { | ||
return parameter.parameterType === ParameterType.Select || parameter.parameterType === ParameterType.GlobeSelect; | ||
}; | ||
const renderAsRadios = (parameter: Parameter) => { | ||
return parameter.parameterType === ParameterType.Select && optionsAreTerse(parameter); | ||
}; | ||
const submitForm = () => { | ||
// Not implemented yet | ||
}; | ||
const largeScreen = ref(true); | ||
const breakpoint = 992; // CoreUI's "lg" breakpoint | ||
const setFieldSizes = () => { | ||
if (window.innerWidth < breakpoint) { | ||
largeScreen.value = false; | ||
} else { | ||
largeScreen.value = true; | ||
} | ||
}; | ||
onMounted(() => { | ||
setFieldSizes(); | ||
window.addEventListener("resize", setFieldSizes); | ||
}); | ||
onBeforeUnmount(() => { | ||
window.removeEventListener("resize", setFieldSizes); | ||
}); | ||
</script> | ||
|
||
<style lang="scss"> | ||
.inputs { | ||
display: flex; | ||
flex-wrap: wrap; | ||
row-gap: 1rem; | ||
column-gap: 1rem; | ||
} | ||
.field-container { | ||
min-width: 15rem; | ||
flex-grow: 1; | ||
} | ||
#run-button { | ||
align-self: flex-end; | ||
min-width: 6rem; | ||
margin-left: auto; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<template> | ||
<span v-if="iconDetails" class="form-icon"> | ||
<CIconSvg v-if="iconDetails.custom" class="icon parameter-icon"> | ||
<img :src="customIconPath()"> | ||
</CIconSvg> | ||
<CIcon v-else :icon="iconDetails.icon" class="parameter-icon" /> | ||
</span> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { CIcon, CIconSvg } from "@coreui/icons-vue"; | ||
import type { Parameter } from "@/types/apiResponseTypes"; | ||
const props = defineProps<{ | ||
parameter: Parameter | ||
}>(); | ||
const iconDetails = computed((): { icon: string, custom: boolean } | undefined => { | ||
switch (props.parameter.id) { | ||
case "country": | ||
return { icon: "cilGlobeAlt", custom: false }; | ||
case "response": | ||
return { icon: "cilShieldAlt", custom: false }; | ||
case "vaccine": | ||
return { icon: "cilIndustry", custom: false }; | ||
case "pathogen": | ||
return { icon: "wikimediaVirusIcon", custom: true }; | ||
default: | ||
return undefined; | ||
} | ||
}); | ||
const customIconPath = () => { | ||
if (!iconDetails.value || !iconDetails.value.custom) { | ||
return ""; | ||
} | ||
// The icon svg is exceptionally stored in /public to facilitate a simple way of having a dynamic img src attribute: | ||
// https://www.lichter.io/articles/nuxt3-vue3-dynamic-images/#the-public-folder-strategy | ||
// Bear in mind that this means the image will be cached by the browser, so to update the image, you must also change | ||
// the file name. | ||
return `/icons/${iconDetails.value.icon}.svg`; | ||
}; | ||
</script> | ||
|
||
<style lang="scss"> | ||
.field-container { | ||
.parameter-icon { | ||
margin-left: 0.75rem; | ||
margin-right: 0.5rem; | ||
padding: 0; | ||
} | ||
.button-group-container .parameter-icon { | ||
margin-left: 1.5rem; | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.