Skip to content

Commit

Permalink
Implement delete goal functionality (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
wweitzel authored Aug 21, 2024
1 parent 7aa2e58 commit dacd4b2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/components/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
interface InputProps {
interface Props {
label: string;
placeholder: string;
value?: number | string;
onInput: (event: string) => void;
onEnterKeyDown: () => void;
}

function Input({label, placeholder, value, onInput, onEnterKeyDown}: InputProps) {
function Input({label, placeholder, value, onInput, onEnterKeyDown}: Props) {
return (
<div className="w-100">
<label className="form-label text-muted">{label}</label>
Expand Down
11 changes: 9 additions & 2 deletions src/components/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const CLICKED_BUTTON_TEXT = 'Link Copied';
const CLOUDFRONT_BASE_URL = 'https://s3-redditsoccergoals.top90.io/';
const REDDIT_COMMENTS_BASE_URL = 'https://www.reddit.com/r/soccer/comments/';

function Video({goal}: {goal: Goal}) {
interface Props {
goal: Goal;
onDelete?: () => void;
}

function Video({goal, onDelete}: Props) {
const [loggedIn, setLoggedIn] = useState(false);
const [buttonText, setButtonText] = useState(DEFAULT_BUTTON_TEXT);
const [timer, setTimer] = useState<ReturnType<typeof setTimeout>>();
Expand Down Expand Up @@ -88,7 +93,9 @@ function Video({goal}: {goal: Goal}) {
</a>
{loggedIn && (
<button
onClick={() => {}}
onClick={() => {
onDelete && onDelete();
}}
disabled={disableButton}
className="btn btn-outline-danger btn-sm border-0"
>
Expand Down
12 changes: 12 additions & 0 deletions src/lib/api/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export interface GetGoalResponse {
goal: Goal;
}

export interface DeleteGoalResponse {
rowsAffected: number;
}

export async function getGoals(pagination?: Pagination, filter?: GetGoalsFilter) {
if (!pagination) {
pagination = {skip: 0, limit: 5};
Expand All @@ -57,3 +61,11 @@ export async function getGoal(id: string) {
const response = await axios.get<GetGoalResponse>(`${API_BASE_URL}/goals/${id}`);
return response.data;
}

export async function deleteGoal(id: string) {
const token = localStorage.getItem('top90-auth-token');
const response = await axios.delete<DeleteGoalResponse>(`${API_BASE_URL}/goals/${id}`, {
headers: {Authorization: `Bearer ${token}`},
});
return response.data;
}
22 changes: 20 additions & 2 deletions src/pages/Goals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Input from '../components/Input';
import Select from '../components/Select';
import Video from '../components/Video';
import {Pagination} from '../lib/api/core';
import {getGoals, GetGoalsFilter, GetGoalsResponse} from '../lib/api/goals';
import {deleteGoal, getGoals, GetGoalsFilter, GetGoalsResponse, Goal} from '../lib/api/goals';
import {Player, searchPlayers, SearchPlayersResponse} from '../lib/api/players';
import {getTeams, GetTeamsResponse} from '../lib/api/teams';

Expand Down Expand Up @@ -138,6 +138,24 @@ function Goals() {
});
}

function handleDeleteGoal(goal: Goal) {
deleteGoal(goal.id)
.then((data) => {
alert('Deleted ' + data.rowsAffected + ' goal(s)');
if (getGoalsResponse) {
const updatedGoals = getGoalsResponse.goals.filter((g) => g.id !== goal.id);
setGetGoalsResponse({
...getGoalsResponse,
goals: updatedGoals,
});
}
})
.catch((error) => {
const message = error?.response?.data?.message;
alert(message || error);
});
}

return (
<div id="home" role="tabpanel" aria-labelledby="home-tab">
<div className="mt-3">
Expand Down Expand Up @@ -211,7 +229,7 @@ function Goals() {

{getGoalsResponse?.goals?.map((goal) => (
<div key={goal.id} data-cy="goal-container" className="mb-4">
<Video goal={goal}></Video>
<Video goal={goal} onDelete={() => handleDeleteGoal(goal)}></Video>
</div>
))}

Expand Down
9 changes: 0 additions & 9 deletions src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,14 @@ function Settings() {
const [password, setPassword] = useState('');

useEffect(() => {
document.addEventListener('keydown', onKeyDown);
document.addEventListener('dblclick', onDoubleClick);
setLoggedIn(!!localStorage.getItem('top90-auth-token'));

return function cleanup() {
document.removeEventListener('keydown', onKeyDown);
document.removeEventListener('dblclick', onDoubleClick);
};
}, []);

function onKeyDown(event: KeyboardEvent) {
if ((event.metaKey || event.ctrlKey) && event.shiftKey && event.key === 'Enter') {
event.preventDefault();
setShowLogin((show) => !show);
}
}

function onDoubleClick() {
setShowLogin((show) => !show);
}
Expand Down

0 comments on commit dacd4b2

Please sign in to comment.