Files: 52acefb26cfdc545a5bef98c649d3cfbec4ac257 / app / page / settings.js
2493 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | |
4 | exports.gives = nest('app.page.settings') |
5 | |
6 | exports.needs = nest({ |
7 | 'about.html.image': 'first', |
8 | 'about.obs.name': 'first', |
9 | 'history.sync.push': 'first', |
10 | 'history.obs.history': 'first', |
11 | 'keys.sync.id': 'first', |
12 | 'settings.sync.get': 'first', |
13 | 'settings.sync.set': 'first', |
14 | 'settings.obs.get': 'first', |
15 | 'translations.sync.strings': 'first', |
16 | }) |
17 | |
18 | const LANGUAGES = ['zh', 'en'] |
19 | |
20 | // TODO - this needs moving somewhere upstream |
21 | // const DEFAULT_SETTINGS = { |
22 | // onboarded: false, |
23 | // language: 'zh' |
24 | // } |
25 | |
26 | exports.create = (api) => { |
27 | return nest('app.page.settings', settings) |
28 | |
29 | function settings (location) { |
30 | |
31 | // RESET the app when the settings are changed |
32 | api.settings.obs.get('language')(() => { |
33 | console.log('language changed, resetting view') |
34 | |
35 | api.history.obs.history().set([]) // wipe nav cache |
36 | api.history.sync.push({page: 'home'}) // queue up basic pages |
37 | api.history.sync.push({page: 'settings'}) |
38 | }) |
39 | |
40 | const feed = api.keys.sync.id() |
41 | const strings = api.translations.sync.strings() |
42 | const currentLanguage = api.settings.sync.get('language') |
43 | |
44 | const editProfile = () => api.history.sync.push({ |
45 | page:'userEdit', |
46 | feed, |
47 | callback: (err, didEdit) => { |
48 | if (err) throw new Error ('Error editing profile', err) |
49 | api.history.sync.push({ page: 'settings' }) |
50 | } |
51 | }) |
52 | |
53 | return h('Page -settings', [ |
54 | h('div.content', [ |
55 | h('h1', strings.settingsPage.title), |
56 | h('section -profile', [ |
57 | h('header', strings.settingsPage.section.profile), |
58 | h('div.profile', [ |
59 | h('div.name', api.about.obs.name(feed)), |
60 | api.about.html.image(feed), |
61 | ]), |
62 | h('div.actions', [ |
63 | h('Button', { 'ev-click': editProfile }, [ |
64 | strings.settingsPage.action.edit, |
65 | h('i.fa.fa-pencil') |
66 | ]) |
67 | ]) |
68 | ]), |
69 | h('section -language', [ |
70 | h('header', strings.settingsPage.section.language), |
71 | h('div.languages', LANGUAGES.map(Language)) |
72 | ]) |
73 | ]) |
74 | ]) |
75 | |
76 | function Language (lang) { |
77 | const selectLang = () => api.settings.sync.set({ language: lang }) |
78 | const className = currentLanguage === lang ? '-primary' : '' |
79 | |
80 | return h('Button -language', |
81 | { |
82 | 'ev-click': () => selectLang(lang), |
83 | className |
84 | }, |
85 | lang |
86 | ) |
87 | } |
88 | |
89 | } |
90 | } |
91 | |
92 |
Built with git-ssb-web