Alternative sorting method
parent
09e9341c64
commit
3fced3de85
|
@ -1,5 +1,5 @@
|
|||
import * as React from "react";
|
||||
import { PureFarmEvents, stringToMinutes } from "../farm_events";
|
||||
import { PureFarmEvents } from "../farm_events";
|
||||
import {
|
||||
calendarRows
|
||||
} from "../../../__test_support__/farm_event_calendar_support";
|
||||
|
@ -17,16 +17,17 @@ describe("<PureFarmEvents/>", () => {
|
|||
.map(x => x.children)
|
||||
.map(x => x[0])
|
||||
.map(x => get(x, "data", "NOT_FOUND"));
|
||||
expect(rows).not.toContain("NOT_FOUND");
|
||||
expect(rows.length).toEqual(21);
|
||||
expect(rows[0]).toEqual("12:05pm");
|
||||
expect(rows[2]).toEqual("02:00pm");
|
||||
});
|
||||
|
||||
fit("parses a time string into minutes", () => {
|
||||
let items = calendarRows[0].items;
|
||||
expect(stringToMinutes(items[0].timeStr)).toEqual(840);
|
||||
expect(stringToMinutes(items[1].timeStr)).toEqual(1445);
|
||||
expect(stringToMinutes(items[2].timeStr)).toEqual(965);
|
||||
expect(stringToMinutes(items[3].timeStr)).toEqual(840);
|
||||
});
|
||||
// fit("parses a time string into minutes", () => {
|
||||
// let items = calendarRows[0].items;
|
||||
// expect(stringToMinutes(items[0].timeStr)).toEqual(840);
|
||||
// expect(stringToMinutes(items[1].timeStr)).toEqual(1445);
|
||||
// expect(stringToMinutes(items[2].timeStr)).toEqual(965);
|
||||
// expect(stringToMinutes(items[3].timeStr)).toEqual(840);
|
||||
// });
|
||||
});
|
||||
|
|
|
@ -8,27 +8,11 @@ import { FarmEventProps, CalendarOccurrence } from "../interfaces";
|
|||
import { FBSelect } from "../../ui/new_fb_select";
|
||||
import * as _ from "lodash";
|
||||
|
||||
/** GIVEN: A string, formatted as `hh:mmaa`.
|
||||
* RETURNS: The number of minutes, from midnight of that string.
|
||||
* EXAMPLE: 02:05pm returns 845 (since 14:05 is 845 minutes from midnight).
|
||||
*/
|
||||
export function stringToMinutes(hhmmaa: string): number {
|
||||
if (!!hhmmaa.match(/[0-9][0-9]:[0-9][0-9](am|pm)/)) {
|
||||
let h = parseInt(hhmmaa.slice(0, 2)) * 60;
|
||||
let m = parseInt(hhmmaa.slice(3, 5));
|
||||
let a = hhmmaa[5] === "p" ? (12 * 60) : 0;
|
||||
return h + m + a;
|
||||
} else {
|
||||
throw new Error("Bad calendar item string format.");
|
||||
}
|
||||
}
|
||||
|
||||
export class PureFarmEvents extends React.Component<FarmEventProps, {}> {
|
||||
innerRows = (items: CalendarOccurrence[]) => {
|
||||
let SORT_KEY: keyof typeof items[0] = "sortKey";
|
||||
|
||||
return _(items)
|
||||
.sortBy(x => stringToMinutes(x.timeStr))
|
||||
.sortBy(x => x.sortKey)
|
||||
.value()
|
||||
.map((farmEvent, index) => {
|
||||
let url = `/app/designer/farm_events/` + (farmEvent.id || "UNSAVED_EVENT").toString();
|
||||
|
|
Loading…
Reference in New Issue