redesign user data api
This commit is contained in:
parent
f132049948
commit
44361e614d
17 changed files with 80 additions and 52 deletions
|
|
@ -78,9 +78,12 @@ $specificData = [
|
||||||
"exampleHardData" => 'Example hard value'
|
"exampleHardData" => 'Example hard value'
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
```
|
```
|
||||||
|
|
||||||
`$genericData` are defined in the `/public/site/controllers/site.php` controllers. By default, it contains a simple representation of the page object.
|
`$genericData` are defined in the `/public/site/controllers/site.php` controllers. By default, it contains a simple representation of the page object.
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,24 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return function ($page) {
|
return function ($page, $kirby) {
|
||||||
$data = $page->toArray();
|
$data = $page->toArray();
|
||||||
$data['template'] = (string) $page->template();
|
$data['template'] = (string) $page->template();
|
||||||
|
|
||||||
|
$userData = [
|
||||||
|
"role" => $kirby->user()->role(),
|
||||||
|
"uuid" => $kirby->user()->uuid(),
|
||||||
|
"notifications" => Yaml::decode($kirby->user()->notifications()->value()),
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($kirby->user()->role() == 'client') {
|
||||||
|
$userData['client'] = [
|
||||||
|
"name" => $kirby->user()->client()->toPage()->title(),
|
||||||
|
"uuid" => $kirby->user()->client()->toPage()->uuid()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'genericData' => $data,
|
'genericData' => $data,
|
||||||
|
'userData' => $userData
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
@ -1,19 +1,2 @@
|
||||||
<script>
|
|
||||||
<?php if ($kirby->user()): ?>
|
|
||||||
const kirbyData = {
|
|
||||||
user: {
|
|
||||||
role: '<?= $kirby->user()->role() ?>',
|
|
||||||
uuid: '<?= $kirby->user()->uuid() ?>',
|
|
||||||
notifications: <?= json_encode(Yaml::decode($kirby->user()->notifications()->value())) ?>
|
|
||||||
<?php if ($kirby->user()->role() == 'client'): ?>
|
|
||||||
client: {
|
|
||||||
name: '<?= $kirby->user()->client()->toPage()->title() ?>',
|
|
||||||
uuid: '<?= $kirby->user()->client()->toPage()->uuid() ?>',
|
|
||||||
}
|
|
||||||
<?php endif ?>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<?php endif ?>
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
@ -11,6 +11,9 @@ $specificData = [
|
||||||
"moodboard" => $moodboard
|
"moodboard" => $moodboard
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -5,6 +5,9 @@ $specificData = [
|
||||||
"exampleHardData" => 'Example hard value'
|
"exampleHardData" => 'Example hard value'
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -5,6 +5,9 @@ $specificData = [
|
||||||
'blocks' => $blocks
|
'blocks' => $blocks
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -16,6 +16,9 @@ $specificData = [
|
||||||
"images" => $images
|
"images" => $images
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -26,6 +26,9 @@ $specificData = [
|
||||||
"inspirations" => $inspirations
|
"inspirations" => $inspirations
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -4,6 +4,9 @@ $specificData = [
|
||||||
'title' => $page->title()->value(),
|
'title' => $page->title()->value(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -8,6 +8,9 @@ $project = [
|
||||||
'steps' => $page->getSteps(),
|
'steps' => $page->getSteps(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $project);
|
$pageData = array_merge($genericData, $project);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -15,6 +15,9 @@ $specificData = [
|
||||||
"children" => $children,
|
"children" => $children,
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@ $specificData = [
|
||||||
'title' => $page->title()->value(),
|
'title' => $page->title()->value(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$data = array_merge($genericData, $specificData);
|
$pageData = array_merge($genericData, $specificData);
|
||||||
|
|
||||||
echo json_encode($data);
|
echo json_encode([
|
||||||
|
"page" => $pageData,
|
||||||
|
"user" => $userData
|
||||||
|
]);
|
||||||
|
|
@ -13,7 +13,4 @@ import Menu from "./components/Menu.vue";
|
||||||
import { usePageStore } from "./stores/page";
|
import { usePageStore } from "./stores/page";
|
||||||
|
|
||||||
const { page } = storeToRefs(usePageStore());
|
const { page } = storeToRefs(usePageStore());
|
||||||
|
|
||||||
const { user } = storeToRefs(useUserStore());
|
|
||||||
user.value = kirbyData.user;
|
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { createWebHistory, createRouter } from "vue-router";
|
||||||
import routes from "./routes";
|
import routes from "./routes";
|
||||||
import { useApiStore } from "../stores/api";
|
import { useApiStore } from "../stores/api";
|
||||||
import { usePageStore } from "../stores/page";
|
import { usePageStore } from "../stores/page";
|
||||||
|
import { useUserStore } from "../stores/user";
|
||||||
import { getActivePinia } from "pinia";
|
import { getActivePinia } from "pinia";
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
|
|
@ -12,12 +13,14 @@ const router = createRouter({
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
const pinia = getActivePinia();
|
const pinia = getActivePinia();
|
||||||
const api = useApiStore(pinia);
|
const api = useApiStore(pinia);
|
||||||
const page = usePageStore(pinia);
|
const pageStore = usePageStore(pinia);
|
||||||
|
const userStore = useUserStore(pinia);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await api.fetchPageData(to.path);
|
const res = await api.fetchData(to.path);
|
||||||
|
|
||||||
page.page = res;
|
pageStore.page = res.page;
|
||||||
|
userStore.user = res.user;
|
||||||
next();
|
next();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,10 @@ export const useAddImagesModalStore = defineStore("add-images-modal", () => {
|
||||||
|
|
||||||
const api = useApiStore();
|
const api = useApiStore();
|
||||||
api
|
api
|
||||||
.fetchPageData("materials")
|
.fetchData("materials")
|
||||||
.then((json) => (images.value = tabs.value[1].images = json.images));
|
.then((json) => (images.value = tabs.value[1].images = json.images));
|
||||||
|
|
||||||
api.fetchPageData("creations").then((json) => {
|
api.fetchData("creations").then((json) => {
|
||||||
images.value = tabs.value[2].images = json.images;
|
images.value = tabs.value[2].images = json.images;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ export const useApiStore = defineStore("api", () => {
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* // Fetch data for the current page
|
* // Fetch data for the current page
|
||||||
* fetchPageData().then(data => {
|
* fetchData().then(data => {
|
||||||
* console.log(data);
|
* console.log(data);
|
||||||
* }).catch(error => {
|
* }).catch(error => {
|
||||||
* console.error('Error fetching data:', error);
|
* console.error('Error fetching data:', error);
|
||||||
|
|
@ -24,13 +24,13 @@ export const useApiStore = defineStore("api", () => {
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* // Fetch data for a specific path
|
* // Fetch data for a specific path
|
||||||
* fetchPageData('/about').then(data => {
|
* fetchData('/about').then(data => {
|
||||||
* console.log(data);
|
* console.log(data);
|
||||||
* }).catch(error => {
|
* }).catch(error => {
|
||||||
* console.error('Error fetching data:', error);
|
* console.error('Error fetching data:', error);
|
||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
async function fetchPageData(path = window.location.pathname) {
|
async function fetchData(path = window.location.pathname) {
|
||||||
const isHomePage = path === "/";
|
const isHomePage = path === "/";
|
||||||
path = path === "/" ? "/home" : path;
|
path = path === "/" ? "/home" : path;
|
||||||
path = path.startsWith("/") ? path : "/" + path;
|
path = path.startsWith("/") ? path : "/" + path;
|
||||||
|
|
@ -120,5 +120,5 @@ export const useApiStore = defineStore("api", () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { fetchDataThroughKQL, fetchPageData, fetchRoute };
|
return { fetchDataThroughKQL, fetchData, fetchRoute };
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ export const useProjectsStore = defineStore("projects", () => {
|
||||||
|
|
||||||
const api = useApiStore();
|
const api = useApiStore();
|
||||||
api
|
api
|
||||||
.fetchPageData("projects")
|
.fetchData("projects")
|
||||||
.then((json) => (projects.value = json.children));
|
.then((json) => (projects.value = json.page.children));
|
||||||
|
|
||||||
return { projects, currentProjects, archivedProjects };
|
return { projects, currentProjects, archivedProjects };
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue