mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-15 18:56:22 +00:00
on Refactor... #2
This commit is contained in:
@@ -2,35 +2,94 @@
|
||||
|
||||
var Reflux = require('reflux')
|
||||
|
||||
var UserStore = require('./UserStore')
|
||||
|
||||
var actions = Reflux.createActions([
|
||||
'updatePlanet',
|
||||
'destroyPlanet',
|
||||
'update',
|
||||
'destroy',
|
||||
'updateCode',
|
||||
'destroyCode',
|
||||
'updateNote',
|
||||
'destroyNote'
|
||||
])
|
||||
|
||||
function deleteItemFromTargetArray (item, targetArray) {
|
||||
targetArray.some(function (_item, index) {
|
||||
if (_item.id === item.id) {
|
||||
targetArray.splice(index, 1)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return targetArray
|
||||
}
|
||||
|
||||
function updateItemToTargetArray (item, targetArray) {
|
||||
var isNew = !targetArray.some(function (_item, index) {
|
||||
if (_item.id === item.id) {
|
||||
targetArray.splice(index, 1, item)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
if (isNew) targetArray.push(item)
|
||||
|
||||
return targetArray
|
||||
}
|
||||
|
||||
module.exports = Reflux.createStore({
|
||||
listenables: [actions],
|
||||
Actions: actions,
|
||||
onUpdatePlanet: function (planet) {
|
||||
onUpdate: function (planet) {
|
||||
// Copy the planet object
|
||||
var aPlanet = Object.assign({}, planet)
|
||||
delete aPlanet.Codes
|
||||
delete aPlanet.Notes
|
||||
|
||||
// Check if the planet should be updated to currentUser
|
||||
var currentUser = JSON.parse(localStorage.getItem('currentUser'))
|
||||
|
||||
var ownedByCurrentUser = currentUser.id === aPlanet.OwnerId
|
||||
|
||||
if (ownedByCurrentUser) {
|
||||
currentUser.Planets = updateItemToTargetArray(aPlanet, currentUser.Planets)
|
||||
}
|
||||
|
||||
if (!ownedByCurrentUser) {
|
||||
var team = null
|
||||
currentUser.Teams.some(function (_team) {
|
||||
if (_team.id === aPlanet.OwnerId) {
|
||||
team = _team
|
||||
return true
|
||||
}
|
||||
return
|
||||
})
|
||||
|
||||
if (team) {
|
||||
team.Planets = updateItemToTargetArray(aPlanet, team.Planets)
|
||||
}
|
||||
}
|
||||
|
||||
// Update currentUser
|
||||
localStorage.setItem('currentUser', JSON.stringify(currentUser))
|
||||
UserStore.Actions.update(currentUser)
|
||||
|
||||
// Update the planet
|
||||
localStorage.setItem('planet-' + planet.id, JSON.stringify(planet))
|
||||
|
||||
this.trigger({
|
||||
status: 'updated',
|
||||
data: planet
|
||||
})
|
||||
},
|
||||
onUpdateCode: function (code) {
|
||||
code.type = 'code'
|
||||
|
||||
var planet = JSON.parse(localStorage.getItem('planet-' + code.PlanetId))
|
||||
if (planet != null) {
|
||||
var isNew = !planet.Codes.some(function (_code, index) {
|
||||
if (code.id === _code.id) {
|
||||
planet.Codes.splice(index, 1, code)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
if (isNew) planet.Codes.unshift(code)
|
||||
planet.Codes = updateItemToTargetArray(code, planet.Codes)
|
||||
|
||||
localStorage.setItem('planet-' + code.PlanetId, JSON.stringify(planet))
|
||||
}
|
||||
@@ -43,16 +102,11 @@ module.exports = Reflux.createStore({
|
||||
onDestroyCode: function (code) {
|
||||
var planet = JSON.parse(localStorage.getItem('planet-' + code.PlanetId))
|
||||
if (planet != null) {
|
||||
planet.Codes.some(function (_code, index) {
|
||||
if (code.id === _code.id) {
|
||||
planet.Codes.splice(index, 1)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
planet.Codes = deleteItemFromTargetArray(code, planet.Codes)
|
||||
|
||||
localStorage.setItem('planet-' + code.PlanetId, JSON.stringify(planet))
|
||||
}
|
||||
code.type = 'code'
|
||||
|
||||
this.trigger({
|
||||
status: 'codeDestroyed',
|
||||
@@ -64,15 +118,7 @@ module.exports = Reflux.createStore({
|
||||
|
||||
var planet = JSON.parse(localStorage.getItem('planet-' + note.PlanetId))
|
||||
if (planet != null) {
|
||||
var isNew = !planet.Notes.some(function (_note, index) {
|
||||
if (note.id === _note.id) {
|
||||
planet.Notes.splice(index, 1, note)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
if (isNew) planet.Codes.unshift(note)
|
||||
planet.Notes = updateItemToTargetArray(note, planet.Notes)
|
||||
|
||||
localStorage.setItem('planet-' + note.PlanetId, JSON.stringify(planet))
|
||||
}
|
||||
@@ -85,16 +131,11 @@ module.exports = Reflux.createStore({
|
||||
onDestroyNote: function (note) {
|
||||
var planet = JSON.parse(localStorage.getItem('planet-' + note.PlanetId))
|
||||
if (planet != null) {
|
||||
planet.Notes.some(function (_note, index) {
|
||||
if (note.id === _note.id) {
|
||||
planet.Notes.splice(index, 1)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
planet.Notes = deleteItemFromTargetArray(note, planet.Notes)
|
||||
|
||||
localStorage.setItem('planet-' + note.PlanetId, JSON.stringify(planet))
|
||||
}
|
||||
note.type = 'note'
|
||||
|
||||
this.trigger({
|
||||
status: 'noteDestroyed',
|
||||
|
||||
Reference in New Issue
Block a user