This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
atsebay.t/ui/src/components/QuestionForm.svelte

157 lines
5.9 KiB
Svelte
Raw Normal View History

2021-11-18 11:12:28 +00:00
<script>
import { createEventDispatcher } from 'svelte';
2022-09-02 11:09:39 +00:00
import ListInput from './ListInput.svelte';
2022-02-28 09:52:27 +00:00
import QuestionHeader from './QuestionHeader.svelte';
import QuestionProposals from './QuestionProposals.svelte';
import ResponseCorrected from './ResponseCorrected.svelte';
2021-11-18 11:12:28 +00:00
import { user } from '../stores/user';
const dispatch = createEventDispatcher();
2022-02-28 09:52:27 +00:00
2021-11-18 11:12:28 +00:00
let className = '';
export { className as class };
export let question;
export let qid;
export let response_history = null;
export let readonly = false;
2022-09-02 09:54:58 +00:00
export let corrections = null;
2022-02-28 09:52:27 +00:00
export let survey = null;
2021-11-18 11:12:28 +00:00
export let value = "";
export let edit = false;
function saveQuestion() {
question.save().then((response) => {
question.description = response.description;
question = question;
edit = false;
})
}
function editQuestion() {
edit = true;
}
</script>
<div class="card my-3 {className}">
2022-02-28 09:52:27 +00:00
<QuestionHeader
bind:question={question}
{qid}
{edit}
>
{#if $user && $user.is_admin}
2022-03-01 19:21:49 +00:00
<button type="button" class="btn btn-sm btn-danger ms-1 float-end" on:click={() => dispatch('delete')}>
2021-11-18 11:12:28 +00:00
<i class="bi bi-trash-fill"></i>
</button>
{#if edit}
2022-03-01 19:21:49 +00:00
<button type="button" class="btn btn-sm btn-success ms-1 float-end" on:click={saveQuestion}>
2021-11-18 11:12:28 +00:00
<i class="bi bi-check"></i>
</button>
{:else}
2022-03-01 19:21:49 +00:00
<button type="button" class="btn btn-sm btn-primary ms-1 float-end" on:click={editQuestion}>
2021-11-18 11:12:28 +00:00
<i class="bi bi-pencil"></i>
</button>
{/if}
{/if}
2022-02-28 09:52:27 +00:00
</QuestionHeader>
2022-02-28 18:00:30 +00:00
<slot></slot>
2021-11-18 11:12:28 +00:00
<div class="card-body">
{#if false && response_history}
<div class="d-flex justify-content-end mb-2">
<div class="col-auto">
Historique&nbsp;:
<select class="form-select">
<option value="new">Actuel</option>
{#each response_history as history (history.id)}
<option value={history.id}>{new Intl.DateTimeFormat('default', { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric'}).format(new Date(history.time_submit))}</option>
{/each}
</select>
</div>
</div>
{/if}
{#if edit}
2022-09-02 11:09:39 +00:00
{#if question.kind && (question.kind == 'text' || question.kind == 'int' || question.kind.startsWith('list'))}
2021-11-18 11:12:28 +00:00
<div class="form-group row">
<label class="col-2 col-form-label" for="q{qid}placeholder">Placeholder</label>
<div class="col">
<input class="form-control" id="q{qid}placeholder" bind:value={question.placeholder}>
</div>
</div>
2022-03-01 19:21:49 +00:00
{:else if question.kind}
{#if !question.id}
Veuillez enregistrer la question pour pouvoir ajouter des propositions.
{:else}
{#await question.getProposals()}
<div class="text-center">
<div class="spinner-border text-primary mx-3" role="status"></div>
<span>Chargement des choix &hellip;</span>
</div>
{:then proposals}
<QuestionProposals
edit
id_question={question.id}
kind={question.kind}
{proposals}
readonly
2022-09-01 17:15:53 +00:00
live={survey.direct !== null}
2022-09-01 20:09:14 +00:00
{corrections}
2022-03-01 19:21:49 +00:00
bind:value={value}
on:change={() => { dispatch("change"); }}
/>
{/await}
{/if}
2021-11-18 11:12:28 +00:00
{/if}
{:else if question.kind == 'mcq' || question.kind == 'ucq'}
{#await question.getProposals()}
<div class="text-center">
<div class="spinner-border text-primary mx-3" role="status"></div>
<span>Chargement des choix &hellip;</span>
</div>
{:then proposals}
<QuestionProposals
kind={question.kind}
{proposals}
{readonly}
2022-09-01 17:15:53 +00:00
live={survey.direct !== null}
2022-09-01 20:09:14 +00:00
{corrections}
2021-11-18 11:12:28 +00:00
bind:value={value}
2022-02-28 18:00:30 +00:00
on:change={() => { dispatch("change"); }}
2021-11-18 11:12:28 +00:00
/>
{/await}
{:else if readonly}
<p class="card-text alert alert-secondary" style="white-space: pre-line">{value}</p>
{:else if question.kind == 'int'}
2022-02-28 18:00:30 +00:00
<input
class="ml-5 col-sm-2 form-control"
type="number"
bind:value={value}
placeholder={question.placeholder}
on:change={() => { dispatch("change"); }}
>
2022-09-02 11:09:39 +00:00
{:else if question.kind && question.kind.startsWith('list')}
<ListInput
class="ml-5 col-sm-2 form-control my-1"
kind={question.kind}
bind:value={value}
placeholder={question.placeholder}
on:change={() => { dispatch("change"); }}
/>
2021-11-18 11:12:28 +00:00
{:else}
2022-02-28 18:00:30 +00:00
<textarea
class="form-control"
rows="6"
bind:value={value}
placeholder={question.placeholder}
on:change={() => { dispatch("change"); }}
></textarea>
2021-11-18 11:12:28 +00:00
{/if}
{#if survey && survey.corrected && response_history}
2022-02-28 09:52:27 +00:00
<ResponseCorrected
response={response_history}
{survey}
/>
{/if}
2021-11-18 11:12:28 +00:00
</div>
</div>