Skip to content

Commit 084b909

Browse files
Merge pull request #18 from foundersandcoders:refactor
Refactor Question card component
2 parents b254cda + 78a775a commit 084b909

File tree

5 files changed

+84
-74
lines changed

5 files changed

+84
-74
lines changed

src/lib/components/cards/QuestionCard.svelte

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,67 @@
11
<script lang="ts">
22
import { getQuestionById } from '$lib/services/database';
33
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';
84
import ToggleStatus from '../ui/ToggleStatus.svelte';
9-
10-
type Question = Database['public']['Tables']['questions']['Row'];
5+
import { getQuestionDetails } from '$lib/utils/questionDetails.svelte';
116
127
//Delete later --> for development only
138
const user_id = '550e8400-e29b-41d4-a716-446655440005';
149
10+
let actionType = $state('');
11+
let questionDetails = $state<QuestionDetails>({
12+
responseInput: null,
13+
actionsInput: null,
14+
actionType: '',
15+
responseId: null
16+
});
17+
1518
interface Props {
1619
questionId: string;
1720
}
1821
22+
export interface QuestionDetails {
23+
responseInput: string | null;
24+
actionsInput: string | null;
25+
actionType: string;
26+
responseId: string | null;
27+
}
28+
1929
let { questionId }: Props = $props();
2030
21-
const getData = async () => {
31+
const getQuestionData = async () => {
2232
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;
2636
2737
return {
2838
question: question || null,
29-
previousResponse: previousResponse || null
39+
details: details || null
3040
};
3141
};
3242
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');
6444
const toggleVisibility = () => {
65-
isPublic = isPublic === 'public' ? 'private' : 'public';
45+
visibility = visibility === 'public' ? 'private' : 'public';
6646
};
6747
</script>
6848

69-
{#await getData() then response}
49+
{#await getQuestionData() then response}
7050
{#if response.question && response.question.data}
7151
<div class=" m-auto flex min-h-[90dvh] w-sm flex-col justify-around rounded-3xl p-5 shadow-2xl">
7252
<header>
7353
<h1 class="text-center text-2xl">{response.question.data.category}</h1>
7454
</header>
7555

76-
<ToggleStatus {isPublic} {toggleVisibility} />
56+
<ToggleStatus {visibility} {toggleVisibility} />
7757

7858
<div class="flex flex-col">
7959
<label for="response-{questionId}" class="text-xl"
8060
>{response.question.data.question_text || 'Question'}</label
8161
>
8262
<textarea
8363
id="response-{questionId}"
84-
bind:value={responseInput}
64+
bind:value={questionDetails.responseInput}
8565
placeholder="Enter your response here..."
8666
rows="4"
8767
class="text-area"
@@ -103,7 +83,7 @@
10383
<label for="actions-{questionId}">Actions needed:</label>
10484
<textarea
10585
id="actions-{questionId}"
106-
bind:value={actionsInput}
86+
bind:value={questionDetails.actionsInput}
10787
placeholder="Enter your response here..."
10888
rows="3"
10989
class="text-area"
@@ -115,20 +95,16 @@
11595
<SubmitButton
11696
text="Skip"
11797
status="skipped"
118-
{responseInput}
119-
{actionsInput}
12098
{actionType}
121-
{isPublic}
122-
{responseId}
99+
details={questionDetails}
100+
{visibility}
123101
/>
124102
<SubmitButton
125103
text="Submit"
126104
status="answered"
127-
{responseInput}
128-
{actionsInput}
129105
{actionType}
130-
{isPublic}
131-
{responseId}
106+
details={questionDetails}
107+
{visibility}
132108
/>
133109
</div>
134110
</div>

src/lib/components/ui/SubmitButton.svelte

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
<script lang="ts">
22
import { createAction } from '$lib/services/database/actions';
33
import { createResponse } from '$lib/services/database/responses';
4+
import type { QuestionDetails } from '../cards/QuestionCard.svelte';
45
56
//Delete later --> for development only
67
const user_id = '550e8400-e29b-41d4-a716-446655440005';
78
const questionId = 'bacc6ffa-b589-4bdc-8eb8-d29eeef7f153';
89
910
interface Props {
1011
text: string;
11-
responseInput: string;
12-
actionsInput: string;
13-
actionType: string;
1412
status: string;
15-
isPublic: string;
16-
responseId: string;
13+
visibility: string;
14+
actionType: string;
15+
details: QuestionDetails;
1716
}
1817
19-
let { text, responseInput, actionsInput, actionType, status, isPublic, responseId }: Props =
20-
$props();
18+
let { text, status, details, actionType, visibility }: Props = $props();
2119
2220
function handleSubmit() {
2321
createResponse(user_id, {
24-
response_text: responseInput,
22+
response_text: details.responseInput,
2523
question_id: questionId,
2624
status: status,
27-
visibility: isPublic
25+
visibility: visibility
26+
});
27+
createAction(user_id, {
28+
type: actionType,
29+
description: details.actionsInput,
30+
response_id: details.responseId
2831
});
29-
createAction(user_id, { type: actionType, description: actionsInput, response_id: responseId });
3032
}
3133
</script>
3234

src/lib/components/ui/ToggleStatus.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
let { isPublic, toggleVisibility } = $props();
2+
let { visibility, toggleVisibility } = $props();
33
</script>
44

55
<div class="flex items-center gap-3">
@@ -17,5 +17,5 @@
1717
>
1818
</label>
1919
</div>
20-
<span class=" text-sm font-medium">{isPublic}</span>
20+
<span class=" text-sm font-medium">{visibility}</span>
2121
</div>
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<script lang="ts">
2-
import type { View } from "$lib/types/ui";
2+
import type { View } from '$lib/types/ui';
33
import { getContext } from 'svelte';
4+
import QuestionCard from '$lib/components/cards/QuestionCard.svelte';
45
5-
const setView = getContext<(view:View) => void>('setView');
6+
const setView = getContext<(view: View) => void>('setView');
67
7-
const onBackClick = () => { setView("list") };
8+
const onBackClick = () => {
9+
setView('list');
10+
};
11+
12+
const questionId = 'bacc6ffa-b589-4bdc-8eb8-d29eeef7f153';
813
</script>
914

1015
<div class="dev dev-div">
1116
<div id="detail-header" class="dev dev-div flex flex-row justify-between">
1217
<h2 class="dev dev-div">Detail View</h2>
1318

14-
<button onclick={onBackClick} class="dev dev-div dev-button">
15-
Back
16-
</button>
19+
<button onclick={onBackClick} class="dev dev-div dev-button"> Back </button>
1720
</div>
1821

1922
<div id="detail-content" class="dev dev-div">
23+
<QuestionCard {questionId} />
2024
</div>
21-
</div>
25+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { getLatestResponses } from '$lib/services/database/responses';
2+
import { getLatestActions } from '$lib/services/database';
3+
import type { QuestionDetails } from '$lib/components/cards/QuestionCard.svelte';
4+
5+
export const getQuestionDetails = async (
6+
user_id: string,
7+
questionId: string
8+
): Promise<QuestionDetails> => {
9+
const response = await getLatestResponses(user_id);
10+
const questionResponse = response.data?.find((r) => r.question_id === questionId);
11+
const previousAction = await getActionDetails(questionResponse?.id, user_id);
12+
13+
return {
14+
responseInput: questionResponse?.response_text || null,
15+
actionsInput: previousAction?.description || null,
16+
actionType: previousAction?.type || '',
17+
responseId: questionResponse?.id || null
18+
};
19+
};
20+
21+
const getActionDetails = async (response_Id: string | undefined, user_id: string) => {
22+
const response = await getLatestActions(user_id);
23+
if (response?.data) {
24+
const actionResponse = response.data.find((r) => r.response_id === response_Id);
25+
return actionResponse;
26+
}
27+
return null;
28+
};

0 commit comments

Comments
 (0)