Skip to content

Commit

Permalink
feat: added Foreign component, fixed EnumFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed Dec 10, 2024
1 parent 3debcd7 commit e18184d
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/css/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"/js/app.js": "/js/app.js?id=82c6422f61e56e2fcf4f223d37ae20ba",
"/js/app.js": "/js/app.js?id=2ac5d4ba555b3dee9bab9b39626c201f",
"/js/manifest.js": "/js/manifest.js?id=35aa9d461e033ff05abe890341153838",
"/js/vendor.js": "/js/vendor.js?id=b53083a0bb34e922d0d09ba0e52241c6",
"/css/app.css": "/css/app.css?id=d75c395a1c5c29d8b4c39d09727b550c"
"/css/app.css": "/css/app.css?id=e23eef270fc9a2679846e04a9a6e0a85"
}
2 changes: 2 additions & 0 deletions resources/js/components/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import HasManyField from "./has-many-field";
import Modal from "./modal";
import ManualOrderControls from "./manual-order-controls";
import TreeOrderControls from "./tree-order-controls";
import Foreign from "./foreign";

export default {
'text-literal': TextLiteral,
Expand Down Expand Up @@ -104,4 +105,5 @@ export default {
'modal': Modal,
'manual-order-controls': ManualOrderControls,
'tree-order-controls': TreeOrderControls,
'foreign': Foreign,
};
51 changes: 51 additions & 0 deletions resources/js/components/foreign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';
import components from "../rendering/components";

class Foreign extends React.Component {

static defaultProps = {
components: [],
path: {},
data: {},
};

constructor(props) {
super(props);

this.componentList = [];
}

handleSubmit(data) {
this.componentList.forEach(obj => {
obj.ref.current.handleSubmit(data);
});
}

getData(data) {
this.componentList.forEach(obj => {
obj.ref.current.getData(data);
});
return data;
}

render() {

this.componentList = components.renderComponentsWith(this.props.components, this.props.data, this.props.path, (component, i) => {
return (
<div className="foreign__component" key={i}>
{component}
</div>
);
}, true, this.props.errors);

let componentListRenders = this.componentList.map(obj => obj.component);

return (
<div className="foreign">
{componentListRenders}
</div>
);
}
}

export default Foreign;
3 changes: 2 additions & 1 deletion resources/js/filters/enum-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ function EnumFilter(props) {
useEffect(() => {

const values = props.data['filter_'+props.id] ? props.data['filter_'+props.id].split(',') : [];
const readableValues = values.map(value => props.options[value]).filter(value => value);

setState({
...state,
values,
humanReadableValue: (values.length ? values.join(', ') : 'All')
humanReadableValue: (readableValues.length ? readableValues.join(', ') : 'All')
});
}, [props.data]);

Expand Down
1 change: 1 addition & 0 deletions resources/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@
@import 'cmf/components/json-field';
@import 'cmf/components/manual-order-controls';
@import 'cmf/components/inline-frame';
@import 'cmf/components/foreign';
5 changes: 5 additions & 0 deletions resources/sass/cmf/components/_foreign.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@include block(foreign)
{
display: grid;
grid-gap: $rule;
}
65 changes: 65 additions & 0 deletions src/Components/Foreign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace ReinVanOyen\Cmf\Components;

use Illuminate\Database\Eloquent\Model;
use ReinVanOyen\Cmf\Http\Resources\ModelResource;
use ReinVanOyen\Cmf\RelationshipMetaGuesser;

class Foreign extends Compound
{
/**
* @var string $relationship
*/
private $relationship;

/**
* @param string $relationship
* @param array $components
*/
public function __construct(string $relationship, array $components)
{
$this->relationship = $relationship;

parent::__construct($components);
}

/**
* @return string
*/
public function type(): string
{
return 'foreign';
}

/**
* @param ModelResource $model
* @param array $attributes
* @return void
* @throws \ReinVanOyen\Cmf\Exceptions\CouldntGuessMetaException
*/
public function provision(ModelResource $model, array &$attributes)
{
$foreignModel = $model->{$this->relationship};
$modelResource = new ModelResource($foreignModel);

foreach ($this->components as $component) {
$component->provision($modelResource, $attributes);
}
}

/**
* @param Model $model
* @param \Illuminate\Http\Request $request
*/
public function save(Model $model, $request)
{
$foreignModel = $model->{$this->relationship};

foreach ($this->components as $component) {
$component->save($foreignModel, $request);
}

$foreignModel->save();
}
}

0 comments on commit e18184d

Please sign in to comment.