Skip to content
Snippets Groups Projects
Commit 43e17f1c authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Handle variables of type bool & date.

parent 4846fc3a
No related branches found
No related tags found
No related merge requests found
......@@ -29,15 +29,27 @@
$: entitySituation = situation[entity.key_plural]
function asBoolean(value: boolean | number | string): boolean {
return value as boolean
}
function asNumber(value: boolean | number | string): number {
return value as number
}
function asString(value: boolean | number | string): string {
return value as string
}
function changeValue({ target }: Event, populationId: string) {
let { value }: { value: number | string } = target as
let { value }: { value: boolean | number | string } = target as
| HTMLInputElement
| HTMLSelectElement
switch (variable.value_type) {
case "bool":
value = (target as HTMLInputElement).checked
valueError = null
break
case "date":
;[value, valueError] = auditDateIso8601String(laxAudit, value)
if (valueError !== null) {
......@@ -125,17 +137,37 @@
<option value={symbol}>{label}</option>
{/each}
</select>
{:else if variable.value_type === "bool"}
<label>
<input
checked={asBoolean(
getSituationVariableValue(
$session.entityByKey,
situation,
variable,
populationId,
year,
),
)}
class="p-1 rounded mr-1 my-1 focus:border-le-bleu focus:text-le-bleu"
on:change={(event) => changeValue(event, populationId)}
type="checkbox"
/>
{#if asBoolean(getSituationVariableValue($session.entityByKey, situation, variable, populationId, year))}Oui{:else}Non{/if}
</label>
{:else if variable.value_type === "date"}
<input
class="p-1 rounded my-1 focus:border-le-bleu focus:text-le-bleu"
on:change={(event) => changeValue(event, populationId)}
type="date"
value={getSituationVariableValue(
value={asString(
getSituationVariableValue(
$session.entityByKey,
situation,
variable,
populationId,
year,
),
)}
/>
{:else}
......
......@@ -39,15 +39,27 @@
$: metadata = $session.metadata
function asBoolean(value: boolean | number | string): boolean {
return value as boolean
}
function asNumber(value: boolean | number | string): number {
return value as number
}
function asString(value: boolean | number | string): string {
return value as string
}
function changeValue({ target }: Event, populationId: string) {
let { value }: { value: number | string } = target as
let { value }: { value: boolean | number | string } = target as
| HTMLInputElement
| HTMLSelectElement
switch (variable.value_type) {
case "bool":
value = (target as HTMLInputElement).checked
valueError = null
break
case "date":
;[value, valueError] = auditDateIso8601String(laxAudit, value)
if (valueError !== null) {
......@@ -218,17 +230,37 @@
<option value={symbol}>{label}</option>
{/each}
</select>
{:else if variable.value_type === "bool"}
<label>
<input
checked={asBoolean(
getSituationVariableValue(
$session.entityByKey,
situation,
variable,
populationId,
year,
),
)}
class="p-1 rounded mr-1 my-1 focus:border-le-bleu focus:text-le-bleu"
on:change={(event) => changeValue(event, populationId)}
type="checkbox"
/>
{#if asBoolean(getSituationVariableValue($session.entityByKey, situation, variable, populationId, year))}Oui{:else}Non{/if}
</label>
{:else if variable.value_type === "date"}
<input
class="p-1 rounded my-1 focus:border-le-bleu focus:text-le-bleu"
on:change={(event) => changeValue(event, populationId)}
type="date"
value={getSituationVariableValue(
value={asString(
getSituationVariableValue(
$session.entityByKey,
situation,
variable,
populationId,
year,
),
)}
/>
{:else}
......
......@@ -57,7 +57,7 @@ export function getSituationVariableValue(
variable: Variable,
populationId: string,
year: number,
): number | string {
): boolean | number | string {
const entity = entityByKey[variable.entity]
const entitySituation = situation[entity.key_plural]
return (
......@@ -108,7 +108,7 @@ export function setSituationVariableValue(
variable: Variable,
populationId: string,
year: number,
value: number | string,
value: boolean | number | string,
): Situation {
const entity = entityByKey[variable.entity]
const entitySituation = situation[entity.key_plural]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment