Skip to content

Development laptop - User auth #6

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

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
79 changes: 39 additions & 40 deletions client/components/article/ArticleAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { QuillEditor, Delta, Quill } from "@vueup/vue-quill";
import "@vueup/vue-quill/dist/vue-quill.snow.css";
import { ADD_ARTICLE_RAW } from '../../graphql/articles';

const isReadOnly = true;
const isReadOnly = false;

const articleState = reactive({
title: '',
Expand Down Expand Up @@ -90,45 +90,44 @@ const props = defineProps(["categories", "authors"]);

const handleArticleAdd = async (e: Event) => {
e.preventDefault();
// const formData = {
// title: articleState.title,
// content: state.content,
// category: articleState.category,
// author: articleState.author,
// link: articleState.link,
// };
const formData = new FormData();


const myHeaders = new Headers();
myHeaders.append("Cookie", "csrftoken=ccS5qh2RZofjzKhe6KeN51RMYOGQAb5t");

const newImgFile = uploadedImg.value as File;

const operations = {
query: ADD_ARTICLE_RAW,
variables: {
title: articleState.title,
content: state.content,
thumbnail: null, // You may need to handle thumbnail separately based on your requirements
authorId: articleState.author,
categoryId: articleState.category,
},
};

formData.set("operations", JSON.stringify(operations));
formData.set("map", "{\n \"0\": [\"variables.thumbnail\"]\n}");
formData.set("0", newImgFile);

const response = await fetch("http://localhost:8000/graphql/", {
method: 'POST',
headers: myHeaders,
body: formData,
redirect: 'follow'
});

console.log(response);

try {
const formData = new FormData();


const myHeaders = new Headers();
myHeaders.append("Cookie", "csrftoken=ccS5qh2RZofjzKhe6KeN51RMYOGQAb5t");

const newImgFile = uploadedImg.value as File;

const operations = {
query: ADD_ARTICLE_RAW,
variables: {
title: articleState.title,
content: state.content,
thumbnail: null, // You may need to handle thumbnail separately based on your requirements
// authorId: articleState.author,
authorId: 1,
categoryId: articleState.category,
},
};

formData.set("operations", JSON.stringify(operations));
formData.set("map", "{\n \"0\": [\"variables.thumbnail\"]\n}");
formData.set("0", newImgFile);

const response = await fetch("http://localhost:8000/graphql/", {
method: 'POST',
headers: myHeaders,
body: formData,
redirect: 'follow'
});

console.log(response);
} catch (error) {
console.log("Error adding article", error);

}

};

const handleFileChange = (e: Event) => {
Expand Down
35 changes: 34 additions & 1 deletion client/components/category/CategoryAdd.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
<template>
<h1>Category Add</h1>
</template>
<form v-on:submit.prevent="handleCategoryAdd">
<input type="text" v-model="categoryName" class="border border gray-300 p-2" />
<button class="bg-gray-900 text-gray-100 p-2" type="submit">Add Category</button>
</form>
</template>

<script setup lang="ts">
import { ref } from 'vue';
// import { useMutation, gql } from '@urql/vue';

const categoryName = ref('');

const ADD_CATEGORY = gql`
mutation AddCategory($name: String!) {
createOrUpdateCategory(name: $name) {
category {
id
name
}
}
}
`;

const { mutate: addCategory } = useMutation(ADD_CATEGORY);

const handleCategoryAdd = async () => {
try {
const categoryRes = await addCategory({ name: categoryName.value });
console.log({ categoryRes });
} catch (error) {
console.error('Error adding category:', error);
}
};
</script>
4 changes: 2 additions & 2 deletions client/components/home/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</div>
</section>

<Posts v-bind:allArticles="data.allArticles" v-bind:firstArticle="data.allArticles[0]" />
<Posts v-if="data.allArticles && data.allArticles.length > 0" v-bind:allArticles="data.allArticles" v-bind:firstArticle="data.allArticles[0]" />
</div>
</template>

Expand Down Expand Up @@ -48,6 +48,6 @@

const variables = { limit: 20 };
const { data } = await useAsyncQuery<ArticlesResult>(query, variables);
// console.log({ allArticles: data.value.allArticles });
console.log({ allArticles: data.value.allArticles });
</script>

Loading