Created
February 11, 2020 14:31
-
-
Save jonathanmach/c9ed31639b44921e54049f266b0331b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-unused-vars */ | |
import HabitService from "@/services/HabitService.js"; | |
import Habit from "@/models/Habit"; | |
import Category from "../../models/Category"; | |
export default { | |
mutations: { | |
FETCH_HABITS(state, habitArray) { | |
// Test data | |
const data = [ | |
{ id: 1, title: "Morning", habit_order: [4, 3, 5] }, | |
{ id: 2, title: "Daily", habit_order: [6, 8, 7] } | |
] | |
Category.insert({ | |
data: data | |
}); | |
Habit.insert({ data: habitArray }); | |
}, | |
}, | |
actions: { | |
fetchHabits({ commit }) { | |
HabitService.fetchAllHabits().then(response => { | |
let habitArray = response.data; | |
commit("FETCH_HABITS", habitArray); | |
}); | |
}, | |
async addHabit({ commit }, habitObj) { | |
habitObj.category = 1 // @todo | |
// 1. Update the backend and get habit id (primary key) | |
const response = await HabitService.insert(habitObj) | |
const habitId = response.data.id | |
// 2. Update the local store @todo use mutations | |
habitObj.id = habitId | |
Habit.insert({ data: habitObj }) | |
// Important: add created habit to the category.habit_order array | |
let category = Category.find(habitObj.category) | |
category.habit_order.push(habitObj.id) | |
} | |
}, | |
getters: { | |
habitDetails: state => habitId => { | |
return Habit.query() | |
.with("logs") | |
.whereId(habitId) | |
.get(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment