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

61 article resource forms #73

Open
wants to merge 29 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
811bd3f
created branch.
angeliinawang Feb 5, 2025
8f96089
finished resource ui
angeliinawang Feb 6, 2025
5bee44d
Started backend.
angeliinawang Feb 6, 2025
ea58acc
create video
alexespejo Feb 6, 2025
17bd5a3
Merge branch '61-article-resource-forms' of https://github.com/ctc-uc…
alexespejo Feb 6, 2025
db501f1
working search and creat videos
alexespejo Feb 6, 2025
d69e8d3
non-case sensitive search
alexespejo Feb 6, 2025
30013b0
peak form validation code
alexespejo Feb 6, 2025
4c2f8da
cosmetic changes just bc
alexespejo Feb 6, 2025
d04ef74
working article form
alexespejo Feb 6, 2025
2bb7b3e
semi-working article search
alexespejo Feb 6, 2025
d306f1c
finished articles form, slight problem with submission on s3
angeliinawang Feb 7, 2025
51e9f9b
fixed merge conflict
angeliinawang Feb 7, 2025
3bc7b1a
created branch.
angeliinawang Feb 5, 2025
6aebf30
create video
alexespejo Feb 6, 2025
18042e5
finished resource ui
angeliinawang Feb 6, 2025
1e97e05
Started backend.
angeliinawang Feb 6, 2025
de4407f
working search and creat videos
alexespejo Feb 6, 2025
c6d6eda
non-case sensitive search
alexespejo Feb 6, 2025
ec3e5d8
peak form validation code
alexespejo Feb 6, 2025
8974c8e
cosmetic changes just bc
alexespejo Feb 6, 2025
d282d72
working article form
alexespejo Feb 6, 2025
9550366
semi-working article search
alexespejo Feb 6, 2025
9ee96c1
finished articles form, slight problem with submission on s3
angeliinawang Feb 7, 2025
d215839
fixed merge conflict
angeliinawang Feb 7, 2025
8e88979
Merge branch '61-article-resource-forms' of https://github.com/ctc-uc…
h0ethan04 Feb 11, 2025
3aecaa8
i did something(?)
h0ethan04 Feb 11, 2025
566983f
Merge branch 'dev' into 61-article-resource-forms
h0ethan04 Feb 19, 2025
32e2c3a
Remove classes from useEffect dep array
h0ethan04 Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions client/.env.example

This file was deleted.

185 changes: 185 additions & 0 deletions client/src/components/forms/createArticle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { useState } from "react";

import {
Box,
Button,
Checkbox,
FormControl,
FormErrorMessage,
FormLabel,
Input,
Stack,
} from "@chakra-ui/react";

import { z } from "zod";

import { useBackendContext } from "../../contexts/hooks/useBackendContext";

const createArticleSchema = z.object({
s3_url: z.string(),
media_url: z.string().url("Please enter a valid URL."),
tag: z.array(z.string()).min(1, "You must include at least one tag."),
description: z.string().min(1, "Please write a description of the article."),
title: z.string().min(1, "Your article must include a title."),
});

type ArticleFormValues = z.infer<typeof createArticleSchema>;

const CreateArticle = () => {
const [articleData, setArticleData] = useState<ArticleFormValues | null>({
s3_url: "",
media_url: "",
tag: [],
description: "",
title: "",
});
const [errors, setErrors] = useState({
s3_url: false,
media_url: false,
tag: false,
title: false,
});
const validateForm = () => {
const newErrors = {
title: articleData?.title.trim() === "",
media_url: articleData?.media_url.trim() === "",
s3_url: articleData?.s3_url.trim() === "",
tag: articleData?.tag.length === 0,
};
setErrors(newErrors);

// Returns true if there are no errors
return (
!newErrors.title &&
!newErrors.media_url &&
!newErrors.s3_url &&
!newErrors.tag
);
};
const { backend } = useBackendContext();
const postArticle = async () => {
console.log(validateForm());
if (!validateForm()) return;
try {
const res = await backend.post("/articles", {
s3_url: articleData?.s3_url ?? "",
media_url: articleData?.media_url ?? "",
description: articleData?.tag.join(", ") ?? "",
});

if (!res) {
throw new Error("Failed to submit article.");
}

console.log("Successfully submitted article.");
alert("Article submitted successfully!");
} catch (error) {
console.log("Error submitting article:", error);
alert("Failed to submit article.");
}
};

const [selectTags, setSelectTags] = useState<string[]>([]);

const tags = ["Ballet", "Classical", "Custom"];

const handleTags = (tag: string) => {
let newTags;
if (selectTags.includes(tag)) {
newTags = selectTags.filter((t) => t !== tag);
} else {
newTags = [...selectTags, tag];
}

setSelectTags(newTags);
setArticleData({ ...articleData!, tag: newTags });
// setValue("tag", newTags, { shouldValidate: true });
};

return (
<Stack
spacing={8}
sx={{ width: 300, marginX: "auto" }}
>
<form
onSubmit={(e) => {
e.preventDefault();
postArticle();
}}
>
<FormControl isInvalid={!!errors.s3_url}>
<FormLabel>Select media to upload.</FormLabel>
<Input
placeholder="s3 URL"
size={"lg"}
// {...register("s3_url")}
onChange={(e) =>
setArticleData({ ...articleData!, s3_url: e.target.value ?? "" })
}
name="photo"
isRequired
autoComplete="s3Url"
></Input>
<FormErrorMessage>{errors.s3_url?.toString()}</FormErrorMessage>
</FormControl>
<FormControl isInvalid={!!errors.media_url}>
<Input
placeholder="Media URL"
type="link"
size={"lg"}
// {...register("media_url")}
onChange={(e) =>
setArticleData({
...articleData!,
media_url: e.target.value ?? "",
})
}
name="media_url"
isRequired
autoComplete="media_url"
></Input>
<FormErrorMessage>{errors.media_url?.toString()}</FormErrorMessage>
</FormControl>
<FormControl isInvalid={!!errors.tag}>
<FormLabel>Select tags for media.</FormLabel>
<Stack direction="row">
{tags.map((tag) => (
<Button
key={tag}
onClick={() => handleTags(tag)}
>
{tag}
</Button>
))}
</Stack>
<FormErrorMessage>{errors.tag?.toString()}</FormErrorMessage>
</FormControl>
<FormControl isInvalid={!!errors.title}>
<FormLabel>Add a title.</FormLabel>
<Input
placeholder="Add title here."
type="title"
size={"lg"}
// {...register("title")}
onChange={(e) =>
setArticleData({ ...articleData!, title: e.target.value ?? "" })
}
name="title"
isRequired
autoComplete="title"
></Input>
<FormErrorMessage>{errors.title?.toString()}</FormErrorMessage>
</FormControl>
<Button
size={"lg"}
sx={{ width: "100% " }}
type="submit"
>
Submit
</Button>
</form>
</Stack>
);
};

export default CreateArticle;
Loading