Compare commits
2 Commits
cb4ce4188e
...
5cb323a1d2
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cb323a1d2 | |||
| 8314c4daf8 |
@@ -1,5 +1,4 @@
|
|||||||
import FourTwenty from "@components/FourTwenty/FourTwenty";
|
import FourTwenty from "@components/FourTwenty/FourTwenty";
|
||||||
import classNames from "classnames";
|
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import Amogus from "@/components/Amogus/Amogus";
|
import Amogus from "@/components/Amogus/Amogus";
|
||||||
import Card from "@/components/Card/Card";
|
import Card from "@/components/Card/Card";
|
||||||
|
|||||||
@@ -1,35 +1,10 @@
|
|||||||
|
import classNames from "classnames";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useFlatasticStore } from "@/store/flatastic";
|
import { useFlatasticStore } from "@/store/flatastic";
|
||||||
|
|
||||||
import type { FlatasticChore, FlatasticUser } from "@/types/flatasticChore";
|
import type { FlatasticChore, FlatasticUser } from "@/types/flatasticChore";
|
||||||
|
|
||||||
import style from "./style.module.css";
|
import style from "./style.module.css";
|
||||||
|
|
||||||
export default function Flatastic() {
|
function choreItem(chore: FlatasticChore, idToNameMap: Record<number, string>) {
|
||||||
const fetchFlatasticData = useFlatasticStore((state) => state.fetch);
|
|
||||||
const flatasticData = useFlatasticStore((state) => state.flatasticData);
|
|
||||||
const chores = (flatasticData?.chores as FlatasticChore[]) || [];
|
|
||||||
|
|
||||||
chores.sort(
|
|
||||||
(a, b) =>
|
|
||||||
a.timeLeftNext - b.timeLeftNext && b.rotationTime - a.rotationTime,
|
|
||||||
);
|
|
||||||
|
|
||||||
const users = flatasticData?.users;
|
|
||||||
const idToNameMap: Record<number, string> = {};
|
|
||||||
users.forEach((user: FlatasticUser) => {
|
|
||||||
idToNameMap[user.id] = user.firstName;
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
fetchFlatasticData();
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
fetchFlatasticData();
|
|
||||||
}, 60000);
|
|
||||||
return () => clearInterval(interval);
|
|
||||||
}, [fetchFlatasticData]);
|
|
||||||
|
|
||||||
const choresRender = chores.map((chore) => {
|
|
||||||
let className = "";
|
let className = "";
|
||||||
let timeLeftInDays = null;
|
let timeLeftInDays = null;
|
||||||
|
|
||||||
@@ -47,7 +22,7 @@ export default function Flatastic() {
|
|||||||
return (
|
return (
|
||||||
<li
|
<li
|
||||||
key={chore.id}
|
key={chore.id}
|
||||||
className={[style.chore, style[className]].join(" ")}
|
className={classNames(style.chore, style[className])}
|
||||||
>
|
>
|
||||||
<span className={style.userName}>
|
<span className={style.userName}>
|
||||||
{`${idToNameMap[chore.currentUser]}`}
|
{`${idToNameMap[chore.currentUser]}`}
|
||||||
@@ -55,12 +30,55 @@ export default function Flatastic() {
|
|||||||
: {chore.title} {timeLeftInDays} {"🪙".repeat(chore.points)}
|
: {chore.title} {timeLeftInDays} {"🪙".repeat(chore.points)}
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Flatastic() {
|
||||||
|
const fetchFlatasticData = useFlatasticStore((state) => state.fetch);
|
||||||
|
const flatasticData = useFlatasticStore((state) => state.flatasticData);
|
||||||
|
const chores = (flatasticData?.chores as FlatasticChore[]) || [];
|
||||||
|
const regularChores: FlatasticChore[] = [];
|
||||||
|
const irregularChores: FlatasticChore[] = [];
|
||||||
|
|
||||||
|
chores.forEach((chore) => {
|
||||||
|
if (chore.rotationTime === -1) {
|
||||||
|
irregularChores.push(chore);
|
||||||
|
} else {
|
||||||
|
regularChores.push(chore);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
regularChores.sort((a, b) => a.lastDoneDate - b.lastDoneDate);
|
||||||
|
|
||||||
|
const users = flatasticData?.users;
|
||||||
|
const idToNameMap: Record<number, string> = {};
|
||||||
|
users.forEach((user: FlatasticUser) => {
|
||||||
|
idToNameMap[user.id] = user.firstName;
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchFlatasticData();
|
||||||
|
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
fetchFlatasticData();
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, [fetchFlatasticData]);
|
||||||
|
|
||||||
|
const regularChoresRender = regularChores.map((chore) => {
|
||||||
|
return choreItem(chore, idToNameMap);
|
||||||
|
});
|
||||||
|
|
||||||
|
const irregularChoresRender = irregularChores.map((chore) => {
|
||||||
|
return choreItem(chore, idToNameMap);
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={style.container}>
|
<div className={style.container}>
|
||||||
<h1>Chores</h1>
|
<h1>Chores</h1>
|
||||||
<ul className={style.choreList}>{choresRender}</ul>
|
<ul className={style.choreList}>
|
||||||
|
{[...regularChoresRender, ...irregularChoresRender]}
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ interface FlatasticChore {
|
|||||||
points: number;
|
points: number;
|
||||||
rotationTime: number;
|
rotationTime: number;
|
||||||
currentUser: number;
|
currentUser: number;
|
||||||
lastDoneDate: string;
|
lastDoneDate: number;
|
||||||
timeLeftNext: number;
|
timeLeftNext: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user