Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GraphQL Support + Bugfix #10

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.env.php
.env.sh
.env
.idea

# COMPOSER
/vendor
Expand Down
11 changes: 11 additions & 0 deletions src/Calendarize.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
use craft\elements\db\EntryQuery;
use craft\events\CancelableEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterGqlTypesEvent;
use craft\events\RegisterTemplateRootsEvent;
use craft\events\RegisterElementSortOptionsEvent;
use craft\services\Fields;
use craft\services\Gql;
use craft\web\twig\variables\CraftVariable;
use craft\web\View;
use unionco\calendarize\fields\CalendarizeField;
use unionco\calendarize\gql\types\CalendarizeType;
use unionco\calendarize\models\Settings;
use unionco\calendarize\services\CalendarizeService;
use unionco\calendarize\services\ICS;
Expand Down Expand Up @@ -110,6 +113,14 @@ function (RegisterComponentTypesEvent $event) {
}
);

Event::on(
Gql::class,
Gql::EVENT_REGISTER_GQL_TYPES,
function(RegisterGqlTypesEvent $event) {
$event->types[] = CalendarizeType::class;
}
);

Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/cpbundle/dist/css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/assetbundles/cpbundle/dist/js/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/assetbundles/cpbundle/src/js/modules/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CalendarException {

// create base inputs
const localizedMoment = getLocalizeMoment(this.dateField.value);
let newValue = localizedMoment.format('D/M/Y');
let newValue = localizedMoment.format('Y-M-D');

const name = this.hiddenName;
const position = name.indexOf('[date]');
Expand Down
4 changes: 2 additions & 2 deletions src/assetbundles/cpbundle/src/scss/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ body {}
position: absolute;

&:checked + .day-box {
background: #F1F5F8;
background: #dfe5ec;
}

&:checked + .day-box:before {
Expand Down Expand Up @@ -202,4 +202,4 @@ body {}
opacity: 0;
transform: scale(3, 3);
}
}
}
9 changes: 9 additions & 0 deletions src/fields/CalendarizeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use craft\base\PreviewableFieldInterface;
use craft\elements\db\ElementQueryInterface;
use unionco\calendarize\assetbundles\cpbundle\CpAssetBundle;
use unionco\calendarize\gql\types\CalendarizeType;

/**
* @author Franco Valdes
Expand Down Expand Up @@ -126,6 +127,14 @@ public function getElementValidationRules(): array
];
}

/**
* @inheritdoc
*/
public function getContentGqlType(): array|\GraphQL\Type\Definition\Type
{
return CalendarizeType::getType();
}

/**
* @inheritdoc
*/
Expand Down
57 changes: 57 additions & 0 deletions src/gql/types/CalendarizeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace unionco\calendarize\gql\types;

use craft\gql\GqlEntityRegistry;
use craft\helpers\Gql;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

class CalendarizeType {
public static function getName(): string
{
return 'calendarize_Calendarize';
}

public static function getType(): Type
{
if ($type = GqlEntityRegistry::getEntity(self::class)) {
return $type;
}

return GqlEntityRegistry::createEntity(self::class, new ObjectType([
'name' => static::getName(),
'fields' => self::class . '::getFieldDefinitions',
'description' => 'This is the interface implemented by all calendarize fields.',
]));
}

public static function getFieldDefinitions(): array
{
return [
'next' => [
'name' => 'next',
'type' => Type::listOf(Type::int()),
'description' => 'The next occurrence of this event',
'resolve' => function($source, $args, $context, $info) {
return [$source->next()->start->getTimestamp(), $source->next()->end->getTimestamp()];
}
],
'occurrences' => [
'name' => 'occurrences',
'type' => Type::listOf(Type::listOf(Type::int())),
'complexity' => Gql::singleQueryComplexity(),
'description' => 'All occurrences of this event',
'resolve' => function($source, $args, $context, $info) {
$timestamps = [];

$occurrences = $source->getOccurrences(500);
foreach($occurrences as $occurrence) {
$timesstamps[] = [$occurrence->start->getTimestamp(), $occurrence->end->getTimestamp()];
}
return $timesstamps;
},
],
];
}
}
2 changes: 1 addition & 1 deletion src/models/CalendarizeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function rrule()
'FREQ' => "DAILY",
'INTERVAL' => 1,
'DTSTART' => $this->startDate,
'UNTIL' => $this->startDate
'UNTIL' => $this->endDate
];
$this->repeatType = 'daily';
}
Expand Down