Finish to setup data access
This commit is contained in:
parent
90c998fa41
commit
80f72e7dc9
10 changed files with 73 additions and 36 deletions
|
|
@ -1,7 +1,11 @@
|
|||
The project is based on [Kirby CMS](https://getkirby.com), [Vite](https://vitejs.dev/) + [Vue](https://fr.vuejs.org/) and [Pinia](https://pinia.vuejs.org/) as a state manager.
|
||||
Vue is used following the composition API approach.
|
||||
|
||||
## First setup :
|
||||
# Development
|
||||
|
||||
## Development environment
|
||||
|
||||
### First setup :
|
||||
|
||||
- **From the `/public` directory**, install the Kirby dependencies : `composer install`
|
||||
- **From the root directory**, install the Node dependencies : `npm install`
|
||||
|
|
@ -15,7 +19,7 @@ VITE_USERNAME=mail@example.com
|
|||
VITE_PASSWORD=your-private-password
|
||||
```
|
||||
|
||||
## Development environment
|
||||
### Servers
|
||||
|
||||
- **From the `/public` directory**, launch the PHP server : `php -S localhost:8888 kirby/router.php`
|
||||
- In another terminal tab, **from the root directory**, launch the Vite server : `vite`
|
||||
|
|
|
|||
9
public/site/controllers/site.php
Normal file
9
public/site/controllers/site.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
return function ($page) {
|
||||
$data = $page->toArray();
|
||||
$data['template'] = (string) $page->template();
|
||||
return [
|
||||
'genericData' => $data,
|
||||
];
|
||||
};
|
||||
|
|
@ -1,8 +1,3 @@
|
|||
<script>
|
||||
const kirbyData = {
|
||||
pageUri: '<?= $page->uri() ?>',
|
||||
pageTemplate: '<?= $page->template() ?>',
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
3
public/site/snippets/generic-template.php
Normal file
3
public/site/snippets/generic-template.php
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<?php snippet('header') ?>
|
||||
<div id="app"></div>
|
||||
<?php snippet('footer') ?>
|
||||
7
public/site/templates/home.json.php
Normal file
7
public/site/templates/home.json.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
$specificData = [];
|
||||
|
||||
$data = array_merge($genericData, $specificData);
|
||||
|
||||
echo json_encode($data);
|
||||
|
|
@ -1,3 +1 @@
|
|||
<?php snippet('header') ?>
|
||||
<div id="app"></div>
|
||||
<?php snippet('footer') ?>
|
||||
<?php snippet('generic-template') ?>
|
||||
30
src/App.vue
30
src/App.vue
|
|
@ -1,23 +1,17 @@
|
|||
<script setup>
|
||||
import Home from "./views/Home.vue";
|
||||
import { useApiStore } from "./stores/api";
|
||||
<template>
|
||||
<component :is="components[data.template]" v-if="data" :data="data" />
|
||||
</template>
|
||||
|
||||
const templates = {
|
||||
home: {
|
||||
component: Home,
|
||||
query: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
<script setup>
|
||||
import home from "./views/Home.vue";
|
||||
import { useApiStore } from "./stores/api";
|
||||
import { ref } from "vue";
|
||||
|
||||
const components = {
|
||||
home,
|
||||
};
|
||||
|
||||
const data = ref(null);
|
||||
const api = useApiStore();
|
||||
const pageTemplate = kirbyData.pageTemplate;
|
||||
const pageUri = kirbyData.pageUri;
|
||||
|
||||
api.fetchData();
|
||||
api.fetchPageData().then((res) => (data.value = res));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<component :is="templates[pageTemplate].component" />
|
||||
</template>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,29 @@
|
|||
import { defineStore } from "pinia";
|
||||
|
||||
export const useApiStore = defineStore("counter", () => {
|
||||
function fetchData() {
|
||||
async function fetchPageData() {
|
||||
const isHomePage = window.location.pathname === "/";
|
||||
const url = isHomePage
|
||||
? `${window.location.href}home.json`
|
||||
: `${window.location.href}.json`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Une erreur s'est produite lors de la récupération des données :",
|
||||
error
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function fetchDataThroughKQL() {
|
||||
const api = "/api/query";
|
||||
|
||||
const username = import.meta.env.VITE_USERNAME;
|
||||
|
|
@ -42,5 +64,5 @@ export const useApiStore = defineStore("counter", () => {
|
|||
});
|
||||
}
|
||||
|
||||
return { fetchData };
|
||||
return { fetchDataThroughKQL, fetchPageData };
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,3 +1,8 @@
|
|||
<script setup></script>
|
||||
<template></template>
|
||||
<style scoped></style>
|
||||
<template>
|
||||
<h1>{{ data.content.title }}</h1>
|
||||
</template>
|
||||
<script setup>
|
||||
const { data } = defineProps({
|
||||
data: Object,
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue