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

23 heart button interactivity #30

Merged
merged 14 commits into from
Dec 23, 2023
47 changes: 42 additions & 5 deletions src/components/Resources/ResourceTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import TagSection from '@/components/TagSection.tsx'
import client from '@/database/client'
import { Tooltip } from 'flowbite-react'
import { useEffect, useState } from 'react'
import { FaHeart } from 'react-icons/fa'
import { FaHeart, FaSun } from 'react-icons/fa'
import { useLoaderData } from 'react-router-dom'
import SessionWrapper from '../Auth/SessionWrapper'

export interface Resource {
id: number
Expand Down Expand Up @@ -36,6 +37,17 @@ export default function ResourceTable() {
}
}, [tableData.length, data.length])

const favorite = async (row: Resource) => {
const new_helped = row.num_helped + 1
const idx = data.map((o) => o.id).indexOf(row.id)
const temp = [...data]
temp[idx].num_helped = new_helped
setTableData([...temp])
await client
.from('resources')
.update({ num_helped: new_helped })
.eq('id', row.id)
}
return (
<table className="w-full rounded-lg bg-white">
<thead className="border-0 border-b-8 border-solid border-orange-50 ">
Expand Down Expand Up @@ -68,9 +80,13 @@ export default function ResourceTable() {
<td className="flex w-48 flex-wrap gap-1 p-4">
<TagSection resourceId={d.id} />
</td>
<td className="p-4 text-xs">
<td className="w-32 p-4 text-xs">
<Tooltip
content={`This resource has helped ${d.num_helped} people`}
content={
d.num_helped
? `This resource has helped ${d.num_helped} people`
: `New resource`
}
animation="duration-1000"
className="bg-gray-900 text-white dark:bg-gray-700"
arrow={false}
Expand All @@ -79,8 +95,29 @@ export default function ResourceTable() {
data-tooltip-target="tooltip-default"
className="flex justify-end text-right text-xs font-bold"
>
{d.num_helped}
<FaHeart className="ml-1 text-orange-500 " />
{' '}
<span>
{d.num_helped ? (
`Helped ${d.num_helped}`
) : (
<div className="flex items-center">
<FaSun className="ml-1 text-orange-500" />{' '}
New!
</div>
)}
</span>
<SessionWrapper
ifSession={
<button
onClick={async () =>
await favorite(d)
}
>
<FaHeart className="ml-1 text-orange-500 " />
</button>
}
notSession={<></>}
/>
</span>
</Tooltip>
</td>
Expand Down
Loading