sforkowany z mirror/soapbox
Convert reducers/alerts to Typescript
rodzic
7efa10e7e0
commit
836c02b388
|
@ -15,24 +15,31 @@ const AlertRecord = ImmutableRecord({
|
||||||
actionLink: '',
|
actionLink: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
const initialState = ImmutableList();
|
import type { AnyAction } from 'redux';
|
||||||
|
|
||||||
|
type PlainAlert = Record<string, any>;
|
||||||
|
type Alert = ReturnType<typeof AlertRecord>;
|
||||||
|
type State = ImmutableList<Alert>;
|
||||||
|
|
||||||
// Get next key based on last alert
|
// Get next key based on last alert
|
||||||
const getNextKey = state => state.size > 0 ? state.last().get('key') + 1 : 0;
|
const getNextKey = (state: State): number => {
|
||||||
|
const last = state.last();
|
||||||
|
return last ? last.key + 1 : 0;
|
||||||
|
};
|
||||||
|
|
||||||
// Import the alert
|
// Import the alert
|
||||||
const importAlert = (state, alert) => {
|
const importAlert = (state: State, alert: PlainAlert): State => {
|
||||||
const key = getNextKey(state);
|
const key = getNextKey(state);
|
||||||
const record = AlertRecord({ ...alert, key });
|
const record = AlertRecord({ ...alert, key });
|
||||||
return state.push(record);
|
return state.push(record);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete an alert by its key
|
// Delete an alert by its key
|
||||||
const deleteAlert = (state, alert) => {
|
const deleteAlert = (state: State, alert: PlainAlert): State => {
|
||||||
return state.filterNot(item => item.key === alert.key);
|
return state.filterNot(item => item.key === alert.key);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function alerts(state = initialState, action) {
|
export default function alerts(state: State = ImmutableList<Alert>(), action: AnyAction): State {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case ALERT_SHOW:
|
case ALERT_SHOW:
|
||||||
return importAlert(state, action);
|
return importAlert(state, action);
|
|
@ -195,7 +195,7 @@ type Alert = ReturnType<typeof buildAlert>;
|
||||||
|
|
||||||
export const getAlerts = createSelector([getAlertsBase], (base): Alert[] => {
|
export const getAlerts = createSelector([getAlertsBase], (base): Alert[] => {
|
||||||
const arr: Alert[] = [];
|
const arr: Alert[] = [];
|
||||||
base.forEach((item: any) => arr.push(buildAlert(item)));
|
base.forEach(item => arr.push(buildAlert(item)));
|
||||||
return arr;
|
return arr;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue