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.
|
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.
|
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 `/public` directory**, install the Kirby dependencies : `composer install`
|
||||||
- **From the root directory**, install the Node dependencies : `npm 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
|
VITE_PASSWORD=your-private-password
|
||||||
```
|
```
|
||||||
|
|
||||||
## Development environment
|
### Servers
|
||||||
|
|
||||||
- **From the `/public` directory**, launch the PHP server : `php -S localhost:8888 kirby/router.php`
|
- **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`
|
- 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 = {
|
</body>
|
||||||
pageUri: '<?= $page->uri() ?>',
|
|
||||||
pageTemplate: '<?= $page->template() ?>',
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
</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') ?>
|
||||||
|
|
@ -1 +1 @@
|
||||||
<h1><?= $page->title() ?></h1>
|
<h1><?= $page->title() ?></h1>
|
||||||
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') ?>
|
<?php snippet('generic-template') ?>
|
||||||
<div id="app"></div>
|
|
||||||
<?php snippet('footer') ?>
|
|
||||||
30
src/App.vue
30
src/App.vue
|
|
@ -1,23 +1,17 @@
|
||||||
<script setup>
|
<template>
|
||||||
import Home from "./views/Home.vue";
|
<component :is="components[data.template]" v-if="data" :data="data" />
|
||||||
import { useApiStore } from "./stores/api";
|
</template>
|
||||||
|
|
||||||
const templates = {
|
<script setup>
|
||||||
home: {
|
import home from "./views/Home.vue";
|
||||||
component: Home,
|
import { useApiStore } from "./stores/api";
|
||||||
query: {
|
import { ref } from "vue";
|
||||||
title: true,
|
|
||||||
},
|
const components = {
|
||||||
},
|
home,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const data = ref(null);
|
||||||
const api = useApiStore();
|
const api = useApiStore();
|
||||||
const pageTemplate = kirbyData.pageTemplate;
|
api.fetchPageData().then((res) => (data.value = res));
|
||||||
const pageUri = kirbyData.pageUri;
|
|
||||||
|
|
||||||
api.fetchData();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
|
||||||
<component :is="templates[pageTemplate].component" />
|
|
||||||
</template>
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,29 @@
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
|
|
||||||
export const useApiStore = defineStore("counter", () => {
|
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 api = "/api/query";
|
||||||
|
|
||||||
const username = import.meta.env.VITE_USERNAME;
|
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></template>
|
<h1>{{ data.content.title }}</h1>
|
||||||
<style scoped></style>
|
</template>
|
||||||
|
<script setup>
|
||||||
|
const { data } = defineProps({
|
||||||
|
data: Object,
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue