Skip to content

Commit

Permalink
refactor: use useIonAlert
Browse files Browse the repository at this point in the history
  • Loading branch information
httpjamesm authored and aeharding committed Jun 29, 2023
1 parent cdc8b0e commit 2855437
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 85 deletions.
30 changes: 0 additions & 30 deletions src/features/user/AgeAlert.tsx

This file was deleted.

31 changes: 0 additions & 31 deletions src/features/user/ScoreAlert.tsx

This file was deleted.

59 changes: 35 additions & 24 deletions src/features/user/Scores.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import styled from "@emotion/styled";
import { PersonAggregates } from "lemmy-js-client";
import { formatNumber } from "../../helpers/number";
import Ago from "../labels/Ago";
import AccountAgeAlert from "./AgeAlert";
import { useState } from "react";
import ScoreAlert from "./ScoreAlert";
import { useIonAlert } from "@ionic/react";
import { formatDistanceToNowStrict } from "date-fns";

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -33,54 +32,66 @@ interface ScoreProps {
}

export default function Scores({ aggregates, accountCreated }: ScoreProps) {
const [accountAgeAlertOpen, setAccountAgeAlertOpen] = useState(false);
const [scoreAlertOpen, setScoreAlertOpen] = useState(false);
const [scoreAlertFocus, setScoreAlertFocus] = useState<"comment" | "post">(
"comment",
const [present] = useIonAlert();

const relativeDate = formatDistanceToNowStrict(
new Date(`${accountCreated}Z`),
{
addSuffix: false,
}
);
const creationDate = new Date(accountCreated);

const postScore = aggregates.post_score;
const commentScore = aggregates.comment_score;

const showScoreAlert = async (focus: "post" | "comment") => {
const subHeader = `${focus === "post" ? commentScore : postScore} ${
focus === "post" ? "Comment" : "Post"
} Points`;
const message = `${postScore + commentScore} Total Points`;

await present({
header: `${focus === "post" ? postScore : commentScore} ${focus} points`,
htmlAttributes: { style: "white-space: pre-line;" },
subHeader,
message,
buttons: [{ text: "OK" }],
});
};

return (
<>
<Container>
<Score
onClick={() => {
setScoreAlertOpen(true);
setScoreAlertFocus("comment");
showScoreAlert("comment");
}}
>
{formatNumber(aggregates.comment_score)}
<aside>Comment score</aside>
</Score>
<Score
onClick={() => {
setScoreAlertOpen(true);
setScoreAlertFocus("post");
showScoreAlert("post");
}}
>
{formatNumber(aggregates.post_score)}
<aside>Post score</aside>
</Score>
<Score
onClick={() => {
setAccountAgeAlertOpen(true);
present({
header: `Account is ${relativeDate} old`,
message: `Created on ${creationDate.toDateString()} at ${creationDate.toLocaleTimeString()}`,
buttons: [{ text: "OK" }],
});
}}
>
<Ago date={accountCreated} />
<aside>Account age</aside>
</Score>
</Container>
<AccountAgeAlert
isOpen={accountAgeAlertOpen}
setIsOpen={setAccountAgeAlertOpen}
accountCreated={accountCreated}
/>
<ScoreAlert
isOpen={scoreAlertOpen}
setIsOpen={setScoreAlertOpen}
focus={scoreAlertFocus}
postScore={aggregates.post_score}
commentScore={aggregates.comment_score}
/>
</>
);
}

0 comments on commit 2855437

Please sign in to comment.