This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate BrowserStore to redux (#241)
- Loading branch information
Showing
15 changed files
with
616 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
import {StorageTypes} from 'utils/constants'; | ||
import {getPrefix} from 'utils/storage_utils'; | ||
|
||
export function setItem(name, value) { | ||
return async (dispatch, getState) => { | ||
const state = getState(); | ||
const prefix = getPrefix(state); | ||
dispatch({ | ||
type: StorageTypes.SET_ITEM, | ||
data: {prefix, name, value} | ||
}, getState); | ||
return {data: true}; | ||
}; | ||
} | ||
|
||
export function removeItem(name) { | ||
return async (dispatch, getState) => { | ||
const state = getState(); | ||
const prefix = getPrefix(state); | ||
dispatch({ | ||
type: StorageTypes.REMOVE_ITEM, | ||
data: {prefix, name} | ||
}, getState); | ||
return {data: true}; | ||
}; | ||
} | ||
|
||
export function setGlobalItem(name, value) { | ||
return async (dispatch, getState) => { | ||
dispatch({ | ||
type: StorageTypes.SET_GLOBAL_ITEM, | ||
data: {name, value} | ||
}, getState); | ||
return {data: true}; | ||
}; | ||
} | ||
|
||
export function removeGlobalItem(name) { | ||
return async (dispatch, getState) => { | ||
dispatch({ | ||
type: StorageTypes.REMOVE_GLOBAL_ITEM, | ||
data: {name} | ||
}, getState); | ||
return {data: true}; | ||
}; | ||
} | ||
|
||
export function clear(options) { | ||
return async (dispatch, getState) => { | ||
dispatch({ | ||
type: StorageTypes.CLEAR, | ||
data: options | ||
}, getState); | ||
return {data: false}; | ||
}; | ||
} | ||
|
||
export function actionOnGlobalItemsWithPrefix(prefix, action) { | ||
return async (dispatch, getState) => { | ||
dispatch({ | ||
type: StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX, | ||
data: {prefix, action} | ||
}, getState); | ||
return {data: false}; | ||
}; | ||
} | ||
|
||
export function actionOnItemsWithPrefix(prefix, action) { | ||
return async (dispatch, getState) => { | ||
const state = getState(); | ||
const globalPrefix = getPrefix(state); | ||
dispatch({ | ||
type: StorageTypes.ACTION_ON_ITEMS_WITH_PREFIX, | ||
data: {globalPrefix, prefix, action} | ||
}, getState); | ||
return {data: false}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
import {StorageTypes} from 'utils/constants'; | ||
|
||
export default function storage(state = {}, action) { | ||
const nextState = {...state}; | ||
var key; | ||
|
||
switch (action.type) { | ||
case StorageTypes.SET_ITEM: { | ||
nextState[action.data.prefix + action.data.name] = action.data.value; | ||
return nextState; | ||
} | ||
case StorageTypes.REMOVE_ITEM: { | ||
Reflect.deleteProperty(nextState, action.data.prefix + action.data.name); | ||
return nextState; | ||
} | ||
case StorageTypes.SET_GLOBAL_ITEM: { | ||
nextState[action.data.name] = action.data.value; | ||
return nextState; | ||
} | ||
case StorageTypes.REMOVE_GLOBAL_ITEM: { | ||
Reflect.deleteProperty(nextState, action.data.name); | ||
return nextState; | ||
} | ||
case StorageTypes.CLEAR: { | ||
var cleanState = {}; | ||
action.data.exclude.forEach((excluded) => { | ||
if (state[excluded]) { | ||
cleanState[excluded] = state[excluded]; | ||
} | ||
}); | ||
return cleanState; | ||
} | ||
case StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX: { | ||
for (key in state) { | ||
if (key.lastIndexOf(action.data.prefix, 0) === 0) { | ||
nextState[key] = action.data.action(key, state[key]); | ||
} | ||
} | ||
return nextState; | ||
} | ||
case StorageTypes.ACTION_ON_ITEMS_WITH_PREFIX: { | ||
var globalPrefix = action.data.globalPrefix; | ||
var globalPrefixLen = action.data.globalPrefix.length; | ||
for (key in state) { | ||
if (key.lastIndexOf(globalPrefix + action.data.prefix, 0) === 0) { | ||
var userkey = key.substring(globalPrefixLen); | ||
nextState[key] = action.data.action(userkey, state[key]); | ||
} | ||
} | ||
return nextState; | ||
} | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
import {getPrefix} from 'utils/storage_utils'; | ||
|
||
function getGlobalItem(state, name, defaultValue) { | ||
if (state && state.storage && typeof state.storage[name] !== 'undefined' && state.storage[name] !== null) { | ||
return state.storage[name]; | ||
} | ||
return defaultValue; | ||
} | ||
|
||
export function makeGetItem(name, defaultValue) { | ||
return (state) => { | ||
return getGlobalItem(state, getPrefix(state) + name, defaultValue); | ||
}; | ||
} | ||
|
||
export function makeGetGlobalItem(name, defaultValue) { | ||
return (state) => { | ||
return getGlobalItem(state, name, defaultValue); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.