From d7adfaa0382e395bf2da6d87bd7cd724bb43f71d Mon Sep 17 00:00:00 2001
From: Romain Lebbadi-Breteau <romain@lebbadi.fr>
Date: Sat, 12 Feb 2022 15:28:39 -0500
Subject: [PATCH] Make calendar great again

---
 assets/calendar.js                    | 66 ++++++++++++++-------------
 migrations/Version20220212192953.php  | 32 +++++++++++++
 src/Controller/CalendarController.php |  6 ++-
 templates/base.html.twig              |  2 +
 templates/calendar/index.html.twig    | 31 -------------
 5 files changed, 73 insertions(+), 64 deletions(-)
 create mode 100644 migrations/Version20220212192953.php

diff --git a/assets/calendar.js b/assets/calendar.js
index 9a1743b4..8629e61a 100644
--- a/assets/calendar.js
+++ b/assets/calendar.js
@@ -14,10 +14,8 @@ import 'tui-calendar';
 
 var Calendar = require('tui-calendar'); /* CommonJS */
 
-//console.log(calendar)
 
-
-$(document).ready(function () {
+$(function () {
   var calendar = new Calendar('#calendar', {
     defaultView: 'week',
     taskView: false,
@@ -29,18 +27,22 @@ $(document).ready(function () {
   var currentSchedule = 1
 
   function sendCalendarData() {
-    calendar.getSchedule()
+    console.log(scheduleIds)
+    var schedules = []
     scheduleIds.forEach(function(id){
-      var schedule = calendar.getSchedule(id, '1');
-      console.log(schedule)
+      var schedule = calendar.getSchedule(String(id), '1');
+      schedules[schedules.length] = schedule
     })
 
+    console.log(schedules)
+
     $.ajax({
       type: "POST",
       url: "/calendar/send",
-      data: calendar._controller.schedules.items,
+      data: JSON.stringify(schedules),
       success: function (data) { console.log(data) },
-      dataType: "application/json"
+      dataType: "json",
+      contentType: 'application/json'
     });
   }
 
@@ -68,9 +70,9 @@ $(document).ready(function () {
     scheduleIds[scheduleIds.length] = currentSchedule;
     currentSchedule++;
 
-    sendCalendarData();
-
     calendar.createSchedules([schedule]);
+
+    sendCalendarData();
   });
 
   prevBtn.addEventListener("click", e => {
@@ -80,25 +82,27 @@ $(document).ready(function () {
     calendar.next();
     sendCalendarData();
   });
-  calendar.createSchedules([
-    {
-      id: '1',
-      calendarId: '1',
-      title: 'my schedule',
-      category: 'time',
-      dueDateClass: '',
-      start: '2022-02-10T22:30:00+09:00',
-      end: '2022-02-11T02:30:00+09:00'
-    },
-    {
-      id: '2',
-      calendarId: '1',
-      title: 'second schedule',
-      category: 'time',
-      dueDateClass: '',
-      start: '2022-02-11T17:30:00+09:00',
-      end: '2022-02-12T17:31:00+09:00',
-      isReadOnly: true    // schedule is read-only
-    }
-  ]);
+
+
+  $.ajax({
+    type: "POST",
+    url: "/calendar/receive",
+    success: function (data) {
+      var cleanedData = []
+      data.forEach(function(schedule){
+        if (schedule) {
+          schedule.start = schedule.start._date
+          schedule.end = schedule.end._date
+          scheduleIds[scheduleIds.length] = parseInt(schedule.id)
+          currentSchedule++
+          cleanedData[cleanedData.length] = schedule
+        }
+      })
+
+      // console.log(currentSchedule)
+
+      calendar.createSchedules(cleanedData)
+
+     },
+  });
 });
diff --git a/migrations/Version20220212192953.php b/migrations/Version20220212192953.php
new file mode 100644
index 00000000..6d704a56
--- /dev/null
+++ b/migrations/Version20220212192953.php
@@ -0,0 +1,32 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DoctrineMigrations;
+
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\Migrations\AbstractMigration;
+
+/**
+ * Auto-generated Migration: Please modify to your needs!
+ */
+final class Version20220212192953 extends AbstractMigration
+{
+    public function getDescription(): string
+    {
+        return '';
+    }
+
+    public function up(Schema $schema): void
+    {
+        // this up() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE "user" ADD calendar_file JSON DEFAULT NULL');
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('CREATE SCHEMA public');
+        $this->addSql('ALTER TABLE "user" DROP calendar_file');
+    }
+}
diff --git a/src/Controller/CalendarController.php b/src/Controller/CalendarController.php
index 2808935e..0023f099 100644
--- a/src/Controller/CalendarController.php
+++ b/src/Controller/CalendarController.php
@@ -36,11 +36,11 @@ class CalendarController extends AbstractController
 
         return new JsonResponse($calendar);
     }
-    
+
     #[Route('/calendar/send', name: 'calendar_send')]
     #[IsGranted('ROLE_USER')]
     public function calendarSend(Request $request, EntityManagerInterface $manager)
-    {  
+    {
         $json = $request->getContent();
         $calendar = json_decode($json, true);
 
@@ -53,5 +53,7 @@ class CalendarController extends AbstractController
 
         $user->setCalendarFile($calendar);
         $manager->flush();
+
+        return new Response("all good");
     }
 }
diff --git a/templates/base.html.twig b/templates/base.html.twig
index ee61e9a6..bea151f1 100644
--- a/templates/base.html.twig
+++ b/templates/base.html.twig
@@ -3,6 +3,8 @@
     <head>
         <meta charset="UTF-8">
         <title>{% block title %}Welcome!{% endblock %}</title>
+          <link rel="stylesheet" type="text/css" href="https://uicdn.toast.com/tui.time-picker/latest/tui-time-picker.css">
+    <link rel="stylesheet" type="text/css" href="https://uicdn.toast.com/tui.date-picker/latest/tui-date-picker.css">
         <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text></svg>">
         {# Run `composer require symfony/webpack-encore-bundle` to start using Symfony UX #}
         {% block stylesheets %}
diff --git a/templates/calendar/index.html.twig b/templates/calendar/index.html.twig
index 890d2eb7..2d608a23 100644
--- a/templates/calendar/index.html.twig
+++ b/templates/calendar/index.html.twig
@@ -8,37 +8,6 @@
 	{{ encore_entry_script_tags('calendar') }}
 {% endblock %}
 {% block body %}
-	<style>
-		.example-wrapper {
-			margin: 1em auto;
-			max-width: 800px;
-			width: 95%;
-			font: 18px / 1.5 sans-serif;
-		}
-		.example-wrapper code {
-			background: #F5F5F5;
-			padding: 2px 6px;
-		}
-	</style>
-
-	<div class="example-wrapper">
-		<h1>Hello
-			{{ controller_name }}! ✅</h1>
-
-		This friendly message is coming from:
-		<ul>
-			<li>Your controller at
-				<code>
-					<a href="{{ '/home/fanevark/step/calendrier/src/Controller/CalendarController.php'|file_link(0) }}">src/Controller/CalendarController.php</a>
-				</code>
-			</li>
-			<li>Your template at
-				<code>
-					<a href="{{ '/home/fanevark/step/calendrier/templates/calendar/index.html.twig'|file_link(0) }}">templates/calendar/index.html.twig</a>
-				</code>
-			</li>
-		</ul>
-	</div>
 
 	<body>
 		<div class="card">
-- 
GitLab