AI Changes: updated look, recently added, search bar, easy admin checkout
This commit is contained in:
@@ -1,5 +1,25 @@
|
||||
import { DrinkComposite, isDrinkWithContainers, isDrinkWithContainersAndHistory } from "./types";
|
||||
|
||||
function getLatestActivityTimestamp(n: DrinkComposite): Date | null {
|
||||
const drinkTimestamp = (n as { addedAt?: Date | null }).addedAt ?? null;
|
||||
|
||||
if (isDrinkWithContainers(n)) {
|
||||
const containerTimestamps = n.containers
|
||||
.map((container) => (container as { lastAdded?: Date | null }).lastAdded)
|
||||
.filter((value): value is Date => value != null);
|
||||
|
||||
if (containerTimestamps.length > 0) {
|
||||
const latestContainerTimestamp = containerTimestamps.reduce((latest, current) => current > latest ? current : latest, containerTimestamps[0]);
|
||||
if (drinkTimestamp == null) {
|
||||
return latestContainerTimestamp;
|
||||
}
|
||||
return drinkTimestamp > latestContainerTimestamp ? drinkTimestamp : latestContainerTimestamp;
|
||||
}
|
||||
}
|
||||
|
||||
return drinkTimestamp;
|
||||
}
|
||||
|
||||
function sortName(n1:DrinkComposite, n2:DrinkComposite) : number{
|
||||
if (n1.name > n2.name) {
|
||||
return 1;
|
||||
@@ -56,6 +76,24 @@ function sortPopularity(n1:DrinkComposite, n2:DrinkComposite) : number{
|
||||
return totalPopularity(n2) - totalPopularity(n1);
|
||||
}
|
||||
|
||||
function sortNewest(n1:DrinkComposite, n2:DrinkComposite) : number{
|
||||
const left = getLatestActivityTimestamp(n1);
|
||||
const right = getLatestActivityTimestamp(n2);
|
||||
|
||||
if (!left && !right) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!left) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!right) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return right.getTime() - left.getTime();
|
||||
}
|
||||
|
||||
export function sortDrinksFromSearch(searchParams: URLSearchParams, searchResult: DrinkComposite[]) : DrinkComposite[] {
|
||||
|
||||
@@ -85,6 +123,10 @@ export function sortDrinksFromSearch(searchParams: URLSearchParams, searchResult
|
||||
callback = sortPopularity;
|
||||
}
|
||||
|
||||
if(searchParams.has("sort", "new")){
|
||||
callback = sortNewest;
|
||||
}
|
||||
|
||||
var sortedArray: DrinkComposite[] = searchResult.sort(callback);
|
||||
|
||||
return sortedArray;
|
||||
|
||||
Reference in New Issue
Block a user