This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
/
currentGroup.js
257 lines (230 loc) · 8.96 KB
/
currentGroup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import groups from '@/group/api/groups'
import {
createMetaModule,
createRouteRedirect,
metaStatusesWithId,
withMeta,
withPrefixedIdMeta,
} from '@/utils/datastore/helpers'
import { extend } from 'quasar'
import i18n from '@/base/i18n'
import { messages as loadMessages } from '@/locales/index'
function initialState () {
return {
current: null,
id: null,
}
}
export default {
namespaced: true,
modules: { meta: createMetaModule() },
state: initialState(),
getters: {
value: (state, getters) => getters.enrich(state.current),
enrich: (state, getters, rootState, rootGetters) => group => {
if (!group) return
const isPlayground = group.status === 'playground'
return {
...group,
isPlayground,
isBikeKitchen: group.theme === 'bikekitchen',
isGeneralPurpose: group.theme === 'general',
hasPhoto: group.photoUrls && group.photoUrls.fullSize,
hasLocation: group.latitude && group.longitude,
membership: getters.membership,
memberships: getters.memberships,
activeAgreement: getters.activeAgreement,
awaitingAgreement: !!(getters.activeAgreement && getters.activeAgreement.agreed === false),
}
},
memberships: (state, getters, rootState, rootGetters) => {
const group = state.current
if (!group || !group.memberships) return []
return Object.entries(group.memberships).reduce((obj, [userId, membership]) => {
// cannot enrich trustedBy and addedBy, as it would create the cyclic dependency "user -> group -> user"
const authUserId = rootGetters['auth/userId']
const isEditor = membership.roles.includes('editor')
const { trustThresholdForNewcomer } = state.current
obj[userId] = {
...membership,
isEditor,
trusted: membership.trustedBy.includes(authUserId),
trustProgress: isEditor ? 1 : membership.trustedBy.length / trustThresholdForNewcomer,
trustThresholdForNewcomer,
...metaStatusesWithId(getters, ['trustUser', 'revokeTrust'], parseInt(userId)),
}
return obj
}, {})
},
agreement: (state, getters, rootState, rootGetters) => state.current && rootGetters['agreements/get'](state.current.activeAgreement),
conversation: (state, getters, rootState, rootGetters) => {
if (!state.current) return
return rootGetters['conversations/getForGroup'](state.current.id)
},
id: (state) => state.id,
// for current user:
membership: (state, getters, rootState, rootGetters) => getters.memberships[rootGetters['auth/userId']],
roles: (state, getters) => getters.membership ? getters.value.membership.roles : [],
features: state => state.current && state.current.features ? state.current.features : [],
theme: state => state.current && state.current.theme,
isEditor: (state, getters) => getters.roles.includes('editor'),
isBikeKitchen: (state, getters) => Boolean(getters.value && getters.value.isBikeKitchen),
isGeneralPurpose: (state, getters) => Boolean(getters.value && getters.value.isGeneralPurpose),
getNotificationTypeStatus: (state, getters) => notificationType => getters['meta/status']('changeNotificationType', notificationType),
},
actions: {
...withMeta({
async fetch ({ state, commit, dispatch }, groupId) {
commit('setId', groupId)
const group = await groups.get(groupId)
// aborting, another group has been loaded while we waited
if (state.id !== groupId) return
if (group.activeAgreement) {
dispatch('agreements/fetch', group.activeAgreement, { root: true })
}
commit('set', group)
},
async markUserActive ({ getters }) {
/**
* Marks the user as active in the current group
* Should only be triggered when the user visits a group page
* It currently also gets triggered when the user visits the profile page, but that seems fine.
*/
if (getters.id) await groups.throttledMarkUserActive(getters.id)
},
async trustUser ({ getters }, userId) {
if (getters.id) {
await groups.trustUser(getters.id, userId)
}
},
async revokeTrust ({ getters }, userId) {
if (getters.id) {
await groups.revokeTrust(getters.id, userId)
}
},
}),
...withMeta({
async changeNotificationType ({ dispatch, getters }, { notificationType, enabled }) {
if (enabled) {
await groups.addNotificationType(getters.id, notificationType)
}
else {
await groups.removeNotificationType(getters.id, notificationType)
}
dispatch('toasts/show', {
message: 'NOTIFICATIONS.CHANGES_SAVED',
config: {
timeout: 2000,
icon: 'thumb_up',
},
}, { root: true })
},
}, {
findId: ({ notificationType }) => notificationType,
}),
...withPrefixedIdMeta('agreements/', {
async agreementSave ({ commit, dispatch, state, getters }, agreement) {
const { id } = agreement
if (id) {
agreement = await dispatch('agreements/save', agreement, { root: true })
}
else {
agreement = await dispatch('agreements/create', { ...agreement, group: getters.id }, { root: true })
}
if (state.current.activeAgreement !== agreement.id) {
commit('set', await groups.save({ id: getters.id, activeAgreement: agreement.id }))
}
},
async agreementReplace ({ commit, dispatch, state, getters }, agreement) {
agreement = await dispatch('agreements/create', { ...agreement, group: getters.id }, { root: true })
commit('set', await groups.save({ id: getters.id, activeAgreement: agreement.id }))
},
async agreementRemove ({ commit, dispatch, state, getters }) {
commit('set', await groups.save({ id: getters.id, activeAgreement: null }))
},
}),
...withMeta({
async select ({ dispatch, getters, rootGetters }, { groupId }) {
if (!groupId) throw createRouteRedirect({ name: 'groupsGallery' })
await dispatch('fetch', groupId)
const hasError = getters['meta/status']('fetch', groupId).hasValidationErrors
if (hasError) {
const groupExists = Boolean(rootGetters['groups/get'](groupId))
if (groupExists) {
dispatch('toasts/show', {
message: 'GROUP.NONMEMBER_REDIRECT',
config: {
icon: 'warning',
color: 'negative',
},
}, { root: true })
}
throw createRouteRedirect({ name: 'groupPreview', params: { groupPreviewId: groupId } })
}
dispatch('auth/maybeBackgroundSave', { currentGroup: groupId }, { root: true })
},
}, {
findId: ({ groupId }) => groupId,
setCurrentId: ({ commit }, { groupId }) => commit('setId', groupId),
getCurrentId: ({ state }) => state.id,
}),
selectFromCurrentUser ({ dispatch, getters, rootGetters }) {
const selected = getters.id
const groupId = rootGetters['auth/user'].currentGroup
if (!selected && groupId) {
dispatch('select', { groupId })
}
},
maybeUpdate ({ getters, commit }, group) {
if (!getters.id) return
// update group values, do not replace group
if (group.id === getters.id) {
commit('set', group)
}
},
refresh ({ state, dispatch }) {
if (state.current) {
dispatch('fetch', state.current.id)
}
},
},
mutations: {
setId (state, value) {
state.id = value
},
set (state, group) {
state.current = Object.freeze(group)
},
clear (state) {
Object.assign(state, initialState())
},
},
}
export function plugin (datastore) {
// clear group when logged out
datastore.watch((state, getters) => getters['auth/isLoggedIn'], isLoggedIn => {
if (!isLoggedIn) {
datastore.commit('currentGroup/clear')
datastore.commit('currentGroup/meta/clear')
}
})
datastore.watch(
(state, getters) => [getters['currentGroup/isBikeKitchen'], getters['currentGroup/isGeneralPurpose'], getters['i18n/locale']],
async ([isBikeKitchen, isGeneralPurpose, locale] = []) => {
if (!locale) return
if (isBikeKitchen || isGeneralPurpose) {
const generalPurposeMessages = await import('@/locales/generalPurpose.json')
const messages = await loadMessages(locale)
if (!(datastore.getters['currentGroup/isBikeKitchen'] || datastore.getters['currentGroup/isGeneralPurpose'])) return
const mergedMessages = extend(true, {}, messages, generalPurposeMessages)
i18n.setLocaleMessage(locale, mergedMessages)
}
else {
const messages = await loadMessages(locale)
if (datastore.getters['currentGroup/isBikeKitchen'] || datastore.getters['currentGroup/isGeneralPurpose']) return
i18n.setLocaleMessage(locale, messages)
}
},
{ immediate: true },
)
}