Skip to content

Commit

Permalink
Merge pull request #1001 from funmusicplace/various-fixes
Browse files Browse the repository at this point in the history
fix: update draft stuff for posts
  • Loading branch information
simonv3 authored Feb 6, 2025
2 parents 21b8595 + 1c4f377 commit f781fab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
3 changes: 0 additions & 3 deletions client/src/components/ArtistColorsWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const colors = {
};

const updateTheme = (artistColors?: any, theme?: "dark" | "light") => {
console.log("updatinmg theme", artistColors, theme);
document.documentElement.setAttribute("data-mi-theme", theme ?? "light");
if (artistColors) {
// document.documentElement.style.setProperty(
Expand Down Expand Up @@ -83,8 +82,6 @@ const ArtistColorsWrapper: React.FC<{ children: React.ReactElement }> = ({
managedArtist?.properties?.colors ?? artist?.properties?.colors;

React.useEffect(() => {
console.log("prefersDark", prefersDark.matches);

updateTheme(artistColors, prefersDark.matches ? "dark" : "light");
}, [artistColors]);

Expand Down
7 changes: 4 additions & 3 deletions client/src/components/ManageArtist/Posts/PostForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ArtistButton } from "components/Artist/ArtistButtons";
import SavingInput from "../AlbumFormComponents/SavingInput";
import EditPostHeader from "./EditPostHeader";

type FormData = {
export type PostFormData = {
title: string;
publishedAt: string;
content: string;
Expand Down Expand Up @@ -61,7 +61,7 @@ const PostForm: React.FC<{
multiple: true,
});

const methods = useForm<FormData>({
const methods = useForm<PostFormData>({
defaultValues: post
? {
...post,
Expand Down Expand Up @@ -97,7 +97,8 @@ const PostForm: React.FC<{
const publicationDate = watch("publishedAt");

const doSave = React.useCallback(
async (data: FormData) => {
async (data: PostFormData) => {
console.log("saving");
if (userId) {
try {
setIsSaving(true);
Expand Down
69 changes: 44 additions & 25 deletions client/src/components/ManageArtist/Posts/PublishPostButton.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ArtistButton } from "components/Artist/ArtistButtons";
import { pick } from "lodash";
import React from "react";
import { useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import api from "services/api";
import { useSnackbar } from "state/SnackbarContext";
import { useConfirm } from "utils/useConfirm";
import useGetUserObjectById from "utils/useGetUserObjectById";
import { PostFormData } from "./PostForm";

const PublishPostButton: React.FC<{
post: Post;
Expand All @@ -15,7 +17,7 @@ const PublishPostButton: React.FC<{
const snackbar = useSnackbar();
const [isPublishing, setIsPublishing] = React.useState(false);
const { ask } = useConfirm();
const { watch } = useFormContext();
const { watch, handleSubmit } = useFormContext<PostFormData>();

const { reload: reloadImages } = useGetUserObjectById<PostImage>(
`manage/posts/${post?.id}/images`,
Expand All @@ -29,33 +31,50 @@ const PublishPostButton: React.FC<{

const existingId = post.id;

const doPublish = React.useCallback(async () => {
try {
setIsPublishing(true);
const doPublish = React.useCallback(
async (data: PostFormData) => {
console.log("publishing");
try {
setIsPublishing(true);

if (post.isDraft && (content === "" || content === "<p></p>")) {
const ok = await ask(t("contentIsEmpty"));
if (!ok) {
return;
if (post.isDraft && (content === "" || content === "<p></p>")) {
const ok = await ask(t("contentIsEmpty"));
if (!ok) {
return;
}
}
}

if (post.isDraft && title === "") {
const ok = await ask(t("titleIsEmpty"));
if (!ok) {
return;
if (post.isDraft && title === "") {
const ok = await ask(t("titleIsEmpty"));
if (!ok) {
return;
}
}
const picked = {
...pick(data, ["title", "content", "isPublic", "shouldSendEmail"]),
publishedAt: new Date(data.publishedAt + ":00").toISOString(),
artistId: post.artistId,
minimumSubscriptionTierId:
isFinite(+data.minimumTier) && +data.minimumTier !== 0
? Number(data.minimumTier)
: undefined,
};
await api.put<Partial<Post>, { result: { id: number } }>(
`manage/posts/${existingId}`,
picked
);
await api.put(`manage/posts/${existingId}/publish`, {});
reload(existingId);
reloadImages();
snackbar(t("publishedPost"), { type: "success" });
} catch (e) {
console.error(e);
} finally {
setIsPublishing(false);
}
await api.put(`manage/posts/${existingId}/publish`, {});
reload(existingId);
reloadImages();
snackbar(t("publishedPost"), { type: "success" });
} catch (e) {
console.error(e);
} finally {
setIsPublishing(false);
}
}, [existingId, title, content, isDraft]);
},
[existingId, title, content, isDraft]
);

const minimumTier = watch("minimumTier");
const publicationDate = watch("publishedAt");
Expand All @@ -72,8 +91,8 @@ const PublishPostButton: React.FC<{
<ArtistButton
disabled={!minimumTier || !publicationDate}
isLoading={isPublishing}
onClick={doPublish}
type="button"
onClick={handleSubmit(doPublish)}
type="submit"
>
{publishText}
</ArtistButton>
Expand Down

0 comments on commit f781fab

Please sign in to comment.