Skip to content

Commit

Permalink
Delete a favorite deck.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaustinSpayce committed Apr 11, 2023
1 parent 5b64300 commit d3a3937
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/appConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ export const URL_END_POINT = {
SIGNUP: `AccountFiles/SignupAPI.php`,
FORGOT_PASSWORD: `AccountFiles/PasswordResetRequestAPI.php`,
RESET_PASSWORD: `AccountFiles/ResetPassword.php`,
GET_COSMETICS: `APIs/GetCosmetics.php`
GET_COSMETICS: `APIs/GetCosmetics.php`,
DELETE_DECK: `APIs/DeleteDeckAPI.php`
};

export const GAME_VISIBILITY = {
Expand Down
35 changes: 25 additions & 10 deletions src/features/api/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
FetchArgs,
FetchBaseQueryError
} from '@reduxjs/toolkit/query/react';
import { isRejected, isRejectedWithValue } from '@reduxjs/toolkit';
import { isRejectedWithValue } from '@reduxjs/toolkit';
import type { MiddlewareAPI, Middleware } from '@reduxjs/toolkit';
import { RootState } from 'app/Store';
import {
Expand All @@ -30,18 +30,23 @@ import { ChooseFirstPlayer } from 'interface/API/ChooseFirstPlayer.php';
import { SubmitSideboardAPI } from 'interface/API/SubmitSideboard.php';
import { GetFavoriteDecksResponse } from 'interface/API/GetFavoriteDecks.php';
import { GameListResponse } from 'routes/index/components/gameList/GameList';
import { ProcessInputAPI } from 'interface/API/ProcessInputAPI';
import { GetCosmeticsResponse } from 'interface/API/GetCosmeticsResponse.php';
import {
DeleteDeckAPIRequest,
DeleteDeckAPIResponse
} from 'interface/API/DeleteDeckAPI.php';

// catch warnings and show a toast if we get one.
export const rtkQueryErrorToaster: Middleware =
(api: MiddlewareAPI) => (next) => (action) => {
// if (isRejectedWithValue(action)) {
// console.warn('Rejected action:', action);
// const errorMessage = action.error?.message ?? 'an error happened';
// const errorStatus = action.payload?.status ?? 0;
// toast.error(`Error: ${errorStatus} - ${errorMessage}`);
// }
if (isRejectedWithValue(action)) {
console.warn('Rejected action:', action);
const errorMessage = action.error?.message ?? 'an error happened';
const errorStatus = action.payload?.status ?? 0;
toast.error(
`A network error happened, please try again. Error:\n${errorStatus}\n${errorMessage}`
);
}
return next(action);
};

Expand Down Expand Up @@ -203,8 +208,17 @@ export const apiSlice = createApi({
return response;
}
}),
deleteDeck: builder.mutation<DeleteDeckAPIResponse, DeleteDeckAPIRequest>({
query: (body: DeleteDeckAPIRequest) => {
return {
url: URL_END_POINT.DELETE_DECK,
method: 'post',
body: body
};
}
}),
createGame: builder.mutation<CreateGameResponse, CreateGameAPI>({
query: ({ ...body }: CreateGameAPI) => {
query: (body: CreateGameAPI) => {
return {
url: URL_END_POINT.CREATE_GAME,
method: 'POST',
Expand All @@ -216,7 +230,7 @@ export const apiSlice = createApi({
response.status
}),
joinGame: builder.mutation<JoinGameResponse, JoinGameAPI>({
query: ({ ...body }: JoinGameAPI) => {
query: (body: JoinGameAPI) => {
return {
url: URL_END_POINT.JOIN_GAME,
method: 'POST',
Expand Down Expand Up @@ -269,6 +283,7 @@ export const {
useGetGameListQuery,
useGetCosmeticsQuery,
useGetFavoriteDecksQuery,
useDeleteDeckMutation,
useLoginMutation,
useLoginWithCookieQuery,
useLogOutMutation,
Expand Down
7 changes: 7 additions & 0 deletions src/interface/API/DeleteDeckAPI.php.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface DeleteDeckAPIRequest {
deckLink: string;
}

export interface DeleteDeckAPIResponse {
message?: string;
}
1 change: 1 addition & 0 deletions src/interface/API/GetFavoriteDecks.php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface FavoriteDeck {
format: string;
cardBack: string;
playmat: string;
link: string;
}
45 changes: 40 additions & 5 deletions src/routes/user/profile/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import { useGetFavoriteDecksQuery } from 'features/api/apiSlice';
import {
useDeleteDeckMutation,
useGetFavoriteDecksQuery
} from 'features/api/apiSlice';
import { DeleteDeckAPIResponse } from 'interface/API/DeleteDeckAPI.php';
import { toast } from 'react-hot-toast';
import { GiTrashCan } from 'react-icons/gi';
import styles from './profile.module.css';

export const ProfilePage = () => {
const { data, isLoading } = useGetFavoriteDecksQuery(undefined);
const { data, isLoading, refetch } = useGetFavoriteDecksQuery(undefined);
const [deleteDeck] = useDeleteDeckMutation();

const handleButtonClick = (deckKey: string) => {
console.log('clicked this one', deckKey);
const handleDeleteDeckMessage = (resp: DeleteDeckAPIResponse): string => {
if (resp.message !== 'Deck deleted successfully.') {
return 'The deck has been removed from your favorites list. It is still available to view on the deckbuilding site.';
} else {
return 'There has been a problem deleting your deck, please try again.';
}
};

const handleDeleteDeck = async (deckLink: string) => {
try {
const deleteDeckPromise = deleteDeck({ deckLink }).unwrap();
toast.promise(
deleteDeckPromise,
{
loading: 'Deleting deck...',
success: (data) => handleDeleteDeckMessage(data),
error: (err) =>
`There has been an error, please try again. Error: ${err.toString()}`
},
{
style: {
minWidth: '250px'
},
position: 'top-center'
}
);
const resp = await deleteDeckPromise;
} catch (err) {
} finally {
refetch();
}
};

return (
Expand Down Expand Up @@ -51,7 +86,7 @@ export const ProfilePage = () => {
<td className={styles.deleteButton}>
<button
className={styles.button}
onClick={() => handleButtonClick(deck.key)}
onClick={() => handleDeleteDeck(deck.link)}
>
<GiTrashCan
fontSize={'1.5em'}
Expand Down

0 comments on commit d3a3937

Please sign in to comment.