Skip to content

Commit

Permalink
Add type signature to ParameterForm formData and create ParameterType…
Browse files Browse the repository at this point in the history
… enum
  • Loading branch information
david-mears-2 committed Sep 5, 2024
1 parent 19dc5db commit 529fefc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
37 changes: 13 additions & 24 deletions components/ParameterForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
: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]"
v-model="formData[parameter.id] as string"
type="radio"
:button="{ color: 'primary', variant: 'outline' }"
:name="parameter.id"
Expand Down Expand Up @@ -84,6 +85,7 @@
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<{
Expand All @@ -92,28 +94,15 @@ const props = defineProps<{
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 refs with default values.
props.metadata?.parameters.reduce((accumulator, parameter) => {
if (parameter.parameterType !== "select" && parameter.parameterType !== "globeSelect") {
accumulator[parameter.id] = ref("");
const formData = props.metadata
? ref(
// Create a new object with keys set to the id values of the metadata.parameters array of objects, and all values set to refs with default values.
props.metadata.parameters.reduce((accumulator, parameter) => {
accumulator[parameter.id] = parameter.defaultOption || parameter.options[0].id;
return accumulator;
}
// TODO: Make default country UK after November 2024 workshop
if (parameter.id === "country") {
accumulator[parameter.id] = ref("Thailand");
} else {
// Don't set an empty value or there will be a disjoint between the select component and the formData object,
// since the select component will visually appear to have an option selected (the first), but the formData object will have
// an empty string.
const defaultOption = parameter.defaultOption || parameter.options[0].id;
accumulator[parameter.id] = ref(defaultOption);
}
return accumulator;
}, {} as { [key: string]: any }),
);
}, {} as { [key: string]: string | number }),
)
: ref(undefined);
const optionsAreTerse = (parameter: Parameter) => {
const eachOptionIsASingleWord = parameter.options.every((option) => {
Expand All @@ -124,11 +113,11 @@ const optionsAreTerse = (parameter: Parameter) => {
};
const renderAsSelect = (parameter: Parameter) => {
return parameter.parameterType === "select" || parameter.parameterType === "globeSelect";
return parameter.parameterType === ParameterType.Select || parameter.parameterType === ParameterType.GlobeSelect;
};
const renderAsRadios = (parameter: Parameter) => {
return parameter.parameterType === "select" && optionsAreTerse(parameter);
return parameter.parameterType === ParameterType.Select && optionsAreTerse(parameter);
};
const submitForm = () => {
Expand Down
4 changes: 2 additions & 2 deletions pages/scenarios/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<script lang="ts" setup>
import type { FetchError } from "ofetch";
import type { AsyncDataRequestStatus } from "#app";
import type { Metadata } from "@/types/daedalusApiResponseTypes";
import { type Metadata, ParameterType } from "@/types/daedalusApiResponseTypes";
const { data: metadata, status: metadataFetchStatus, error: metadataFetchError } = useFetch("/api/metadata") as {
data: Ref<Metadata>
Expand All @@ -26,7 +26,7 @@ const { data: metadata, status: metadataFetchStatus, error: metadataFetchError }
const globeParameter = computed(() => {
if (metadata.value) {
return metadata.value.parameters.filter(parameter => parameter.parameterType === "globeSelect")[0];
return metadata.value.parameters.filter(parameter => parameter.parameterType === ParameterType.GlobeSelect)[0];
} else {
return undefined;
}
Expand Down
6 changes: 5 additions & 1 deletion types/daedalusApiResponseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ export interface ParameterOption {
label: string
}

export enum ParameterType {
Select = "select",
GlobeSelect = "globeSelect",
}
export interface Parameter {
id: string
label: string
parameterType: string
parameterType: ParameterType
defaultOption: string | null
ordered: boolean
options: Array<ParameterOption>
Expand Down

0 comments on commit 529fefc

Please sign in to comment.