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

Updated PolicyActions.tsx with Follow/Unfollow functionality #1657

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions components/api/bills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { httpsCallable } from "firebase/functions";
import { functions } from "../firebase"; // Path to Firebase initialization

export async function followBill(data: { court: number; id: string }) {
const callable = httpsCallable(functions, "followBill");
return callable(data);
}

export async function unfollowBill(data: { court: number; id: string }) {
const callable = httpsCallable(functions, "unfollowBill");
return callable(data);
}
100 changes: 63 additions & 37 deletions components/testimony/TestimonyDetailPage/PolicyActions.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,90 @@
import { Card, ListItem, ListItemProps } from "components/Card"
import { useFlags } from "components/featureFlags"
import { formatBillId } from "components/formatting"
import { formUrl } from "components/publish"
import { isNotNull } from "components/utils"
import { FC, ReactElement } from "react"
import { useCurrentTestimonyDetails } from "./testimonyDetailSlice"
import { useTranslation } from "next-i18next"
import { Card, ListItem, ListItemProps } from "components/Card";
import { useFlags } from "components/featureFlags";
import { formatBillId } from "components/formatting";
import { formUrl } from "components/publish";
import { isNotNull } from "components/utils";
import { FC, ReactElement, useState, useEffect } from "react";
import { useCurrentTestimonyDetails } from "./testimonyDetailSlice";
import { useTranslation } from "next-i18next";
import { followBill, unfollowBill } from "../../api/bills";

interface PolicyActionsProps {
className?: string
isUser?: boolean
isReporting: boolean
setReporting: (boolean: boolean) => void
className?: string;
isUser?: boolean;
isReporting: boolean;
setReporting: (boolean: boolean) => void;
}

const PolicyActionItem: FC<React.PropsWithChildren<ListItemProps>> = props => (
const PolicyActionItem: FC<React.PropsWithChildren<ListItemProps>> = (props) => (
<ListItem action active={false} variant="secondary" {...props} />
)
);

export const PolicyActions: FC<React.PropsWithChildren<PolicyActionsProps>> = ({
className,
isUser,
isReporting,
setReporting
setReporting,
}) => {
const { bill } = useCurrentTestimonyDetails(),
billLabel = formatBillId(bill.id)
const { notifications } = useFlags()
const { bill } = useCurrentTestimonyDetails(),
billLabel = formatBillId(bill.id);
const { notifications } = useFlags();

const items: ReactElement[] = []
if (notifications)
items.push(
<PolicyActionItem
onClick={() => window.alert("TODO")} // TODO: add follow action here
key="follow"
billName={`Follow ${billLabel}`}
/>
)
items.push(
const [isFollowing, setIsFollowing] = useState(false); // Track follow state
const [loading, setLoading] = useState(false); // Track loading state

// Fetch initial follow state
useEffect(() => {
const userIsFollowing = bill.followers?.includes("currentUserId"); // Replace "currentUserId" with actual user ID logic

Check failure on line 37 in components/testimony/TestimonyDetailPage/PolicyActions.tsx

View workflow job for this annotation

GitHub Actions / Code Quality

Property 'followers' does not exist on type 'Bill'.
setIsFollowing(userIsFollowing || false);
}, [bill]);

const handleFollowToggle = async () => {
setLoading(true);
try {
if (isFollowing) {
await unfollowBill({ court: bill.court, id: bill.id }); // Backend call to unfollow
} else {
await followBill({ court: bill.court, id: bill.id }); // Backend call to follow
}
setIsFollowing(!isFollowing); // Toggle state
} catch (error) {
console.error("Error toggling follow state:", error);
} finally {
setLoading(false);
}
};

const items: ReactElement[] = [

Check failure on line 57 in components/testimony/TestimonyDetailPage/PolicyActions.tsx

View workflow job for this annotation

GitHub Actions / Code Quality

Type '(false | Element)[]' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>>[]'.
notifications && (
<div key="follow" className="follow-button-container">
<button disabled={loading} onClick={handleFollowToggle}>
{loading
? "Loading..."
: isFollowing
? `Unfollow ${billLabel}`
: `Follow ${billLabel}`}
</button>
</div>
),
<PolicyActionItem
key="report-testimony"
billName={`Report Testimony`}
billName="Report Testimony"
onClick={() => setReporting(!isReporting)}
/>
)
items.push(
/>,
<PolicyActionItem
key="add-testimony"
billName={`${isUser ? "Edit" : "Add"} Testimony for ${billLabel}`}
href={formUrl(bill.id, bill.court)}
/>
)
/>,
].filter(isNotNull);

const { t } = useTranslation("testimony")
const { t } = useTranslation("testimony");

return (
<Card
className={className}
header={t("policyActions.actions") ?? "Actions"}
items={items}
/>
)
}
);
};
Loading