mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Added full Article creation pathway
This commit is contained in:
@@ -21,4 +21,16 @@ export async function getArticle(id: string) : Promise<Article> {
|
|||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function postArticle(article: Article) : Promise<object> {
|
||||||
|
return await fetch(`${baseURL}/articles/${article.idName}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { "Authorization": `Bearer ${useAuthStore().token}` },
|
||||||
|
body: JSON.stringify(article)
|
||||||
|
})
|
||||||
|
.then(result => result.json())
|
||||||
|
.catch(error => {
|
||||||
|
console.error(error)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,63 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { RouterLink } from 'vue-router'
|
import { postArticle, type Article } from '@/utils/database';
|
||||||
|
import { ref, type Ref } from 'vue';
|
||||||
|
import { RouterLink, useRouter } from 'vue-router'
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
const defaultData = `<h1>Bienvenue</h1><p>Vous êtes en mode <em>édition</em> d'article.</p><p>Celui-ci semble encore neuf ! Supprimez ces lignes et laissez libre cours à votre imagination :)</p><p>Pour plus d'infos, rendez-vous sur la <a title="Attention, rickroll incoming" href="https:/www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank" rel="noopener">page d'aide</a>.</p>`
|
const defaultData = `<h1>Bienvenue</h1><p>Vous êtes en mode <em>édition</em> d'article.</p><p>Celui-ci semble encore neuf ! Supprimez ces lignes et laissez libre cours à votre imagination :)</p><p>Pour plus d'infos, rendez-vous sur la <a title="Attention, rickroll incoming" href="https:/www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank" rel="noopener">page d'aide</a>.</p>`
|
||||||
|
const title = ref('')
|
||||||
|
const page = ref('')
|
||||||
|
|
||||||
|
function createArticle() {
|
||||||
|
if (!isFormValid()) {
|
||||||
|
alert("L'identifiant de l'article ne doit contenir que des caractères alphanumériques (aA-zZ, 1-9) !")
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const article: Article = {
|
||||||
|
idName: title.value,
|
||||||
|
date: new Date().getTime(),
|
||||||
|
content: {
|
||||||
|
html: defaultData
|
||||||
|
},
|
||||||
|
online: false,
|
||||||
|
pageId: page.value
|
||||||
|
}
|
||||||
|
postArticle(article)
|
||||||
|
router.push(`/articles/edit/${article.idName}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const isFormEmpty: () => boolean = () => {
|
||||||
|
return title.value === '' || page.value === ''
|
||||||
|
}
|
||||||
|
|
||||||
|
function isFormValid(): boolean {
|
||||||
|
const regex = /^[a-zA-Z0-9]+$/
|
||||||
|
return regex.test(title.value)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<main>
|
<main>
|
||||||
<h2>Créer un nouvel article</h2>
|
<h2>Créer un nouvel article</h2>
|
||||||
<form @submit.prevent="">
|
<form @submit.prevent="createArticle()">
|
||||||
<div class="inputs-group">
|
<div class="inputs-group">
|
||||||
<div class="label-input">
|
<div class="label-input">
|
||||||
<label for="title">Titre de l'article</label>
|
<label for="title">Identifiant de l'article</label>
|
||||||
<input id="title" name="title" placeholder="" type="text">
|
<input id="title" name="title" placeholder="Identifiant de l'article" type="text" v-model="title">
|
||||||
<p>⚠️ Ce titre servira d'identifiant, il doit être unique !</p>
|
<p>⚠️ Doit être unique et composé de caractères alphanumériques !</p>
|
||||||
|
<!-- TODO: Remplacer alerte pas ouf par passage du champ en erreur -->
|
||||||
</div>
|
</div>
|
||||||
<div class="label-input">
|
<div class="label-input">
|
||||||
<label for="page">Page</label>
|
<label for="page">Page</label>
|
||||||
<input id="page" name="page" placeholder="" type="text">
|
<input id="page" name="page" placeholder="Page" type="text" v-model="page">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="buttons-group">
|
<div class="buttons-group">
|
||||||
<RouterLink class="button-secondary" type="submit" to="/articles">Annuler</RouterLink>
|
<RouterLink class="button-secondary" type="submit" to="/articles">Annuler</RouterLink>
|
||||||
<button class="button-primary" type="submit">Créer</button>
|
<button :class="`button-${isFormEmpty() ? 'disabled' : 'primary'}`" type="submit"
|
||||||
|
:disabled="isFormEmpty()">Créer</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</main>
|
</main>
|
||||||
|
|||||||
Reference in New Issue
Block a user