Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit e55bbcd

Browse files
Added vuex. Also integrated it with FAQs.
1 parent 730a835 commit e55bbcd

File tree

5 files changed

+65
-31
lines changed

5 files changed

+65
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"register-service-worker": "^1.6.2",
2525
"vue": "^2.6.10",
2626
"vue-router": "^3.0.3",
27-
"vuex": "^3.0.1"
27+
"vuex": "^3.1.1"
2828
},
2929
"devDependencies": {
3030
"@vue/cli-plugin-babel": "^3.8.0",

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue';
22
import App from './App.vue';
33
import router from './router';
4-
import store from './store';
4+
import store from './store/store';
55
import './registerServiceWorker';
66

77
Vue.config.productionTip = false;

src/store/modules/FAQs.module.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const FAQsModule = {
2+
namespaced: true,
3+
state: {
4+
FAQs: [],
5+
},
6+
7+
mutations: {
8+
ADD_FAQS(state, FAQs) {
9+
state.FAQs = FAQs;
10+
},
11+
},
12+
13+
actions: {
14+
addFAQsAction(context) {
15+
fetch('https://api-dev.codinggarden.community/faqs')
16+
.then(n => n.json())
17+
.then(json => {
18+
const dataItems = json.map(n => ({
19+
...n.attributes,
20+
isOpen: false,
21+
}));
22+
context.commit('ADD_FAQS', dataItems);
23+
});
24+
},
25+
},
26+
};
27+
28+
export default FAQsModule;
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Vue from 'vue';
22
import Vuex from 'vuex';
3+
import FAQs from './modules/FAQs.module';
34

45
Vue.use(Vuex);
56

67
export default new Vuex.Store({
7-
state: {},
8-
mutations: {},
9-
actions: {},
8+
modules: {
9+
FAQs,
10+
},
1011
});

src/views/FAQPage/FAQList.vue

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
<template>
2-
<ul class="data-list faq-data-list">
3-
<li
4-
:class="{ isClosed: !item.isOpen }"
5-
:key="index"
6-
class="data-item faq-data-item initBorder"
7-
v-for="(item, index) in dataItems"
8-
>
9-
<TheQuestion :question="item.question" :toggleAccordion="toggleAccordion" />
10-
<TheAnswer :answer="item.answer" />
11-
</li>
12-
</ul>
2+
<div>
3+
<ul :v-if="FAQs.length" class="data-list faq-data-list">
4+
<li
5+
:class="{ isClosed: !item.isOpen }"
6+
:key="index"
7+
class="data-item faq-data-item initBorder"
8+
v-for="(item, index) in FAQs"
9+
>
10+
<TheQuestion :question="item.question" :toggleAccordion="toggleAccordion" />
11+
<TheAnswer :answer="item.answer" />
12+
</li>
13+
</ul>
14+
</div>
1315
</template>
1416

1517
<script>
16-
import fetch from 'node-fetch';
18+
import { mapActions } from 'vuex';
1719
import TheQuestion from './TheQuestion.vue';
1820
import TheAnswer from './TheAnswer.vue';
1921
@@ -23,37 +25,40 @@ export default {
2325
TheQuestion,
2426
TheAnswer,
2527
},
26-
data() {
27-
return {
28-
dataItems: [],
29-
};
30-
},
3128
3229
props: {
3330
question: String,
3431
answer: String,
3532
},
3633
3734
created() {
38-
fetch('https://api-dev.codinggarden.community/faqs')
39-
.then(n => n.json())
40-
.then(json => {
41-
this.dataItems = json.map(n => ({
42-
...n.attributes,
43-
isOpen: false,
44-
}));
45-
});
35+
this.faq();
36+
},
37+
38+
computed: {
39+
FAQs: {
40+
get() {
41+
return this.$store.state.FAQs.FAQs;
42+
},
43+
44+
set(updated) {
45+
this.$store.state.FAQs.FAQs = updated;
46+
},
47+
},
4648
},
4749
4850
methods: {
4951
toggleAccordion(question) {
50-
this.dataItems = this.dataItems.map(e => {
52+
this.FAQs = this.FAQs.map(e => {
5153
if (e.question === question) {
5254
return { ...e, isOpen: !e.isOpen };
5355
}
5456
return { ...e, isOpen: false };
5557
});
5658
},
59+
...mapActions({
60+
faq: 'FAQs/addFAQsAction',
61+
}),
5762
},
5863
};
5964
</script>

0 commit comments

Comments
 (0)