123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import Vuex from 'vuex'
- const createStore = () => {
- return new Vuex.Store({
- state: {
- headlines: [],
- loading: false,
- category: '',
- token: '',
- country: 'us'
- },
- mutations: {
- setHeadLines (state, headlines) {
- state.headlines = headlines
- },
- setLoading (state, loading) {
- state.loading = loading
- },
- setCategory (state, category) {
- state.category = category
- },
- setCountry (state, country) {
- state.country = country
- },
- setToken (state, token) {
- state.token = token
- }
- },
- actions: {
- async loadHeadLines ({ commit }, apiUrl) {
- commit('setLoading', true)
- const { articles } = await this.$axios.$get(apiUrl)
- commit('setLoading', false)
- commit('setHeadLines', articles)
- },
- async authenticateUser ({ commit }, userPayload) {
- try {
- commit('setLoading', true)
- const autuUserData = await this.$axios.$post(
- '/register/',
- userPayload
- )
- commit('setToken', autuUserData.idToken)
- commit('setLoading', false)
- } catch (error) {
- console.log(error)
- }
- }
- },
- getters: {
- headlines: state => state.headlines,
- loading: state => state.loading,
- category: state => state.category,
- country: state => state.country,
- isAuthenticated: state => !!state.token
- }
- })
- }
- export default createStore
|