Skip to content

Commit 0bc632f

Browse files
authored
Persist Questions When Clicked (#69)
* Persisting questions when clicked. * - Refactored the questions to store them in the localStorage so we can persist them when a question is clicked.
1 parent a68557d commit 0bc632f

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

database/seeders/PromptSeeder.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public function run(): void
3636
[
3737
'category' => 'questions',
3838
'prompt' => <<<'EOT'
39-
Create an curriculum outline for the subject for the kids to learn more about the topic below.
39+
Create a curriculum outline for the subject for the kids to learn more about the topic below.
4040
Don't number anything and simply list out the topics to the question. Each of the topics needs
4141
to be able to be on it's own. (exclude things like "conclusion" "Wrapping up"). Give me the results
42-
in a list and in json format with a key of questions. Include at least 10 items in the list.
42+
in a list and in json format with a key of questions and each item being an object that contains
43+
a question and a selected property set to false like such {question: "Why is the sky blue", selected: false}.
44+
Include at least 10 items in the list.
4345
EOT
4446
],
4547
[

resources/js/Pages/Student/Prompts/Index.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@
107107
const submit = () => {
108108
flagged.value = false;
109109
form.post(route('student.prompts.store'));
110+
111+
localStorage.removeItem('questions');
110112
}
111113
112-
const handleQuestionClicked = (question) => {
113-
form.question = question;
114+
const handleQuestionClicked = (event) => {
115+
form.question = event.question;
114116
form.post(route('student.prompts.store'));
115117
}
116118

resources/js/Pages/Student/Prompts/Questions.vue

+24-11
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
<div class="whitespace-pre-wrap mt-14">
1414
<ul>
1515
<li
16-
v-for="(question, index) in questions"
16+
v-for="(item, index) in questions"
1717
:key="index"
1818
class="mb-2.5 cursor-pointer text-primary-yellow underline"
19-
@click.prevent="questionClicked(question)"
19+
:class="{'text-yellow-300 font-bold': item.selected}"
20+
@click.prevent="questionClicked(item)"
2021
>
21-
{{ question }}
22+
{{ item.question }}
2223
</li>
2324
</ul>
2425
</div>
@@ -42,18 +43,30 @@ const props = defineProps({
4243
4344
const emit = defineEmits(['questionClicked']);
4445
45-
const questions = ref([]);
46+
const questions = ref(JSON.parse(localStorage.getItem('questions')) || []);
4647
const loading = ref(false);
4748
4849
onMounted(() => {
49-
loading.value = true;
50-
axios.post('/student/prompts/questions', {question: props.question}).then(response => {
51-
questions.value = response.data.questions;
52-
loading.value = false;
53-
});
50+
if (questions.value.length === 0) {
51+
loading.value = true;
52+
53+
axios.post('/student/prompts/questions', {question: props.question}).then(response => {
54+
questions.value = response.data.questions;
55+
loading.value = false;
56+
});
57+
}
5458
});
5559
56-
const questionClicked = (question) => {
57-
emit('questionClicked', question);
60+
const questionClicked = (item) => {
61+
const formattedQuestions = questions.value.map((q, index) => {
62+
return {
63+
'question': q.question,
64+
'selected': q.question === item.question || q.selected
65+
}
66+
});
67+
68+
localStorage.setItem('questions', JSON.stringify(formattedQuestions));
69+
70+
emit('questionClicked', {question: item.question});
5871
}
5972
</script>

0 commit comments

Comments
 (0)