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

Draft posts #142

Merged
merged 9 commits into from
Aug 28, 2024
6 changes: 2 additions & 4 deletions src/components/common/cart/CartAuthMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ export const CartAuthMode: Component<Props> = (props) => {
<button
class="btn-secondary max-w-1/2 inline"
onclick={props.checkoutAsGuest}
// TODO: INTERNATIONALIZE
aria-label="checkout as guest"
aria-label={t("buttons.checkoutAsGuest")}
>
{/* TODO: Internationalize*/}
Checkout as Guest
{t("buttons.checkoutAsGuest")}
</button>
<form class="max-w-1/2 inline">
<button
Expand Down
2 changes: 2 additions & 0 deletions src/components/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const Home: Component = () => {
.select("*")
.order("id", { ascending: true })
.eq("listing_status", true)
.eq("draft_status", false)
.limit(8);
if (!data) {
alert("No posts available.");
Expand Down Expand Up @@ -89,6 +90,7 @@ export const Home: Component = () => {
.from("sellerposts")
.select("*")
.eq("listing_status", true)
.eq("draft_status", false)
.order("id", { ascending: false })
.limit(8);

Expand Down
27 changes: 27 additions & 0 deletions src/components/posts/CreateNewPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import stripe from "../../lib/stripe";
import Dropdown from "@components/common/Dropdown";
import { UploadFiles } from "@components/posts/UploadResource";
import tinymce from "tinymce";
import { sortResourceTypes } from "@lib/utils/resourceSort";

const lang = getLangFromUrl(new URL(window.location.href));
const t = useTranslations(lang);
Expand Down Expand Up @@ -194,6 +195,7 @@ export const CreateNewPost: Component = () => {
const [isAdmin, setIsAdmin] = createSignal<boolean>(false);
const [isHosted, setIsHosted] = createSignal<boolean>(false);
const [resourceLinks, setResourceLinks] = createSignal<string>("");
const [draftStatus, setDraftStatus] = createSignal<boolean>(false);

onMount(() => {
window.addEventListener("storage", (event) => {
Expand Down Expand Up @@ -290,6 +292,7 @@ export const CreateNewPost: Component = () => {
if (error) {
console.log("supabase error: " + error.message);
} else {
sortResourceTypes(resourceType);
resourceType.forEach((type) => {
setResourceTypes([
...resourceTypes(),
Expand Down Expand Up @@ -380,12 +383,25 @@ export const CreateNewPost: Component = () => {
}
});

function saveAsDraft(e: Event) {
e.preventDefault();
setDraftStatus(true);

const button = e.currentTarget as HTMLFormElement;
const form = button.closest("form") as HTMLFormElement;

if (form) {
form.requestSubmit();
}
}

async function submit(e: SubmitEvent) {
e.preventDefault();

const formData = new FormData(e.target as HTMLFormElement);
formData.append("access_token", session()?.access_token!);
formData.append("refresh_token", session()?.refresh_token!);
formData.append("draft_status", JSON.stringify(draftStatus()));
formData.append("lang", lang);
if (isFree()) {
setPrice("0");
Expand Down Expand Up @@ -1338,6 +1354,17 @@ export const CreateNewPost: Component = () => {

<br />
<div class="flex justify-center">
<button
id="save-as-draft"
class={`btn-primary text-2xl`}
onclick={(e) => {
e.preventDefault();
e.stopPropagation();
saveAsDraft(e);
}}
>
Save as Draft
</button>
<button
id="post"
disabled={!allRequirementsMet()}
Expand Down
94 changes: 86 additions & 8 deletions src/components/posts/EditPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getLangFromUrl, useTranslations } from "../../i18n/utils";
import { TinyComp } from "./TinyComp";
import { createStore } from "solid-js/store";
import type { Post } from "@lib/types";
import { sortResourceTypes } from "@lib/utils/resourceSort";

const lang = getLangFromUrl(new URL(window.location.href));
const t = useTranslations(lang);
Expand Down Expand Up @@ -74,6 +75,9 @@ export const EditPost: Component<Props> = (props: Props) => {
const [resourceExpanded, setResourceExpanded] =
createSignal<boolean>(false);
const [secular, setSecular] = createSignal<boolean>(false);
const [draftStatus, setDraftStatus] = createSignal<boolean>(false);
const [startDraftStatus, setStartDraftStatus] =
createSignal<boolean>(false);

onMount(async () => {
console.log(props.post);
Expand All @@ -94,6 +98,8 @@ export const EditPost: Component<Props> = (props: Props) => {
setSubjectPick(props.post.product_subject);
setResourceTypesPick(props.post?.resource_types!);
setSecular(props.post.secular);
setDraftStatus(props.post.draft_status);
setStartDraftStatus(props.post.draft_status);

if (props.post?.image_urls) {
setImageUrl(props.post?.image_urls.split(","));
Expand Down Expand Up @@ -136,6 +142,7 @@ export const EditPost: Component<Props> = (props: Props) => {
if (error) {
console.log("supabase error: " + error.message);
} else {
sortResourceTypes(resourceType);
resourceType.forEach((type) => {
setResourceTypes([
...resourceTypes(),
Expand Down Expand Up @@ -199,6 +206,32 @@ export const EditPost: Component<Props> = (props: Props) => {
}
});

function saveAsDraft(e: Event) {
e.preventDefault();
e.stopPropagation();
setDraftStatus(true);

const button = e.currentTarget as HTMLFormElement;
const form = button.closest("form") as HTMLFormElement;

if (form) {
form.requestSubmit();
}
}

function listResourcePost(e: Event) {
e.preventDefault();
e.stopPropagation();

const button = e.currentTarget as HTMLFormElement;
const form = button.closest("form") as HTMLFormElement;

if (form) {
setDraftStatus(false);
form.requestSubmit();
}
}

async function submit(e: SubmitEvent) {
e.preventDefault();

Expand All @@ -217,6 +250,7 @@ export const EditPost: Component<Props> = (props: Props) => {
tmpDiv.innerHTML = formData.get("Content") as string;
let description = tmpDiv.textContent || tmpDiv.innerText || "";
formData.append("description", description);
formData.append("draft_status", JSON.stringify(draftStatus()));

if (subjectPick() !== undefined) {
formData.append("subject", JSON.stringify(subjectPick()));
Expand Down Expand Up @@ -440,6 +474,49 @@ export const EditPost: Component<Props> = (props: Props) => {
}
}

function postButton() {
if (startDraftStatus() === false) {
return (
<>
<button
id="post"
disabled={!allRequirementsMet()}
class={`text-2xl ${
allRequirementsMet()
? "btn-primary"
: "btn-disabled"
}`}
>
{t("buttons.updateResource")}
</button>
</>
);
} else if (startDraftStatus() === true) {
return (
<>
<button
id="post"
disabled={!allRequirementsMet()}
class={`text-2xl ${
allRequirementsMet()
? "btn-primary"
: "btn-disabled"
}`}
onclick={(e) => {
e.preventDefault();
e.stopPropagation();
listResourcePost(e);
}}
>
{t("buttons.listResource")}
</button>
</>
);
} else {
return <></>;
}
}

return (
<div>
<form onSubmit={submit}>
Expand Down Expand Up @@ -1064,16 +1141,17 @@ export const EditPost: Component<Props> = (props: Props) => {
<br />
<div class="flex justify-center">
<button
id="post"
disabled={!allRequirementsMet()}
class={`text-2xl ${
allRequirementsMet()
? "btn-primary"
: "btn-disabled"
}`}
id="save-as-draft"
class={`btn-primary text-2xl`}
onclick={(e) => {
e.preventDefault();
e.stopPropagation();
saveAsDraft(e);
}}
>
{t("buttons.updateResource")}
Save as Draft
</button>
{postButton()}
</div>
</form>
</div>
Expand Down
Loading
Loading