Skip to content

Commit

Permalink
adding delete release
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmartin committed Jul 24, 2023
1 parent 0e36239 commit 0c23ffe
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v14.20.0
v14.21.1
4 changes: 2 additions & 2 deletions src/components/Release/Reviews/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { useReleaseContext } from '@/context/release';
import { onDeleteRelease } from '@/context/release/api';
import { onDeleteReview } from '@/context/release/api';
import { WithCurrentUser } from '@/hooks/user';
import { Review, User } from '@/types/Data';
import { PropsWithChildren } from 'react';
Expand Down Expand Up @@ -62,7 +62,7 @@ const ReviewItem = (review: Review) => {
const { id, name, content, user_id, score } = review;

const onDelete = async (id: number) => {
await onDeleteRelease(dispatchRelease, appDispatch)(
await onDeleteReview(dispatchRelease, appDispatch)(
{
id,
releaseSlug: state?.release?.slug as string,
Expand Down
42 changes: 39 additions & 3 deletions src/components/Release/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ReleaseFormValues,
onEditRelease,
onCreateRelease,
onDeleteRelease,
} from '@/context/release/api';
import { useRouter } from 'next/router';
import { RichTextField } from '../Forms/Fields/RichTextField';
Expand All @@ -44,7 +45,7 @@ import { Head, SeoProps } from '../Head';
import { PLAYLIST_URL } from '../Playlist';
import moment from 'moment';
import { CreateSocialFormContainer } from '../Forms/SocialPost/create';
import ImagesExample from '../Forms/Fields/RichTextField/images';
// import ImagesExample from '../Forms/Fields/RichTextField/images';
// import ImagesExample from '../Forms/Fields/RichTextField/images';

const Calendar =
Expand Down Expand Up @@ -139,15 +140,50 @@ export const ReleasePage = ({ isNew }: Partial<ServerSideWithAdminArgs>) => {
ogImage: image.square,
};

const onDelete = async () =>
await onDeleteRelease(dispatch, appDispatch)(
{
releaseSlug: release?.slug as string,
},
() => {
console.log(`deleted`);
},
);

return (
<>
<Head {...seo} />
<MaybeForm
Footer={<FormFooter isFixed actionName="Edit" />}
Footer={
<FormFooter
AdditionalActions={
!isNew ? (
<button
type="button"
onClick={async (event) => {
event.preventDefault();

try {
const success = await onDelete();
if (success) {
push(`/releases`);
}
} catch (error) {}
}}
className="btn justify-start self-start btn-danger"
>
Delete
</button>
) : undefined
}
isFixed
actionName="Edit"
/>
}
handleSubmit={formik.handleSubmit}
>
<div className={styles.ReleasePage}>
<ImagesExample />
{/* <ImagesExample /> */}
<header className={styles.ReleasePageHeader}>
<div className={styles.ReleasePageTitle}>
<MaybeField<ReleaseFormValues>
Expand Down
53 changes: 51 additions & 2 deletions src/context/release/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export type DeleteReviewFormValues = {
releaseSlug: Release['slug'];
};

export type DeleteReleaseFormValues = {
releaseSlug: Release['slug'];
};

/**
* api helpers for release
*/
Expand Down Expand Up @@ -212,12 +216,12 @@ export const onEditReview: OnCreateReview<
}
};

type OnDeleteRelease<Action, Values> = (
type OnDeleteReview<Action, Values> = (
dispatch: Dispatch<Action>,
appDispatch: Dispatch<AppAction>,
) => (values: Values, onSuccess: () => void) => Promise<void>;

export const onDeleteRelease: OnDeleteRelease<
export const onDeleteReview: OnDeleteReview<
ReleaseAction,
DeleteReviewFormValues
> = (dispatch, appDispatch) => async (values, onSuccess) => {
Expand Down Expand Up @@ -255,6 +259,51 @@ export const onDeleteRelease: OnDeleteRelease<
}
};

type OnDeleteRelease<Action, Values> = (
dispatch: Dispatch<Action>,
appDispatch: Dispatch<AppAction>,
) => (values: Values, onSuccess: () => void) => Promise<boolean>;

export const onDeleteRelease: OnDeleteRelease<
ReleaseAction,
DeleteReleaseFormValues
> = (dispatch, appDispatch) => async (values, onSuccess) => {
const { releaseSlug: slug } = values;
if (!CONFIRM(`NO_UNDO`)) {
return false;
}
dispatch({
type: `start`,
fetchType: `review`,
isFetching: true,
});

try {
const { data } = await AXIOS().instance.delete<Review>(`releases/${slug}`);

onSuccess();

dispatch({
type: `successDeleteReview`,
fetchType: `review`,
data,
});

toast(`release deleted!`);
return true;
} catch (error: any) {
actionHelperCatch(error, appDispatch, () => {
dispatch(
genericErrorAction<ReleaseAction, FetchType>(
`review`,
error.toString(),
),
);
});
return false;
}
};

type onGetReleases<Action> = (
dispatch: Dispatch<Action>,
params: Record<string, any>,
Expand Down
4 changes: 4 additions & 0 deletions src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ a {
.btn-secondary {
@apply transition-all bg-gray-200 text-gray-700;
}

.btn-danger {
@apply transition-all bg-red-500 text-white;
}
.btn-secondary:hover {
@apply bg-gray-500 text-white;
}
Expand Down
1 change: 1 addition & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
emerald: colors.emerald,
indigo: colors.indigo,
yellow: colors.yellow,
red: colors.red,
// https://www.tailwindshades.com/
pink: {
DEFAULT: '#AC1E8C',
Expand Down

0 comments on commit 0c23ffe

Please sign in to comment.