refs #850 Replace Settings with typescript

This commit is contained in:
AkiraFukushima 2019-04-04 23:52:08 +09:00
parent 3971e56126
commit 0edac701ae
7 changed files with 205 additions and 142 deletions

View File

@ -1,18 +0,0 @@
import General from './Settings/General'
import Timeline from './Settings/Timeline'
export default {
namespaced: true,
modules: {
General,
Timeline
},
state: {
accountID: null
},
mutations: {
changeAccountID (state, id) {
state.accountID = id
}
}
}

View File

@ -0,0 +1,34 @@
import General from './Settings/General'
import Timeline from './Settings/Timeline'
import { Module, MutationTree } from 'vuex'
export interface SettingsState {
accountID: number | null
}
const state = (): SettingsState => ({
accountID: null
})
export const MUTATION_TYPES = {
CHANGE_ACCOUNT_ID: 'changeAccountID'
}
const mutations: MutationTree<SettingsState> = {
[MUTATION_TYPES.CHANGE_ACCOUNT_ID]: (state, id: number) => {
state.accountID = id
}
}
// TODO: use type of rootState
const Settings: Module<SettingsState, any> = {
namespaced: true,
modules: {
General,
Timeline
},
state: state,
mutations: mutations
}
export default Settings

View File

@ -1,68 +0,0 @@
import Mastodon from 'megalodon'
import Visibility from '~/src/constants/visibility'
export default {
namespaced: true,
state: {
visibility: Visibility.Public.value,
sensitive: false
},
mutations: {
changeVisibility (state, value) {
state.visibility = value
},
changeSensitive (state, value) {
state.sensitive = value
}
},
actions: {
fetchSettings ({ commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/accounts/verify_credentials')
.then(res => {
const visibility = Object.values(Visibility).find((v) => {
return v.key === res.data.source.privacy
})
commit('changeVisibility', visibility.value)
commit('changeSensitive', res.data.source.sensitive)
return res.data
})
},
setVisibility ({ commit, rootState }, value) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const visibility = Object.values(Visibility).find((v) => {
return v.value === value
})
return client.patch('/accounts/update_credentials', {
source: {
privacy: visibility.key
}
})
.then(res => {
commit('changeVisibility', visibility.value)
return res.data
})
},
setSensitive ({ commit, rootState }, value) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.patch('/accounts/update_credentials', {
source: {
sensitive: value
}
})
.then(res => {
commit('changeSensitive', value)
return res.data
})
}
}
}

View File

@ -0,0 +1,84 @@
import Mastodon, { Account } from 'megalodon'
import Visibilities from '~/src/constants/visibility'
import { Module, MutationTree, ActionTree } from 'vuex'
import Visibility from '~/src/types/visibility'
export interface GeneralState {
visibility: number,
sensitive: boolean
}
const state = (): GeneralState => ({
visibility: Visibilities.Public.value,
sensitive: false
})
export const MUTATION_TYPES = {
CHANGE_VISIBILITY: 'changeVisibility',
CHANGE_SENSITIVE: 'changeSensitive'
}
const mutations: MutationTree<GeneralState> = {
[MUTATION_TYPES.CHANGE_VISIBILITY]: (state, value: number) => {
state.visibility = value
},
[MUTATION_TYPES.CHANGE_SENSITIVE]: (state, value: boolean) => {
state.sensitive = value
}
}
// TODO: use type of rootState
const actions: ActionTree<GeneralState, any> = {
fetchSettings: async ({ commit, rootState }): Promise<Account> => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res = await client.get<Account>('/accounts/verify_credentials')
const visibility: Visibility | undefined = Object.values(Visibilities as Array<Visibility>).find((v) => {
return v.key === res.data.source!.privacy
})
commit(MUTATION_TYPES.CHANGE_VISIBILITY, visibility!.value)
commit(MUTATION_TYPES.CHANGE_SENSITIVE, res.data.source!.sensitive)
return res.data
},
setVisibility: async ({ commit, rootState }, value: number) => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const visibility = Object.values(Visibilities as Array<Visibility>).find((v) => {
return v.value === value
})
const res = await client.patch<Account>('/accounts/update_credentials', {
source: {
privacy: visibility!.key
}
})
commit(MUTATION_TYPES.CHANGE_VISIBILITY, visibility!.value)
return res.data
},
setSensitive: async ({ commit, rootState }, value: boolean) => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res = await client.patch<Account>('/accounts/update_credentials', {
source: {
sensitive: value
}
})
commit(MUTATION_TYPES.CHANGE_SENSITIVE, value)
return res.data
}
}
// TODO: use type of rootState
const General: Module<GeneralState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default General

View File

@ -1,56 +0,0 @@
import { ipcRenderer } from 'electron'
import unreadSettings from '~/src/constants/unreadNotification'
export default {
namespaced: true,
state: {
unreadNotification: {
direct: unreadSettings.Direct.default,
local: unreadSettings.Local.default,
public: unreadSettings.Public.default
}
},
mutations: {
updateUnreadNotification (state, settings) {
state.unreadNotification = settings
}
},
actions: {
loadUnreadNotification ({ commit, rootState }) {
return new Promise(resolve => {
ipcRenderer.once('response-get-unread-notification', (_, settings) => {
ipcRenderer.removeAllListeners('error-get-unread-notification')
commit('updateUnreadNotification', settings)
resolve(settings)
})
ipcRenderer.once('error-get-unread-notification', () => {
ipcRenderer.removeAllListeners('response-get-unread-notification')
commit('updateUnreadNotification', {
direct: unreadSettings.Direct.default,
local: unreadSettings.Local.default,
public: unreadSettings.Public.default
})
resolve(null)
})
ipcRenderer.send('get-unread-notification', rootState.Settings.accountID)
})
},
changeUnreadNotification ({ dispatch, state, rootState }, timeline) {
const settings = Object.assign({}, state.unreadNotification, timeline, {
accountID: rootState.Settings.accountID
})
return new Promise((resolve, reject) => {
ipcRenderer.once('response-update-unread-notification', () => {
ipcRenderer.removeAllListeners('error-update-unread-notification')
dispatch('loadUnreadNotification')
resolve(settings)
})
ipcRenderer.once('error-update-unread-notification', (_, err) => {
ipcRenderer.removeAllListeners('response-update-unread-notification')
reject(err)
})
ipcRenderer.send('update-unread-notification', settings)
})
}
}
}

View File

@ -0,0 +1,82 @@
import { ipcRenderer } from 'electron'
import unreadSettings from '~/src/constants/unreadNotification'
import { Module, MutationTree, ActionTree } from 'vuex'
interface UnreadNotification {
direct: boolean,
local: boolean,
public: boolean
}
export interface TimelineState {
unreadNotification: UnreadNotification
}
const state = (): TimelineState => ({
unreadNotification: {
direct: unreadSettings.Direct.default,
local: unreadSettings.Local.default,
public: unreadSettings.Public.default
}
})
export const MUTATION_TYPES = {
UPDATE_UNREAD_NOTIFICATION: 'updateUnreadNotification'
}
const mutations: MutationTree<TimelineState> = {
[MUTATION_TYPES.UPDATE_UNREAD_NOTIFICATION]: (state, settings: UnreadNotification) => {
state.unreadNotification = settings
}
}
// TODO: use type of rootState
const actions: ActionTree<TimelineState, any> = {
loadUnreadNotification: ({ commit, rootState }): Promise<boolean> => {
return new Promise(resolve => {
ipcRenderer.once('response-get-unread-notification', (_, settings: UnreadNotification) => {
ipcRenderer.removeAllListeners('error-get-unread-notification')
commit(MUTATION_TYPES.UPDATE_UNREAD_NOTIFICATION, settings)
resolve(true)
})
ipcRenderer.once('error-get-unread-notification', () => {
ipcRenderer.removeAllListeners('response-get-unread-notification')
const settings: UnreadNotification = {
direct: unreadSettings.Direct.default,
local: unreadSettings.Local.default,
public: unreadSettings.Public.default
}
commit(MUTATION_TYPES.UPDATE_UNREAD_NOTIFICATION, settings)
resolve(false)
})
ipcRenderer.send('get-unread-notification', rootState.Settings.accountID)
})
},
changeUnreadNotification: ({ dispatch, state, rootState }, timeline: object): Promise<boolean> => {
const settings = Object.assign({}, state.unreadNotification, timeline, {
accountID: rootState.Settings.accountID
})
return new Promise((resolve, reject) => {
ipcRenderer.once('response-update-unread-notification', () => {
ipcRenderer.removeAllListeners('error-update-unread-notification')
dispatch('loadUnreadNotification')
resolve(true)
})
ipcRenderer.once('error-update-unread-notification', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-update-unread-notification')
reject(err)
})
ipcRenderer.send('update-unread-notification', settings)
})
}
}
// Todo: use type of rootState
const Timeline: Module<TimelineState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Timeline

5
src/types/visibility.ts Normal file
View File

@ -0,0 +1,5 @@
export default interface Visibility {
name: string,
value: number,
key: string
}