Skip to content
Snippets Groups Projects
Commit 9320236b authored by Tom Robinson's avatar Tom Robinson
Browse files

Move timestamp parsing to reducers to fix pulse list ordering bug

parent fba6bf08
No related branches found
No related tags found
No related merge requests found
import moment from "moment";
import _ from "underscore";
// HACK: just use our Angular resources for now
export function AngularResourceProxy(serviceName, methods) {
......@@ -25,3 +27,18 @@ export function createThunkAction(actionType, actionThunkCreator) {
}
}
}
// turns string timestamps into moment objects
export function momentifyTimestamps(object, keys = ["created_at", "updated_at"]) {
object = { ...object };
for (let timestamp of keys) {
if (timestamp in object) {
object[timestamp] = moment(object[timestamp]);
}
}
return object;
}
export function momentifyObjectsTimestamps(objects, keys) {
return _.mapObject(objects, o => momentifyTimestamps(o, keys));
}
......@@ -3,8 +3,6 @@ import { createAction } from "redux-actions";
import { AngularResourceProxy, createThunkAction } from "metabase/lib/redux";
import { normalize, Schema, arrayOf } from "normalizr";
import moment from "moment";
const card = new Schema('card');
const pulse = new Schema('pulse');
const user = new Schema('user');
......@@ -31,10 +29,6 @@ export const FETCH_PULSE_CARD_PREVIEW = 'FETCH_PULSE_CARD_PREVIEW';
export const fetchPulses = createThunkAction(FETCH_PULSES, function() {
return async function(dispatch, getState) {
let pulses = await Pulse.list();
for (var p of pulses) {
p.updated_at = moment(p.updated_at);
p.created_at = moment(p.created_at);
}
return normalize(pulses, arrayOf(pulse));
};
});
......@@ -84,9 +78,6 @@ export const deletePulse = createThunkAction(DELETE_PULSE, function(id) {
export const fetchCards = createThunkAction(FETCH_CARDS, function(filterMode = "all") {
return async function(dispatch, getState) {
let cards = await Card.list({ filterMode });
for (var c of cards) {
c.updated_at = moment(c.updated_at);
}
return normalize(cards, arrayOf(card));
};
});
......@@ -95,12 +86,6 @@ export const fetchCards = createThunkAction(FETCH_CARDS, function(filterMode = "
export const fetchUsers = createThunkAction(FETCH_USERS, function() {
return async function(dispatch, getState) {
let users = await User.list();
for (var u of users) {
u.date_joined = (u.date_joined) ? moment(u.date_joined) : null;
u.last_login = (u.last_login) ? moment(u.last_login) : null;
}
return normalize(users, arrayOf(user));
};
});
......
import { handleActions } from 'redux-actions';
import { momentifyTimestamps, momentifyObjectsTimestamps } from "metabase/lib/redux";
import {
FETCH_PULSES,
SET_EDITING_PULSE,
......@@ -14,9 +16,9 @@ import {
} from "./actions";
export const pulses = handleActions({
[FETCH_PULSES]: { next: (state, { payload }) => ({ ...payload.entities.pulse }) },
[SAVE_PULSE]: { next: (state, { payload }) => ({ ...state, [payload.id]: payload }) },
[SAVE_EDITING_PULSE]: { next: (state, { payload }) => ({ ...state, [payload.id]: payload }) }
[FETCH_PULSES]: { next: (state, { payload }) => ({ ...momentifyObjectsTimestamps(payload.entities.pulse) }) },
[SAVE_PULSE]: { next: (state, { payload }) => ({ ...state, [payload.id]: momentifyTimestamps(payload) }) },
[SAVE_EDITING_PULSE]: { next: (state, { payload }) => ({ ...state, [payload.id]: momentifyTimestamps(payload) }) }
}, {});
export const pulseList = handleActions({
......@@ -33,7 +35,7 @@ export const editingPulse = handleActions({
// NOTE: duplicated from dashboards/reducers.js
export const cards = handleActions({
[FETCH_CARDS]: { next: (state, { payload }) => ({ ...payload.entities.card }) }
[FETCH_CARDS]: { next: (state, { payload }) => ({ ...momentifyObjectsTimestamps(payload.entities.card) }) }
}, {});
export const cardList = handleActions({
[FETCH_CARDS]: { next: (state, { payload }) => payload.result }
......@@ -41,7 +43,7 @@ export const cardList = handleActions({
// NOTE: duplicated from admin/people/reducers.js
export const users = handleActions({
[FETCH_USERS]: { next: (state, { payload }) => ({ ...payload.entities.user }) }
[FETCH_USERS]: { next: (state, { payload }) => ({ ...momentifyObjectsTimestamps(payload.entities.user) }) }
}, []);
export const formInput = handleActions({
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment