auto save, poll and comments improvements

This commit is contained in:
Rotimi Best
2023-10-09 11:30:26 +01:00
parent 203bbe2106
commit e47d3d38eb
7 changed files with 76 additions and 51 deletions
@@ -53,6 +53,7 @@
commentAt: new Date() commentAt: new Date()
} }
]; ];
$lesson.totalComments = comments.length;
scrollToBottom(); scrollToBottom();
isSaving = true; isSaving = true;
@@ -65,9 +66,8 @@
}) })
.then(() => { .then(() => {
isSaving = false; isSaving = false;
comment = '';
}); });
comment = '';
} }
function handleKeyDown(e: KeyboardEvent) { function handleKeyDown(e: KeyboardEvent) {
@@ -212,7 +212,7 @@
</p> </p>
</div> </div>
</div> </div>
<article class="prose prose-sm sm:prose ml-8 max-w-[300px]"> <article class="prose prose-sm sm:prose ml-8 max-w-[300px] dark:text-white">
{comment.comment} {comment.comment}
</article> </article>
</div> </div>
@@ -1,46 +1,47 @@
<script> <script lang="ts">
import FormSection from './FormSection.svelte'; import FormSection from './FormSection.svelte';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte'; import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import { VARIANTS } from '$lib/components/PrimaryButton/constants'; import { VARIANTS } from '$lib/components/PrimaryButton/constants';
import type { Poll } from '../types';
export let title = 'Poll'; export let title = 'Poll';
export let onSubmit; export let onSubmit = (p: Poll) => {};
export let onCancel; export let onCancel = () => {};
let values = { let poll: Poll = {
question: '', question: '',
author: '', authorId: '',
isPublic: false, isPublic: false,
expiration: '', expiration: '',
options: [ options: [
{ {
label: '', label: '',
selectedBy: [], selectedBy: []
}, }
], ]
}; };
function handleAddOptions() { function handleAddOptions() {
values = { poll = {
...values, ...poll,
options: [ options: [
...values.options, ...poll.options,
{ {
label: '', label: '',
selectedBy: [], selectedBy: []
}, }
], ]
}; };
} }
function handleRemoveOptions(index) { function handleRemoveOptions(index: number) {
return () => { return () => {
values.options = values.options.filter((o, i) => i !== index); poll.options = poll.options.filter((o, i) => i !== index);
}; };
} }
function finishPoll() { function finishPoll() {
onSubmit(values); onSubmit(poll);
} }
</script> </script>
@@ -49,16 +50,23 @@
{title} {title}
</div> </div>
<div class="p-3"> <div class="p-3">
<FormSection label="Question" bind:value={values.question} /> <FormSection
label="Question"
bind:value={poll.question}
handleLabelButton={undefined}
handleInputButton={undefined}
/>
<FormSection <FormSection
label="Expiration" label="Expiration"
bind:value={values.expiration} bind:value={poll.expiration}
type="date" type="date"
handleLabelButton={undefined}
handleInputButton={undefined}
/> />
{#each values.options as option, index} {#each poll.options as option, index}
<FormSection <FormSection
label={index === 0 && 'Options'} label={index === 0 ? 'Options' : ''}
bind:value={option.label} bind:value={option.label}
handleLabelButton={handleAddOptions} handleLabelButton={handleAddOptions}
handleInputButton={handleRemoveOptions(index)} handleInputButton={handleRemoveOptions(index)}
@@ -68,10 +76,6 @@
<div class="w-full flex justify-center mb-3"> <div class="w-full flex justify-center mb-3">
<PrimaryButton label="Create poll" onClick={finishPoll} /> <PrimaryButton label="Create poll" onClick={finishPoll} />
<PrimaryButton <PrimaryButton label="Cancel" variant={VARIANTS.OUTLINED} onClick={onCancel} />
label="Cancel"
variant={VARIANTS.OUTLINED}
onClick={onCancel}
/>
</div> </div>
</div> </div>
@@ -1,23 +1,19 @@
<script> <script lang="ts">
export let value; export let value: string | Date;
export let label; export let label: string;
export let type = 'text'; export let type = 'text';
export let name = 'text'; export let name = 'text';
export let handleLabelButton; export let handleLabelButton: undefined | (() => void);
export let handleInputButton; export let handleInputButton: undefined | (() => void);
</script> </script>
<div class="mb-3"> <div class="mb-3">
{#if label} {#if label}
<p <p class="dark:text-white p-0 m-0 mb-1 uppercase text-sm font-bold text-gray-600">
class="dark:text-white p-0 m-0 mb-1 uppercase text-sm font-bold text-gray-600"
>
{label} {label}
{#if typeof handleLabelButton === 'function'} {#if typeof handleLabelButton === 'function'}
<button class="text-xl px-3 ml-5" on:click={handleLabelButton}> <button class="text-xl px-3 ml-5" on:click={handleLabelButton}> + </button>
+
</button>
{/if} {/if}
</p> </p>
{/if} {/if}
@@ -32,9 +28,7 @@
{/if} {/if}
{#if typeof handleInputButton === 'function'} {#if typeof handleInputButton === 'function'}
<button class="text-xl px-3 ml-5" on:click={handleInputButton}> <button class="text-xl px-3 ml-5" on:click={handleInputButton}> - </button>
-
</button>
{/if} {/if}
</div> </div>
</div> </div>
@@ -110,7 +110,7 @@
</div> </div>
<Label>Responses</Label> <Label>Responses</Label>
{#each poll.options as option, index} {#each poll.options as option}
<div class="border-b p-5"> <div class="border-b p-5">
<p class="dark:text-white"> <p class="dark:text-white">
{option.label} {option.label}
@@ -140,7 +140,7 @@
{:else} {:else}
{#each poll.options as option, index} {#each poll.options as option, index}
<button <button
class="bg-white dark:bg-black rounded-md border-2 border-gray-500 {didUserSelect( class="bg-white dark:bg-black rounded-md border-2 border-gray-500 h-[50px] {didUserSelect(
option option
) && ) &&
'focus:border-primary-700 border-primary-700'} text-black p-2 w-full mb-3 text-left relative" 'focus:border-primary-700 border-primary-700'} text-black p-2 w-full mb-3 text-left relative"
@@ -153,9 +153,11 @@
: 'bg-gray-300'} h-full bg-opacity-25" : 'bg-gray-300'} h-full bg-opacity-25"
style="width: {calculatePercent(poll.options, option)}%;" style="width: {calculatePercent(poll.options, option)}%;"
/> />
<strong class="mr-3">{calculatePercent(poll.options, option)}%</strong>
{/if} {/if}
{option.label} <p class="absolute top-3 text-black left-2">
{option.label}
({calculatePercent(poll.options, option)}%)
</p>
</button> </button>
{/each} {/each}
<div class="flex items-center"> <div class="flex items-center">
@@ -43,7 +43,7 @@
]; ];
let selectedTab = 0; let selectedTab = 0;
let userId = users[0].value; let userId = 'satoshi-nakamoto';
let shouldCreatePoll = false; let shouldCreatePoll = false;
let polls = [ let polls = [
{ {
@@ -92,7 +92,11 @@
polls = [ polls = [
{ {
...poll, ...poll,
author: users.find((u) => u.value === userId) author: {
label: 'Satoshi Nakamoto',
value: 'satoshi-nakamoto',
avatar: 'https://i.pravatar.cc/150?img=4'
}
}, },
...polls ...polls
]; ];
@@ -122,7 +126,11 @@
// Select if already selected // Select if already selected
option.selectedBy = [ option.selectedBy = [
...option.selectedBy, ...option.selectedBy,
users.find((user) => user.value === userId) {
label: 'Satoshi Nakamoto',
value: 'satoshi-nakamoto',
avatar: 'https://i.pravatar.cc/150?img=4'
}
]; ];
} }
} else if (option.selectedBy.find((user) => user.value === userId)) { } else if (option.selectedBy.find((user) => user.value === userId)) {
@@ -147,7 +155,7 @@
</div> </div>
</PageNav> </PageNav>
<div class="m-2 overlow-y-auto min-w-[300px]"> <div class="m-2 overlow-y-auto min-w-[350px]">
{#if shouldCreatePoll} {#if shouldCreatePoll}
<CreatePollForm onSubmit={handlePollCreate} onCancel={handleCreatePoll} /> <CreatePollForm onSubmit={handlePollCreate} onCancel={handleCreatePoll} />
{:else} {:else}
@@ -0,0 +1,14 @@
export type Poll = {
question: string;
authorId: string; // groupmember_id
isPublic: boolean;
expiration: string | Date;
options: {
label: string;
selectedBy: {
id: string;
fullname: string;
avatarUrl: string;
}[];
}[];
};
@@ -170,6 +170,9 @@
_isLoading?: boolean, _isLoading?: boolean,
lessonId?: string lessonId?: string
) { ) {
// don't autosave on view mode
if (mode === MODES.view) return;
if (timeoutId) clearTimeout(timeoutId); if (timeoutId) clearTimeout(timeoutId);
console.log('autosaving'); console.log('autosaving');
if (!initAutoSave) { if (!initAutoSave) {