Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add firebase auth and database support
- Add router navigation guard to redirect unauthenticated user to `/login` page - Use **vuexfire** to integrate firebase database with vuex - Initialize firebase and vuex state when `<App>` is created
- Loading branch information
Showing
5 changed files
with
74 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,70 @@ | ||
import Vue from 'vue' | ||
import Vuex, { Store } from 'vuex' | ||
import { desc, uuid } from './utils' | ||
import { firebaseMutations, firebaseAction } from 'vuexfire' | ||
|
||
Vue.use(Vuex) | ||
|
||
const CREATE = 'CREATE' | ||
const UPDATE = 'UPDATE' | ||
|
||
function readFromLocalStorage(key, defaultValue) { | ||
const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/ | ||
|
||
function reviver(key, value) { | ||
if (typeof value === 'string' && dateFormat.test(value)) { | ||
return new Date(value) | ||
} | ||
|
||
return value | ||
} | ||
|
||
const value = localStorage.getItem(key) | ||
|
||
if (!value) return defaultValue | ||
import { db } from './firebase' | ||
import { uuid, desc } from './utils' | ||
|
||
try { | ||
return JSON.parse(value, reviver) | ||
} catch (e) { | ||
localStorage.removeItem(key) | ||
} | ||
Vue.use(Vuex) | ||
|
||
return defaultValue | ||
} | ||
const notesRef = db.ref('notes') | ||
|
||
const getters = { | ||
noteById: ({ notes }) => id => notes.find(it => it.id === id), | ||
notesSortedByLastUpdated: ({ notes }) => notes.slice().sort((a, b) => desc(a.updatedAt, b.updatedAt)) | ||
} | ||
|
||
const state = () => ({ | ||
notes: readFromLocalStorage('notebook', []) | ||
notes: [] | ||
}) | ||
|
||
const mutations = { | ||
[CREATE]: (state, note) => { state.notes.push(note) }, | ||
[UPDATE]: (state, [old, note]) => { state.notes.splice(state.notes.indexOf(old), 1, note) } | ||
} | ||
|
||
const actions = { | ||
async save({ dispatch }, payload) { | ||
if (payload.id) return dispatch('update', payload) | ||
init: firebaseAction(({ bindFirebaseRef }) => bindFirebaseRef('notes', notesRef)), | ||
|
||
return dispatch('create', payload) | ||
async save({ dispatch }, payload) { | ||
return await dispatch(payload.id ? 'update' : 'create', payload) | ||
}, | ||
|
||
async create({ commit }, { title, body }) { | ||
const timestamp = new Date() | ||
const note = { | ||
async create(_, { title, body }) { | ||
const newNoteRef = notesRef.push() | ||
const timestamp = Date.now() | ||
const payload = { | ||
id: uuid(), | ||
title, | ||
body, | ||
createdAt: timestamp, | ||
updatedAt: timestamp | ||
} | ||
|
||
commit(CREATE, note) | ||
await newNoteRef.set(payload) | ||
|
||
return note | ||
return payload | ||
}, | ||
|
||
async update({ commit, getters }, { id, title, body }) { | ||
const note = getters.noteById(id) | ||
async update(_, item) { | ||
const note = getters.noteById(item.id) | ||
|
||
if (!note) throw Error('Cannot update non-existing note.') | ||
await notesRef.child(note['.key']).update({ | ||
updatedAt: Date.now(), | ||
...item | ||
}) | ||
|
||
const payload = { | ||
...note, | ||
id, | ||
title, | ||
body, | ||
updatedAt: new Date() | ||
} | ||
|
||
commit(UPDATE, [note, payload]) | ||
return note | ||
}, | ||
|
||
return payload | ||
async remove(_, item) { | ||
await notesRef.child(item['.key']).remove() | ||
} | ||
} | ||
|
||
const store = new Store({ | ||
const mutations = { | ||
...firebaseMutations | ||
} | ||
|
||
export default new Store({ | ||
getters, | ||
state, | ||
mutations, | ||
actions | ||
}) | ||
|
||
store.subscribe((mutation, state) => { | ||
if (mutation.type === CREATE || mutation.type === UPDATE) { | ||
localStorage.setItem('notebook', JSON.stringify(state.notes)) | ||
} | ||
}) | ||
|
||
export default store |
This file contains 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
This file contains 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