Skip to content

Commit

Permalink
no localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Jan 11, 2024
1 parent ba1af61 commit edb41d4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
15 changes: 15 additions & 0 deletions client/webserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,21 @@ func (s *WebServer) apiLogin(w http.ResponseWriter, r *http.Request) {
}, s.indent)
}

func (s *WebServer) apiNotes(w http.ResponseWriter, r *http.Request) {
notes, err := s.core.Notifications(100)
if err != nil {
log.Errorf("failed to get notifications: %v", err)
}

writeJSON(w, &struct {
OK bool `json:"ok"`
Notes []*db.Notification `json:"notes"`
}{
OK: true,
Notes: notes,
}, s.indent)
}

// apiLogout handles the 'logout' API request.
func (s *WebServer) apiLogout(w http.ResponseWriter, r *http.Request) {
err := s.core.Logout()
Expand Down
38 changes: 12 additions & 26 deletions client/webserver/site/src/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ export default class Application {
this.attachHeader()
this.attachCommon(this.header)
this.attach({})
// If we are authed, populate notes, otherwise get we'll them from the login
// response.
if (this.user && this.user.authed) await this.fetchNotes()
this.updateMenuItemsDisplay()
// initialize browser notifications
ntfn.fetchBrowserNtfnSettings()
Expand Down Expand Up @@ -363,7 +366,6 @@ export default class Application {
}
this.setNoteTimes(page.noteList)
this.setNoteTimes(page.pokeList)
this.storeNotes()
})

bind(page.burgerIcon, 'click', () => {
Expand Down Expand Up @@ -463,23 +465,6 @@ export default class Application {
})
}

/*
* storeNotes stores the list of notifications in Window.localStorage. The
* actual stored list is stripped of information not necessary for display.
*/
storeNotes () {
State.storeLocal(State.notificationsLK, this.notes.map(n => {
return {
subject: n.subject,
details: n.details,
severity: n.severity,
stamp: n.stamp,
id: n.id,
acked: n.acked
}
}))
}

/*
* updateMenuItemsDisplay should be called when the user has signed in or out,
* and when the user registers a DEX.
Expand All @@ -497,12 +482,15 @@ export default class Application {
Doc.hide(page.noteBell, page.walletsMenuEntry, page.marketsMenuEntry)
return
}

page.profileBox.classList.add('authed')
Doc.show(page.noteBell, page.walletsMenuEntry, page.marketsMenuEntry)
}

// Load recent notifications from Window.localStorage.
const notes = State.fetchLocal(State.notificationsLK)
this.setNotes(notes || [])
async fetchNotes () {
const res = await getJSON('/api/notes')
if (!this.checkResponse(res)) return console.error('failed to fetch notes:', res?.msg || String(res))
this.setNotes(res.notes)
}

/* attachCommon scans the provided node and handles some common bindings. */
Expand Down Expand Up @@ -553,9 +541,8 @@ export default class Application {
this.notes = []
Doc.empty(this.page.noteList)
for (let i = 0; i < notes.length; i++) {
this.prependNoteElement(notes[i], true)
this.prependNoteElement(notes[i])
}
this.storeNotes()
}

updateUser (note: CoreNote) {
Expand Down Expand Up @@ -758,14 +745,13 @@ export default class Application {
this.prependListElement(this.page.pokeList, note, el)
}

prependNoteElement (cn: CoreNote, skipSave?: boolean) {
prependNoteElement (cn: CoreNote) {
const [el, note] = this.makeNote(cn)
this.notes.push(note)
while (this.notes.length > noteCacheSize) this.notes.shift()
const noteList = this.page.noteList
this.prependListElement(noteList, note, el)
this.bindUrlHandlers(el)
if (!skipSave) this.storeNotes()
// Set the indicator color.
if (this.notes.length === 0 || (Doc.isDisplayed(this.page.noteBox) && Doc.isDisplayed(noteList))) return
let unacked = 0
Expand Down Expand Up @@ -971,7 +957,7 @@ export default class Application {
}
State.removeCookie(State.authCK)
State.removeCookie(State.pwKeyCK)
State.removeLocal(State.notificationsLK)
State.removeLocal(State.notificationsLK) // Notification storage was DEPRECATED pre-v1.
window.location.href = '/login'
}

Expand Down
1 change: 0 additions & 1 deletion client/webserver/site/src/js/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,6 @@ export interface Application {
ackNotes (): void
setNoteTimes (noteList: HTMLElement): void
bindInternalNavigation (ancestor: HTMLElement): void
storeNotes (): void
updateMenuItemsDisplay (): void
attachCommon (node: HTMLElement): void
updateBondConfs (dexAddr: string, coinID: string, confs: number, assetID: number): void
Expand Down
2 changes: 1 addition & 1 deletion client/webserver/site/src/js/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class State {
static optionsExpansionLK = 'mmOptsExpand'
static leftMarketDockLK = 'leftmarketdock'
static selectedAssetLK = 'selectedasset'
static notificationsLK = 'notifications'
static notificationsLK = 'notifications' // DEPRECATED before v1
static orderDisclaimerAckedLK = 'ordAck'
static lastCandleDurationLK = 'lastCandleDuration'

Expand Down
1 change: 1 addition & 0 deletions client/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func New(cfg *Config) (*WebServer, error) {
r.Group(func(apiAuth chi.Router) {
apiAuth.Use(s.rejectUnauthed)
apiAuth.Get("/user", s.apiUser)
apiAuth.Get("/notes", s.apiNotes)
apiAuth.Post("/defaultwalletcfg", s.apiDefaultWalletCfg)
apiAuth.Post("/register", s.apiRegister)
apiAuth.Post("/postbond", s.apiPostBond)
Expand Down

0 comments on commit edb41d4

Please sign in to comment.