diff --git a/assets/controllers/organization_controller.js b/assets/controllers/organization_controller.js index 446de27..126d9d3 100644 --- a/assets/controllers/organization_controller.js +++ b/assets/controllers/organization_controller.js @@ -4,10 +4,24 @@ import {TabulatorFull as Tabulator} from 'tabulator-tables'; import {eyeIconLink, TABULATOR_FR_LANG} from "../js/global.js"; export default class extends Controller { - static values = {aws: String}; + static values = {aws: String, + id: String, + activities: Boolean, + table: Boolean, + }; + static targets = ["activityList", "emptyMessage"] connect() { - this.table(); + if(this.activitiesValue){ + this.loadActivities(); + setInterval(() => { + this.loadActivities(); + }, 5000); // Refresh every 60 seconds + } + if (this.tableValue){ + this.table(); + } + } table(){ @@ -82,4 +96,59 @@ export default class extends Controller { }], }); } + + async loadActivities() { + try { + // 1. Fetch the data using the ID from values + const response = await fetch(`/actions/organization/${this.idValue}/activities-ajax`); + + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + const activities = await response.json(); + + // 2. Render + this.renderActivities(activities); + + } catch (error) { + console.error('Error fetching activities:', error); + this.activityListTarget.innerHTML = `
Erreur lors du chargement.
`; + } + } + + renderActivities(activities) { + // Clear the loading spinner + this.activityListTarget.innerHTML = ''; + + if (activities.length === 0) { + // Show empty message + this.activityListTarget.innerHTML = this.emptyMessageTarget.innerHTML; + return; + } + + // Loop through JSON and build HTML + const html = activities.map(activity => { + return ` +
+ +
+
+ ${activity.date} +
+
+ +
+
+ ${activity.userName} +
${activity.actionType}
+
+
+
+ `; + }).join(''); + + this.activityListTarget.innerHTML = html; + } } \ No newline at end of file diff --git a/src/Controller/ActionController.php b/src/Controller/ActionController.php new file mode 100644 index 0000000..b01b613 --- /dev/null +++ b/src/Controller/ActionController.php @@ -0,0 +1,34 @@ +entityManager->getRepository(Actions::class)->findBy( + ['Organization' => $organization], + ['date' => 'DESC'], + 15 + ); + $formattedActivities = $this->actionService->formatActivities($actions); + + return new JsonResponse($formattedActivities); + } +} diff --git a/src/Service/ActionService.php b/src/Service/ActionService.php index f0a7a44..7b750f9 100644 --- a/src/Service/ActionService.php +++ b/src/Service/ActionService.php @@ -40,11 +40,11 @@ readonly class ActionService { return array_map(function (Actions $activity) { return [ - 'date' => $activity->getDate(), + 'date' => $activity->getDate()->format('d/m/Y H:i'), 'actionType' => $activity->getActionType(), - 'users' => $activity->getUsers(), - 'organization' => $activity->getOrganization(), - 'description' => $activity->getDescription(), + 'userName' => $activity->getUsers()->getName(), +// 'organization' => $activity->getOrganization(), +// 'description' => $activity->getDescription(), 'color' => $this->getActivityColor($activity->getDate()) ]; }, $activities); diff --git a/templates/organization/activity.html.twig b/templates/organization/activity.html.twig deleted file mode 100644 index 3e8723f..0000000 --- a/templates/organization/activity.html.twig +++ /dev/null @@ -1,25 +0,0 @@ -{% block body %} - - -
-
-

{{ title }}

-
-
- {% if activities|length == 0 %} -

Aucune activité récente.

- {% else %} - {% set sortedActivities = activities|sort((a, b) => a.date <=> b.date)|reverse %} - - {% endif %} -
-{% endblock %} \ No newline at end of file diff --git a/templates/organization/index.html.twig b/templates/organization/index.html.twig index e47f594..3e09ad9 100644 --- a/templates/organization/index.html.twig +++ b/templates/organization/index.html.twig @@ -35,6 +35,7 @@ {% else %}
diff --git a/templates/organization/show.html.twig b/templates/organization/show.html.twig index 9704040..fe48db6 100644 --- a/templates/organization/show.html.twig +++ b/templates/organization/show.html.twig @@ -39,9 +39,9 @@
-{# single row so that activity and users tabs are next to each other#} + {# single row so that activity and users tabs are next to each other #}
-{# User tables #} + {# User tables #}
@@ -49,7 +49,8 @@

Nouveaux utilisateurs

- Ajouter un utilisateur + Ajouter un utilisateur
{# APPLICATION ROW #} -{# TODO: Weird gap not going away#} + {# TODO: Weird gap not going away #}
{% for application in applications %}
@@ -104,17 +105,37 @@ {% endfor %}
-{# Activities col#} + {# Activities col #}
- {% include 'organization/activity.html.twig' with { - title: 'Activités récentes', - empty_message: 'Aucune activité récente.' - } %} +
+ +
+

Activité récente

+ + +
+ +
+
+
+ +
+
+ + {# Empty state #} +
+
Aucune activité récente.
+
+
+
-{# Ne pas enlever le 2ème /div#} -
diff --git a/templates/user/organization/userActivity.html.twig b/templates/user/organization/userActivity.html.twig deleted file mode 100644 index aa12274..0000000 --- a/templates/user/organization/userActivity.html.twig +++ /dev/null @@ -1,17 +0,0 @@ -{% block body %} - -
-
-
- -

- - {{ activityTime|ago }}

-
-
-
-

{{ userName }} - {{ action }}

-
-
- -{% endblock %} \ No newline at end of file