|
1 | 1 | <script lang="ts"> |
2 | 2 | import { getQuestionById } from '$lib/services/database'; |
3 | 3 | import SubmitButton from '../ui/SubmitButton.svelte'; |
4 | | - import type { DbResult } from '$lib/services/database/types'; |
5 | | - import type { Database } from '$lib/types/supabase'; |
6 | | - import { getLatestResponses } from '$lib/services/database/responses'; |
7 | | - import { getLatestActions } from '$lib/services/database'; |
8 | 4 | import ToggleStatus from '../ui/ToggleStatus.svelte'; |
9 | | -
|
10 | | - type Question = Database['public']['Tables']['questions']['Row']; |
| 5 | + import { getQuestionDetails } from '$lib/utils/questionDetails.svelte'; |
11 | 6 |
|
12 | 7 | //Delete later --> for development only |
13 | 8 | const user_id = '550e8400-e29b-41d4-a716-446655440005'; |
14 | 9 |
|
| 10 | + let actionType = $state(''); |
| 11 | + let questionDetails = $state<QuestionDetails>({ |
| 12 | + responseInput: null, |
| 13 | + actionsInput: null, |
| 14 | + actionType: '', |
| 15 | + responseId: null |
| 16 | + }); |
| 17 | +
|
15 | 18 | interface Props { |
16 | 19 | questionId: string; |
17 | 20 | } |
18 | 21 |
|
| 22 | + export interface QuestionDetails { |
| 23 | + responseInput: string | null; |
| 24 | + actionsInput: string | null; |
| 25 | + actionType: string; |
| 26 | + responseId: string | null; |
| 27 | + } |
| 28 | +
|
19 | 29 | let { questionId }: Props = $props(); |
20 | 30 |
|
21 | | - const getData = async () => { |
| 31 | + const getQuestionData = async () => { |
22 | 32 | const question = await getQuestionById(questionId); |
23 | | - const previousResponse = await getLatestResponse(); |
24 | | -
|
25 | | - if (previousResponse) responseInput = previousResponse; |
| 33 | + const details = await getQuestionDetails(user_id, questionId); |
| 34 | + questionDetails = details; |
| 35 | + if (details.actionType !== '') actionType = details.actionType; |
26 | 36 |
|
27 | 37 | return { |
28 | 38 | question: question || null, |
29 | | - previousResponse: previousResponse || null |
| 39 | + details: details || null |
30 | 40 | }; |
31 | 41 | }; |
32 | 42 |
|
33 | | - const getLatestResponse = async () => { |
34 | | - const response = await getLatestResponses(user_id); |
35 | | - if (response?.data) { |
36 | | - const questionResponse = response.data.find((r) => r.question_id === questionId); |
37 | | - const previousAction = await getLatestAction(questionResponse?.id); |
38 | | - if (previousAction?.description) { |
39 | | - actionsInput = previousAction.description; |
40 | | - actionType = previousAction.type; |
41 | | - } |
42 | | - if (questionResponse?.visibility) isPublic = questionResponse?.visibility; |
43 | | - if (questionResponse?.id) responseId = questionResponse?.id; |
44 | | - return questionResponse?.response_text; |
45 | | - } |
46 | | - return null; |
47 | | - }; |
48 | | - let responseInput = $state(''); |
49 | | - let responseId = $state(''); |
50 | | -
|
51 | | - const getLatestAction = async (response_Id: string | undefined) => { |
52 | | - const response = await getLatestActions(user_id); |
53 | | - if (response?.data) { |
54 | | - const actionResponse = response.data.find((r) => r.response_id === response_Id); |
55 | | - return actionResponse; |
56 | | - } |
57 | | - return null; |
58 | | - }; |
59 | | - let actionType = $state(''); |
60 | | - let actionsInput = $state(''); |
61 | | - let isPublic = $state('private'); |
62 | | - $inspect(isPublic); |
63 | | -
|
| 43 | + let visibility = $state('private'); |
64 | 44 | const toggleVisibility = () => { |
65 | | - isPublic = isPublic === 'public' ? 'private' : 'public'; |
| 45 | + visibility = visibility === 'public' ? 'private' : 'public'; |
66 | 46 | }; |
67 | 47 | </script> |
68 | 48 |
|
69 | | -{#await getData() then response} |
| 49 | +{#await getQuestionData() then response} |
70 | 50 | {#if response.question && response.question.data} |
71 | 51 | <div class=" m-auto flex min-h-[90dvh] w-sm flex-col justify-around rounded-3xl p-5 shadow-2xl"> |
72 | 52 | <header> |
73 | 53 | <h1 class="text-center text-2xl">{response.question.data.category}</h1> |
74 | 54 | </header> |
75 | 55 |
|
76 | | - <ToggleStatus {isPublic} {toggleVisibility} /> |
| 56 | + <ToggleStatus {visibility} {toggleVisibility} /> |
77 | 57 |
|
78 | 58 | <div class="flex flex-col"> |
79 | 59 | <label for="response-{questionId}" class="text-xl" |
80 | 60 | >{response.question.data.question_text || 'Question'}</label |
81 | 61 | > |
82 | 62 | <textarea |
83 | 63 | id="response-{questionId}" |
84 | | - bind:value={responseInput} |
| 64 | + bind:value={questionDetails.responseInput} |
85 | 65 | placeholder="Enter your response here..." |
86 | 66 | rows="4" |
87 | 67 | class="text-area" |
|
103 | 83 | <label for="actions-{questionId}">Actions needed:</label> |
104 | 84 | <textarea |
105 | 85 | id="actions-{questionId}" |
106 | | - bind:value={actionsInput} |
| 86 | + bind:value={questionDetails.actionsInput} |
107 | 87 | placeholder="Enter your response here..." |
108 | 88 | rows="3" |
109 | 89 | class="text-area" |
|
115 | 95 | <SubmitButton |
116 | 96 | text="Skip" |
117 | 97 | status="skipped" |
118 | | - {responseInput} |
119 | | - {actionsInput} |
120 | 98 | {actionType} |
121 | | - {isPublic} |
122 | | - {responseId} |
| 99 | + details={questionDetails} |
| 100 | + {visibility} |
123 | 101 | /> |
124 | 102 | <SubmitButton |
125 | 103 | text="Submit" |
126 | 104 | status="answered" |
127 | | - {responseInput} |
128 | | - {actionsInput} |
129 | 105 | {actionType} |
130 | | - {isPublic} |
131 | | - {responseId} |
| 106 | + details={questionDetails} |
| 107 | + {visibility} |
132 | 108 | /> |
133 | 109 | </div> |
134 | 110 | </div> |
|
0 commit comments