Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Home & Homeworks date #501

Merged
merged 13 commits into from
Dec 19, 2024
17 changes: 17 additions & 0 deletions src/utils/format/DateHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const timestampToString = (timestamp: number) => {
const date = new Date(timestamp);
const today = new Date();

today.setHours(0, 0, 0, 0);
date.setHours(0, 0, 0, 0);

const difference = Math.ceil((date.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));

return difference === 0
? "Aujourd'hui"
: difference === 1
? "Demain"
: difference === 2
? "Après-demain"
: `Dans ${difference} jours`;
};
2 changes: 1 addition & 1 deletion src/views/account/Home/Elements/AttendanceElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const AttendanceElement: React.FC<AttendanceElementProps> = ({ onImportance }) =

return (
<>
<NativeListHeader label={`Vie scolaire — ${defaultPeriod}`}
<NativeListHeader label={`Vie scolaire`}
trailing={(
<RedirectButton navigation={PapillonNavigation.current} redirect="Attendance" />
)}
Expand Down
9 changes: 3 additions & 6 deletions src/views/account/Homeworks/Atoms/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { LinearGradient } from "expo-linear-gradient";
import { useCurrentAccount } from "@/stores/account";
import LinkFavicon, { getURLDomain } from "@/components/Global/LinkFavicon";
import { AutoFileIcon } from "@/components/Global/FileIcon";
import { timestampToString } from "@/utils/format/DateHelper";


interface HomeworkItemProps {
key: number | string
Expand All @@ -27,6 +29,7 @@ interface HomeworkItemProps {
navigation: NativeStackNavigationProp<RouteParameters, "HomeScreen" | "Homeworks", undefined>
}


const HomeworkItem = ({ homework, navigation, onDonePressHandler, index, total }: HomeworkItemProps) => {
const theme = useTheme();
const [subjectData, setSubjectData] = useState(getSubjectData(homework.subject));
Expand Down Expand Up @@ -69,13 +72,7 @@ const HomeworkItem = ({ homework, navigation, onDonePressHandler, index, total }
setMainLoaded(true);
}, [homework.done]);

const timestampToString = (timestamp: number) => {
const date = new Date(timestamp);
const today = new Date();

const difference = Math.ceil((date.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
return difference === 0 ? "Aujourd'hui" : difference === 1 ? "Demain" : difference === 2 ? "Après-demain" : `Dans ${difference} jours`;
};

const renderCategoryOrReturnType = () => {
if (category) {
Expand Down
16 changes: 6 additions & 10 deletions src/views/account/Homeworks/Document.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import {
NativeItem,
NativeList,
Expand All @@ -16,11 +17,6 @@ import {
} from "react-native";
import { Homework, HomeworkReturnType } from "@/services/shared/Homework";
import { getSubjectData } from "@/services/shared/Subject";

import { formatDistance } from "date-fns";
import { fr } from "date-fns/locale";
import { FileText, Link, Paperclip, CircleAlert, FileIcon } from "lucide-react-native";

import * as WebBrowser from "expo-web-browser";
import { useTheme } from "@react-navigation/native";
import HTMLView from "react-native-htmlview";
Expand All @@ -31,8 +27,10 @@ import { PapillonModernHeader } from "@/components/Global/PapillonModernHeader";
import { useCurrentAccount } from "@/stores/account";
import { AccountService } from "@/stores/account/types";
import getAndOpenFile from "@/utils/files/getAndOpenFile";
import LinkFavicon, { getURLDomain } from "@/components/Global/LinkFavicon";
import { AutoFileIcon } from "@/components/Global/FileIcon";
import { FileText, Link, Paperclip, CircleAlert, FileIcon } from "lucide-react-native";
import LinkFavicon, { getURLDomain } from "@/components/Global/LinkFavicon";
import { timestampToString } from "@/utils/format/DateHelper";

const HomeworksDocument: Screen<"HomeworksDocument"> = ({ route }) => {
const theme = useTheme();
Expand Down Expand Up @@ -81,6 +79,7 @@ const HomeworksDocument: Screen<"HomeworksDocument"> = ({ route }) => {
fetchSubjectData();
}, [homework.subject]);


return (
<View style={{ flex: 1 }}>
<PapillonModernHeader native outsideNav={true}>
Expand All @@ -106,10 +105,7 @@ const HomeworksDocument: Screen<"HomeworksDocument"> = ({ route }) => {
{subjectData.pretty}
</NativeText>
<NativeText variant="subtitle" numberOfLines={1}>
{formatDistance(new Date(homework.due), new Date(), {
addSuffix: true,
locale: fr,
})}
{timestampToString(new Date(homework.due).getTime())}
</NativeText>
</View>
<View>
Expand Down
Loading