Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | // Use the moment-range addon import groupBy from 'lodash.groupby' import { parseISO, parse, isValid, formatISO } from 'date-fns' import uniqBy from 'lodash.uniqby' import debounce from 'lodash.debounce' import Vue from 'vue' import { getMutations } from './index' export const state = () => ({ settings: [], verboseChoices: { LIGHT: 'Clair', DARK: 'Sombre', AUTO: 'Automatique', }, }) export const getters = { all: (state, getters) => state.settings, fromCategory: (state, getters) => category => state.settings.filter(s => s.category === category), one: (state, getters) => (propval, prop = 'key') => state.settings.find(o => o[prop] === propval) || null, value: (state, getters) => (propval, fallback = null, prop = 'key') => { const setting = getters.one(propval, prop) if (setting === null) if (fallback !== null) return fallback else { // eslint-disable-next-line console.error( `No setting with ${prop}=${propval}. Available ${prop}s: ${getters.all.map( o => o[prop] )}` ) return } return setting.value }, group: () => (settings, { removeHidden }) => { /* Groups `settings` by category. * `removeHidden`: when set to true, categories that match the regex pattern below * get filtered out of the returned array: * ^__.+__$ */ if (removeHidden) settings = settings.filter(o => !o.category.match(/^__.+__$/)) settings = groupBy(settings, 'category') return [...Object.entries(settings)] }, grouped: (_, { group, all }) => group(all, { removeHidden: true }), userHasSetting: (_, { one }) => (propval, prop = 'key') => { const setting = one(propval, prop) return setting ? !setting.isDefaultValue : null }, } export const mutations = { SET: (state, { definitions, settings }) => { /* Set settings for the user, by combining a setting definition (SettinDefinition in the backend) * and a value (Setting.value in the backend) */ let hydratedSettings = [] definitions.forEach(definition => { let value, uuid let { choices } = definition const { category } = definition // Attempt to get the user's value for this setting definition const setting = settings.find(o => o.setting.key === definition.key) // Set `value` property: if setting is undefined, fallback to the default // value, else use the one on the found setting const isDefaultValue = setting === undefined if (isDefaultValue) { value = definition.default uuid = null } else { value = setting.value uuid = setting.uuid } const rawValue = value // Convert to the appropriate JS representation of the value value = parsedValue(value, definition) if (choices) { choices = choices.split(',') } const hidden = category.startsWith('__') && category.endsWith('__') /* Adds the definition to the state as a setting object + the value prop * and a bool to tell if the setting is set to the default value. */ const hydratedDefinition = { ...definition, choices, value, isDefaultValue, rawValue, uuid, hidden, } hydratedSettings.push(hydratedDefinition) }) // Remove duplicate settings (by key) hydratedSettings = uniqBy(hydratedSettings, 'key') // Replace state's settings Vue.set(state, 'settings', hydratedSettings) }, // eslint-disable-next-line ...getMutations( 'setting', o => ({ ...o, value: parsedValue(o.value, o) }), true, ['del', 'add', 'patch'], 'key', true ), // TODO validator: customConstraints from definitions // eg: if (definition.required) /* check if setting is not empty */ else return true // eg2: try { parseValue(definition.type, setting.value) } catch (e) { return false } } export const actions = { async fetchSettings({ commit }) { try { const { data } = await this.$axios.get(`/settings/`) // console.log(`[from API] GET /settings/: OK`) return data } catch (error) { // eslint-disable-next-line console.error(error) return null } }, async fetchDefinitions({ commit }) { try { const { data } = await this.$axios.get(`/settings-definitions/`) // console.log(`[from API] GET /settings-definitions/: OK`) return data } catch (error) { // eslint-disable-next-line console.error(error) return null } }, async load({ commit, state, dispatch }, force = false) { /* Computes the settings that should be used (state.settings) * from definitions and 'raw' settings returned directly by the API in fetchSettings */ if (!force && state.settings.length) return const definitions = (await dispatch('fetchDefinitions')) || [] const settings = (await dispatch('fetchSettings')) || [] commit('SET', { definitions, settings }) }, async post({ commit }, { setting, early }) { early = early || false setting = { ...setting, value: stringifiedValue(setting.value), user: this.$auth.user.id, } if (early) commit('PATCH', { modifications: setting }) const res = await this.$axios.post('/settings/', setting) const { data } = await this.$axios.get(`/settings/${res.data.setting}/`) if (data) commit('PATCH', { modifications: data }) // console.log(`[from API] POST /settings/: OK`) }, async patch({ commit, getters }, { key, modifications, early }) { early = early || false const type = getters.one(key).type if (Object.prototype.hasOwnProperty.call(modifications, 'value')) { modifications.value = stringifiedValue({ value: modifications.value, type, }) } if (early) { commit('PATCH', { pk: key, modifications }) } const res = await this.$axios.patch(`/settings/${key}/`, modifications) const { data } = await this.$axios.get(`/settings/${res.data.setting}/`) if (data) commit('PATCH', { pk: key, modifications: data }) }, async delete({ commit }, key) { try { const { data } = await this.$axios.delete(`/settings/${key}`) if (data) commit('DEL', key) } catch (error) { // eslint-disable-next-line console.error(error) } }, async userHasSetting(_, { key }) { try { await this.$axios.get(`/settings/${key}/`) return true } catch (error) { return false } }, setValue: debounce( async function({ getters, dispatch }, { key, value, force, early }) { force = force || false early = early === undefined ? true : early try { // Check if the value is coherent with the setting's type // TODO: put this into a checkValueType function if (!force) { const type = getters.one(key).type let typeIsCorrect = true switch (type) { case 'DATE': typeIsCorrect = isValid(value) break default: break } if (!typeIsCorrect) { console.error( `settings#setValue: ${JSON.stringify(value)} should be a ${type}` ) } } // User already has that setting if (await dispatch('userHasSetting', { key })) { await dispatch('patch', { key, modifications: { value }, early, }) // The setting exist but that user has never set a value } else if (getters.one(key)) { await dispatch('post', { setting: { setting: key, value, }, early, }) // The setting does not exist } else { this.$toast.error("Ce réglage n'exsite pas", { icon: 'error_outline', }) } } catch (error) { // eslint-disable-next-line console.error(error) } }, 500, { trailing: false, leading: true } ), async toggle({ dispatch, getters }, { key, force }) { const setting = getters.one(key) if (setting.type !== 'BOOLEAN') { console.error( `Can't toggle ${key}: ${key} is not a BOOLEAN but a ${setting.type}` ) return } const value = !setting.value await dispatch('setValue', { key, force, value, early: true }) }, } const parsedValue = ( value, { type, multiple, positive }, recursionLevel = 0 // <-- Debug purposes only ) => { /* Considers the setting's type to determine how to parse a value. * If the type is not known, returns the value as-is. * * @param value: the setting's value to parse * @params {...} : different properties of a SettingDefinition object. */ let parsed if (value === null) return null if (value.trim() === '') { if (multiple) return [] else return null } if (multiple) { // Handle multiple values parsed = value .split('\n') // split by newlines .map(v => v.replace('\r', '')) // remove windows fuckeries .map(v => // parse each value ↓ Prevents ∞ recursion parsedValue(v, { type, multiple: false, positive }, recursionLevel + 1) ) } else { // Handle single values switch (type) { case 'BOOLEAN': parsed = value === 'true' break case 'DATE': if (value.match(/\//g)) { parsed = parse(value, 'dd/MM/yyyy', new Date()) } else { parsed = parseISO(value) } break case 'TIME': parsed = parse(value, 'HH:mm:ss', new Date()) break case 'DATETIME': parsed = parseISO(value) break case 'DATERANGE': { // Gets the start and stop dates. let dates = value.split(' - ') // Parse each date dates = dates.map(date => parsedValue( date, { type: 'DATE', multiple: false, positive: false, }, recursionLevel + 1 ) ) // If we don't have exactly two dates (start - stop), sets to the first date only. Else, create the range. parsed = dates.length === 2 ? { start: dates[0], end: dates[1] } : (parsed = dates[0]) break } case 'TIMERANGE': // TODO throw new Error('TIMERANGE setting types are not available yet.') case 'JSON': parsed = JSON.parse(value) break case 'FLOAT': parsed = parseFloat(value) break case 'INTEGER': parsed = parseInt(value) break case 'TEXT': case 'SELECT': parsed = value.trim() break default: parsed = value } } if (typeof parsed === 'number' && positive) parsed = Math.abs(parsed) return parsed } const stringifiedValue = ({ value, type }) => { // TODO: Handle multiple values switch (type) { case 'DATE': case 'TIME': case 'DATETIME': return formatISO(value, { representation: { DATE: 'complete', TIME: 'time', DATETIME: 'complete', }[type], }) case 'BOOLEAN': return value ? 'true' : 'false' default: return value } } |