RateComment.svelte 2.89 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
<script lang="ts">
	import { toast } from 'svelte-sonner';

4
	import { createEventDispatcher, onMount, getContext } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
5

6
	const i18n = getContext('i18n');
Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
9

	const dispatch = createEventDispatcher();

10
	export let messageId = null;
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
13
	export let show = false;
	export let message;

14
15
16
17
18
	let LIKE_REASONS = [];
	let DISLIKE_REASONS = [];

	function loadReasons() {
		LIKE_REASONS = [
Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
22
23
24
25
			$i18n.t('Accurate information'),
			$i18n.t('Followed instructions perfectly'),
			$i18n.t('Showcased creativity'),
			$i18n.t('Positive attitude'),
			$i18n.t('Attention to detail'),
			$i18n.t('Thorough explanation'),
			$i18n.t('Other')
26
27
28
29
		];

		DISLIKE_REASONS = [
			$i18n.t("Don't like the style"),
Timothy J. Baek's avatar
Timothy J. Baek committed
30
			$i18n.t('Not factually correct'),
31
32
			$i18n.t("Didn't fully follow instructions"),
			$i18n.t("Refused when it shouldn't have"),
Timothy J. Baek's avatar
Timothy J. Baek committed
33
34
			$i18n.t('Being lazy'),
			$i18n.t('Other')
35
36
		];
	}
Timothy J. Baek's avatar
Timothy J. Baek committed
37
38
39
40
41

	let reasons = [];
	let selectedReason = null;
	let comment = '';

Timothy J. Baek's avatar
Timothy J. Baek committed
42
	$: if (message?.annotation?.rating === 1) {
Timothy J. Baek's avatar
Timothy J. Baek committed
43
		reasons = LIKE_REASONS;
Timothy J. Baek's avatar
Timothy J. Baek committed
44
	} else if (message?.annotation?.rating === -1) {
Timothy J. Baek's avatar
Timothy J. Baek committed
45
46
47
48
		reasons = DISLIKE_REASONS;
	}

	onMount(() => {
Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
49
		selectedReason = message?.annotation?.reason ?? '';
Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
50
		comment = message?.annotation?.comment ?? '';
51
		loadReasons();
Timothy J. Baek's avatar
Timothy J. Baek committed
52
53
54
55
56
57
58
59
60
61
	});

	const submitHandler = () => {
		console.log('submitHandler');

		message.annotation.reason = selectedReason;
		message.annotation.comment = comment;

		dispatch('submit');

62
		toast.success($i18n.t('Thanks for your feedback!'));
Timothy J. Baek's avatar
Timothy J. Baek committed
63
64
65
66
		show = false;
	};
</script>

67
68
69
70
<div
	class=" my-2.5 rounded-xl px-4 py-3 border dark:border-gray-850"
	id="message-feedback-{messageId}"
>
Timothy J. Baek's avatar
Timothy J. Baek committed
71
	<div class="flex justify-between items-center">
72
		<div class=" text-sm">{$i18n.t('Tell us more:')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

		<button
			on:click={() => {
				show = false;
			}}
		>
			<svg
				xmlns="http://www.w3.org/2000/svg"
				fill="none"
				viewBox="0 0 24 24"
				stroke-width="1.5"
				stroke="currentColor"
				class="size-4"
			>
				<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
			</svg>
		</button>
	</div>

	{#if reasons.length > 0}
		<div class="flex flex-wrap gap-2 text-sm mt-2.5">
			{#each reasons as reason}
				<button
96
					class="px-3.5 py-1 border dark:border-gray-850 hover:bg-gray-100 dark:hover:bg-gray-850 {selectedReason ===
Timothy J. Baek's avatar
Timothy J. Baek committed
97
					reason
98
						? 'bg-gray-200 dark:bg-gray-800'
Timothy J. Baek's avatar
Timothy J. Baek committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
						: ''} transition rounded-lg"
					on:click={() => {
						selectedReason = reason;
					}}
				>
					{reason}
				</button>
			{/each}
		</div>
	{/if}

	<div class="mt-2">
		<textarea
			bind:value={comment}
			class="w-full text-sm px-1 py-2 bg-transparent outline-none resize-none rounded-xl"
Timothy J. Baek's avatar
Timothy J. Baek committed
114
			placeholder={$i18n.t('Feel free to add specific details')}
Timothy J. Baek's avatar
Timothy J. Baek committed
115
116
117
118
119
120
			rows="2"
		/>
	</div>

	<div class="mt-2 flex justify-end">
		<button
Timothy J. Baek's avatar
Timothy J. Baek committed
121
			class=" bg-emerald-700 text-white text-sm font-medium rounded-lg px-3.5 py-1.5"
Timothy J. Baek's avatar
Timothy J. Baek committed
122
123
124
125
			on:click={() => {
				submitHandler();
			}}
		>
Timothy J. Baek's avatar
Timothy J. Baek committed
126
			{$i18n.t('Submit')}
Timothy J. Baek's avatar
Timothy J. Baek committed
127
128
129
		</button>
	</div>
</div>