Skip to content

Commit

Permalink
Add Location to PageBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
tobifra committed Feb 18, 2024
1 parent 1eae51e commit 5936611
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 3 deletions.
11 changes: 11 additions & 0 deletions api/app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\FormItem;
use App\Models\GenericItem;
use App\Models\ImageItem;
use App\Models\LocationItem;
use App\Models\Page;
use App\Models\TextItem;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -219,6 +220,16 @@ private function createPageItemsFromValidatedData(Page $page, $validatedData)
]
);
break;
case 'locationItem':
LocationItem::updateOrCreate(
['id' => $pageItemData['id'] ?? null],
[
'page_id' => $page->id,
'sort' => $sort_counter,
'location_id' => $pageItemData['location_id'] ?? null,
]
);
break;
default:
throw new \InvalidArgumentException("Unsupported field type: {$pageItemData['type']}");
}
Expand Down
6 changes: 4 additions & 2 deletions api/app/Http/Requests/StorePageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public function rules()
'page_items' => 'nullable|array',
'page_items.*.id' => 'nullable',
'page_items.*.sort' => 'nullable',
'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem',
'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem,locationItem',
'page_items.*.title' => 'nullable',
'page_items.*.body' => 'nullable',
'page_items.*.show_fleur_de_lis' => 'nullable',
'page_items.*.files' => 'nullable',
'page_items.*.file_id' => 'nullable'
'page_items.*.file_id' => 'nullable',
'page_items.*.form_id' => 'nullable',
'page_items.*.location_id' => 'nullable',
];
}
}
3 changes: 2 additions & 1 deletion api/app/Http/Requests/UpdatePageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ public function rules()
'page_items' => 'nullable|array',
'page_items.*.id' => 'nullable',
'page_items.*.sort' => 'nullable',
'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem',
'page_items.*.type' => 'required|string|in:textItem,imageItem,formItem,filesItem,contactItem,groupsItem,sectionsItem,campsItem,locationItem',
'page_items.*.title' => 'nullable',
'page_items.*.body' => 'nullable',
'page_items.*.show_fleur_de_lis' => 'nullable',
'page_items.*.files' => 'nullable',
'page_items.*.form_id' => 'nullable',
'page_items.*.location_id' => 'nullable',
];
}
}
30 changes: 30 additions & 0 deletions api/app/Models/LocationItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LocationItem extends Model
{
use HasFactory;

protected $fillable = ['location_id', 'sort', 'page_id'];

protected $appends = ['type'];

public function getTypeAttribute()
{
return "locationItem";
}

public function page()
{
return $this->belongsTo(Page::class);
}

public function location()
{
return $this->belongsTo(Location::class);
}
}
8 changes: 8 additions & 0 deletions api/app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function filesItems()
return $this->hasMany(FilesItem::class)->with('files');
}

public function locationItems()
{

return $this->hasMany(LocationItem::class)->with('location');
}

public function genericItems()
{

Expand All @@ -69,12 +75,14 @@ public function getAllItems()
$formItems = $this->formItems;
$filesItems = $this->filesItems;
$genericItems = $this->genericItems;
$locationItems = $this->locationItems;

$items = $items->concat($textItems);
$items = $items->concat($imageItems);
$items = $items->concat($formItems);
$items = $items->concat($filesItems);
$items = $items->concat($genericItems);
$items = $items->concat($locationItems);

$items = $items->sortBy('sort')->values()->all();;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('location_items', function (Blueprint $table) {
$table->id();
$table->foreignId('location_id')->nullable()->constrained()->onDelete('cascade');
$table->integer('sort')->nullable();
$table->unsignedBigInteger('page_id');
$table->foreign('page_id')->references('id')->on('pages')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('location_items');
}
};
1 change: 1 addition & 0 deletions frontend/src/components/admin/AddPageItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default {
{ name: "Groups", type: "groupsItem" },
{ name: "Sections", type: "sectionsItem" },
{ name: "Camps", type: "campsItem" },
{ name: "Location", type: "locationItem" },
],
};
},
Expand Down
61 changes: 61 additions & 0 deletions frontend/src/components/admin/PageItems/LocationItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<template>
<DragItemBox
:boxTitle="boxTitle"
:item="item"
@delete="(event) => $emit('delete', event)"
@startedDragging="$emit('startedDragging')"
@endedDragging="$emit('endedDragging')"
>
<Card class="space-y-2">
<SelectComponent
selection="LocationItem"
@selectLocationItem="handleChange"
:value="item.locationId"
:options="options"
/>
</Card>
</DragItemBox>
</template>

<script>
import Card from "../Card.vue";
import SelectComponent from "../SelectComponent.vue";
import DragItemBox from "../DragItemBox.vue";
export default {
components: { Card, SelectComponent, DragItemBox },
props: ["item", "boxTitle"],
emits: [
"updatePage",
"changeLocation",
"delete",
"startedDragging",
"endedDragging",
],
data() {
return {
options: [],
};
},
methods: {
async getLocations() {
try {
const response = await this.callApi("get", "locations");
this.options = response.data.data;
} catch (error) {
console.log(error);
}
},
handleChange(value) {
this.$emit("changeLocation", { locationId: value, id: this.item.id });
},
},
computed: {},
async created() {
await this.getLocations();
},
};
</script>

<style></style>
18 changes: 18 additions & 0 deletions frontend/src/components/main/LocationItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<div>
<MapComponent class="w-full h-96" :markers="[item.location]" />
</div>
</template>
<script>
import MapComponent from "./MapComponent.vue";
export default {
props: ["item"],
data() {
return {};
},
methods: {},
async created() {},
components: { MapComponent },
};
</script>
3 changes: 3 additions & 0 deletions frontend/src/components/main/PageBuilder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
:files="pageItem.files"
/>
<CampsItem v-if="pageItem.type == 'campsItem'" :item="pageItem" />
<LocationItem v-if="pageItem.type == 'locationItem'" :item="pageItem" />
</template>
</PageWrapper>
</template>
Expand All @@ -26,6 +27,7 @@ import ImageItem from "./ImageItem.vue";
import PageWrapper from "./PageWrapper.vue";
import SectionsItem from "./SectionsItem.vue";
import TextItem from "./TextItem.vue";
import LocationItem from "./LocationItem.vue";
export default {
props: ["page"],
Expand All @@ -44,6 +46,7 @@ export default {
PageWrapper,
FilesItem,
CampsItem,
LocationItem,
},
};
</script>
21 changes: 21 additions & 0 deletions frontend/src/views/Dashboard/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
:key="i"
:item="pageItem"
/>
<LocationItem
v-if="pageItem.type == 'locationItem'"
boxTitle="Location Item"
@delete="deleteItem"
@startedDragging="isDragging = true"
@endedDragging="isDragging = false"
:key="i"
@changeLocation="changeLocationItem"
:item="pageItem"
/>
<AddPageItem
@changeOrder="changeOrder"
@select="addItem"
Expand Down Expand Up @@ -147,6 +157,7 @@ import GroupsItem from "../../components/admin/PageItems/GroupsItem.vue";
import SectionsItem from "../../components/admin/PageItems/SectionsItem.vue";
import FilesItem from "../../components/admin/PageItems/FilesItem.vue";
import CampsItem from "../../components/admin/PageItems/CampsItem.vue";
import LocationItem from "../../components/admin/PageItems/LocationItem.vue";
export default {
components: {
Expand All @@ -165,6 +176,7 @@ export default {
CheckBox,
FilesItem,
CampsItem,
LocationItem,
},
data() {
return {
Expand Down Expand Up @@ -259,6 +271,15 @@ export default {
this.content.pageItems[itemIndex].formId = formId;
},
changeLocationItem(event) {
const pageItemId = event.id;
const locationId = event.locationId;
const itemIndex = this.content.pageItems.findIndex(
(p) => p.id == pageItemId && p.type == "locationItem",
);
this.content.pageItems[itemIndex].locationId = locationId;
},
changeHeaderImages(event) {
this.content.files = event.files;
},
Expand Down

0 comments on commit 5936611

Please sign in to comment.