Merge branch 'event-list-fixes' into 'main'

fix(front): Fix behaviour when deleting an event from event list

See merge request framasoft/mobilizon!1380
This commit is contained in:
Thomas Citharel 2023-04-21 08:45:25 +00:00
commit 09d705a1d0
7 changed files with 135 additions and 40 deletions

View File

@ -22,8 +22,9 @@ const props = withDefaults(
defineProps<{ defineProps<{
events: IEvent[]; events: IEvent[];
isCurrentActorMember?: boolean; isCurrentActorMember?: boolean;
order: "ASC" | "DESC";
}>(), }>(),
{ isCurrentActorMember: false } { isCurrentActorMember: false, order: "ASC" }
); );
const monthlyGroupedEvents = computed((): Map<string, IEvent[]> => { const monthlyGroupedEvents = computed((): Map<string, IEvent[]> => {
@ -48,7 +49,9 @@ const keys = computed((): string[] => {
number number
]; ];
const bDate = new Date(...bParams); const bDate = new Date(...bParams);
return bDate.getTime() - aDate.getTime(); return props.order === "DESC"
? bDate.getTime() - aDate.getTime()
: aDate.getTime() - bDate.getTime();
}); });
}); });

View File

@ -424,6 +424,8 @@ export const FETCH_GROUP_EVENTS = gql`
$name: String! $name: String!
$afterDateTime: DateTime $afterDateTime: DateTime
$beforeDateTime: DateTime $beforeDateTime: DateTime
$order: EventOrderBy
$orderDirection: SortDirection
$organisedEventsPage: Int $organisedEventsPage: Int
$organisedEventsLimit: Int $organisedEventsLimit: Int
) { ) {
@ -431,6 +433,8 @@ export const FETCH_GROUP_EVENTS = gql`
organizedEvents( organizedEvents(
afterDatetime: $afterDateTime afterDatetime: $afterDateTime
beforeDatetime: $beforeDateTime beforeDatetime: $beforeDateTime
order: $order
orderDirection: $orderDirection
page: $organisedEventsPage page: $organisedEventsPage
limit: $organisedEventsLimit limit: $organisedEventsLimit
) { ) {

View File

@ -48,8 +48,10 @@
t("Past events") t("Past events")
}}</o-switch> }}</o-switch>
<grouped-multi-event-minimalist-card <grouped-multi-event-minimalist-card
class="mb-6"
:events="group.organizedEvents.elements" :events="group.organizedEvents.elements"
:isCurrentActorMember="isCurrentActorMember" :isCurrentActorMember="isCurrentActorMember"
:order="showPassedEvents ? 'DESC' : 'ASC'"
/> />
<empty-content <empty-content
v-if=" v-if="
@ -77,6 +79,7 @@
</template> </template>
</empty-content> </empty-content>
<o-pagination <o-pagination
v-if="group.organizedEvents.total > EVENTS_PAGE_LIMIT"
class="mt-4" class="mt-4"
:total="group.organizedEvents.total" :total="group.organizedEvents.total"
v-model:current="page" v-model:current="page"
@ -150,6 +153,8 @@ const variables = computed(() => ({
name: route.params.preferredUsername as string, name: route.params.preferredUsername as string,
beforeDateTime: showPassedEvents.value ? new Date() : null, beforeDateTime: showPassedEvents.value ? new Date() : null,
afterDateTime: showPassedEvents.value ? null : new Date(), afterDateTime: showPassedEvents.value ? null : new Date(),
order: "BEGINS_ON",
orderDirection: showPassedEvents.value ? "DESC" : "ASC",
organisedEventsPage: page.value, organisedEventsPage: page.value,
organisedEventsLimit: EVENTS_PAGE_LIMIT, organisedEventsLimit: EVENTS_PAGE_LIMIT,
})); }));

View File

@ -184,7 +184,7 @@
<section v-if="!showUpcoming && pastParticipations.elements.length > 0"> <section v-if="!showUpcoming && pastParticipations.elements.length > 0">
<transition-group name="list" tag="p"> <transition-group name="list" tag="p">
<div v-for="month in monthlyPastParticipations" :key="month[0]"> <div v-for="month in monthlyPastParticipations" :key="month[0]">
<span class="past-month">{{ month[0] }}</span> <h2 class="capitalize inline-block relative">{{ month[0] }}</h2>
<event-participation-card <event-participation-card
v-for="participation in month[1]" v-for="participation in month[1]"
:key="participation.id" :key="participation.id"
@ -226,7 +226,7 @@ import {
LOGGED_USER_PARTICIPATIONS, LOGGED_USER_PARTICIPATIONS,
LOGGED_USER_UPCOMING_EVENTS, LOGGED_USER_UPCOMING_EVENTS,
} from "@/graphql/participant"; } from "@/graphql/participant";
import { useQuery } from "@vue/apollo-composable"; import { useApolloClient, useQuery } from "@vue/apollo-composable";
import { computed, inject, ref, defineAsyncComponent } from "vue"; import { computed, inject, ref, defineAsyncComponent } from "vue";
import { IUser } from "@/types/current-user.model"; import { IUser } from "@/types/current-user.model";
import { import {
@ -414,16 +414,74 @@ const loadMorePastParticipations = (): void => {
} }
}; };
const apollo = useApolloClient();
const eventDeleted = (eventid: string): void => { const eventDeleted = (eventid: string): void => {
futureParticipations.value = futureParticipations.value.filter( /**
(participation) => participation.event.id !== eventid * Remove event from upcoming event participations
); */
pastParticipations.value = { const upcomingEventsData = apollo.client.cache.readQuery<{
elements: pastParticipations.value.elements.filter( loggedUser: IUser;
(participation) => participation.event.id !== eventid }>({
), query: LOGGED_USER_UPCOMING_EVENTS,
total: pastParticipations.value.total - 1, variables: () => ({
}; page: 1,
limit: 10,
afterDateTime: dateFilter.value,
}),
});
if (!upcomingEventsData) return;
const loggedUser = upcomingEventsData?.loggedUser;
const participations = loggedUser?.participations;
apollo.client.cache.writeQuery<{ loggedUser: IUser }>({
query: LOGGED_USER_UPCOMING_EVENTS,
variables: () => ({
page: 1,
limit: 10,
afterDateTime: dateFilter.value,
}),
data: {
loggedUser: {
...loggedUser,
participations: {
total: participations.total - 1,
elements: participations.elements.filter(
(participation) => participation.event.id !== eventid
),
},
},
},
});
/**
* Remove event from past event participations
*/
const participationData = apollo.client.cache.readQuery<{
loggedUser: Pick<IUser, "participations">;
}>({
query: LOGGED_USER_PARTICIPATIONS,
variables: () => ({ page: 1, limit: 10 }),
});
if (!participationData) return;
const loggedUser2 = participationData?.loggedUser;
const participations2 = loggedUser?.participations;
apollo.client.cache.writeQuery<{
loggedUser: Pick<IUser, "participations">;
}>({
query: LOGGED_USER_PARTICIPATIONS,
variables: () => ({ page: 1, limit: 10 }),
data: {
loggedUser: {
...loggedUser2,
participations: {
total: participations2.total - 1,
elements: participations2.elements.filter(
(participation) => participation.event.id !== eventid
),
},
},
},
});
}; };
const { restrictions } = useRestrictions(); const { restrictions } = useRestrictions();

View File

@ -431,13 +431,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
{:error, dgettext("errors", "You need to be logged-in to unfollow a group")} {:error, dgettext("errors", "You need to be logged-in to unfollow a group")}
end end
@spec find_events_for_group(Actor.t(), map(), Absinthe.Resolution.t()) :: @spec find_events_for_group(Actor.t(), map(), Absinthe.Resolution.t() | nil) ::
{:ok, Page.t(Event.t())} {:ok, Page.t(Event.t())}
def find_events_for_group( def find_events_for_group(
%Actor{id: group_id} = group, %Actor{id: group_id} = group,
%{ %{
page: page, page: page,
limit: limit limit: limit,
order: order,
order_direction: order_direction
} = args, } = args,
%{ %{
context: %{ context: %{
@ -453,6 +455,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
:all, :all,
Map.get(args, :after_datetime), Map.get(args, :after_datetime),
Map.get(args, :before_datetime), Map.get(args, :before_datetime),
order,
order_direction,
page, page,
limit limit
)} )}
@ -465,7 +469,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
%Actor{} = group, %Actor{} = group,
%{ %{
page: page, page: page,
limit: limit limit: limit,
order: order,
order_direction: order_direction
} = args, } = args,
_resolution _resolution
) do ) do
@ -475,6 +481,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
:public, :public,
Map.get(args, :after_datetime), Map.get(args, :after_datetime),
Map.get(args, :before_datetime), Map.get(args, :before_datetime),
order,
order_direction,
page, page,
limit limit
)} )}

View File

@ -91,6 +91,16 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
description: "Filter events that begin before this datetime" description: "Filter events that begin before this datetime"
) )
arg(:order, :event_order_by,
default_value: :begins_on,
description: "Order events by start date"
)
arg(:order_direction, :sort_direction,
default_value: :asc,
description: "Order events direction"
)
arg(:page, :integer, default_value: 1, description: "The page in the paginated event list") arg(:page, :integer, default_value: 1, description: "The page in the paginated event list")
arg(:limit, :integer, default_value: 10, description: "The limit of events per page") arg(:limit, :integer, default_value: 10, description: "The limit of events per page")
resolve(&Group.find_events_for_group/3) resolve(&Group.find_events_for_group/3)

View File

@ -415,7 +415,7 @@ defmodule Mobilizon.Events do
def list_public_events_for_actor(actor, page \\ nil, limit \\ nil) def list_public_events_for_actor(actor, page \\ nil, limit \\ nil)
def list_public_events_for_actor(%Actor{type: :Group} = group, page, limit), def list_public_events_for_actor(%Actor{type: :Group} = group, page, limit),
do: list_organized_events_for_group(group, :public, nil, nil, page, limit) do: list_organized_events_for_group(group, :public, nil, nil, nil, nil, page, limit)
def list_public_events_for_actor(%Actor{id: actor_id}, page, limit) do def list_public_events_for_actor(%Actor{id: actor_id}, page, limit) do
actor_id actor_id
@ -431,7 +431,17 @@ defmodule Mobilizon.Events do
def list_public_upcoming_events_for_actor(actor, page \\ nil, limit \\ nil) def list_public_upcoming_events_for_actor(actor, page \\ nil, limit \\ nil)
def list_public_upcoming_events_for_actor(%Actor{type: :Group} = group, page, limit), def list_public_upcoming_events_for_actor(%Actor{type: :Group} = group, page, limit),
do: list_organized_events_for_group(group, :public, DateTime.utc_now(), nil, page, limit) do:
list_organized_events_for_group(
group,
:public,
DateTime.utc_now(),
nil,
nil,
nil,
page,
limit
)
def list_public_upcoming_events_for_actor(%Actor{id: actor_id}, page, limit) do def list_public_upcoming_events_for_actor(%Actor{id: actor_id}, page, limit) do
actor_id actor_id
@ -461,14 +471,16 @@ defmodule Mobilizon.Events do
@spec list_simple_organized_events_for_group(Actor.t(), integer | nil, integer | nil) :: @spec list_simple_organized_events_for_group(Actor.t(), integer | nil, integer | nil) ::
Page.t(Event.t()) Page.t(Event.t())
def list_simple_organized_events_for_group(%Actor{} = actor, page \\ nil, limit \\ nil) do def list_simple_organized_events_for_group(%Actor{} = actor, page \\ nil, limit \\ nil) do
list_organized_events_for_group(actor, :all, nil, nil, page, limit) list_organized_events_for_group(actor, :all, nil, nil, nil, nil, page, limit)
end end
@spec list_organized_events_for_group( @spec list_organized_events_for_group(
Actor.t(), Actor.t(),
atom(), :public | :all,
DateTime.t() | nil, DateTime.t() | nil,
DateTime.t() | nil, DateTime.t() | nil,
atom() | nil,
atom() | nil,
integer | nil, integer | nil,
integer | nil integer | nil
) :: Page.t(Event.t()) ) :: Page.t(Event.t())
@ -477,6 +489,8 @@ defmodule Mobilizon.Events do
visibility \\ :public, visibility \\ :public,
after_datetime \\ nil, after_datetime \\ nil,
before_datetime \\ nil, before_datetime \\ nil,
order_by \\ nil,
order_direction \\ nil,
page \\ nil, page \\ nil,
limit \\ nil limit \\ nil
) do ) do
@ -484,6 +498,7 @@ defmodule Mobilizon.Events do
|> event_for_group_query() |> event_for_group_query()
|> event_filter_visibility(visibility) |> event_filter_visibility(visibility)
|> event_filter_begins_on(after_datetime, before_datetime) |> event_filter_begins_on(after_datetime, before_datetime)
|> event_order_by(order_by, order_direction)
|> preload_for_event() |> preload_for_event()
|> Page.build_page(page, limit) |> Page.build_page(page, limit)
end end
@ -1245,11 +1260,7 @@ defmodule Mobilizon.Events do
@spec event_for_group_query(integer | String.t()) :: Ecto.Query.t() @spec event_for_group_query(integer | String.t()) :: Ecto.Query.t()
defp event_for_group_query(group_id) do defp event_for_group_query(group_id) do
from( where(Event, [e], e.attributed_to_id == ^group_id)
e in Event,
where: e.attributed_to_id == ^group_id,
order_by: [asc: :begins_on]
)
end end
@spec upcoming_public_event_for_actor_query(integer | String.t()) :: Ecto.Query.t() @spec upcoming_public_event_for_actor_query(integer | String.t()) :: Ecto.Query.t()
@ -1909,19 +1920,14 @@ defmodule Mobilizon.Events do
where(query, visibility: ^:public, draft: false) where(query, visibility: ^:public, draft: false)
end end
defp event_filter_begins_on(query, nil, nil), defp event_filter_begins_on(query, nil, nil), do: query
do: event_order_begins_on_desc(query)
defp event_filter_begins_on(query, %DateTime{} = after_datetime, nil) do defp event_filter_begins_on(query, %DateTime{} = after_datetime, nil) do
query where(query, [e], e.begins_on > ^after_datetime)
|> where([e], e.begins_on > ^after_datetime)
|> event_order_begins_on_asc()
end end
defp event_filter_begins_on(query, nil, %DateTime{} = before_datetime) do defp event_filter_begins_on(query, nil, %DateTime{} = before_datetime) do
query where(query, [e], e.begins_on < ^before_datetime)
|> where([e], e.begins_on < ^before_datetime)
|> event_order_begins_on_desc()
end end
defp event_filter_begins_on( defp event_filter_begins_on(
@ -1932,9 +1938,16 @@ defmodule Mobilizon.Events do
query query
|> where([e], e.begins_on < ^before_datetime) |> where([e], e.begins_on < ^before_datetime)
|> where([e], e.begins_on > ^after_datetime) |> where([e], e.begins_on > ^after_datetime)
|> event_order_begins_on_asc()
end end
defp event_order_by(query, order_by, direction)
when order_by in [:begins_on, :inserted_at, :updated_at] and direction in [:asc, :desc] do
order_by_instruction = Keyword.new([{direction, order_by}])
order_by(query, [e], ^order_by_instruction)
end
defp event_order_by(query, _order_by, _direction), do: event_order_by(query, :begins_on, :asc)
defp event_order(query, :match_desc), defp event_order(query, :match_desc),
do: order_by(query, [e, f], desc: f.rank, asc: e.begins_on) do: order_by(query, [e, f], desc: f.rank, asc: e.begins_on)
@ -1947,12 +1960,6 @@ defmodule Mobilizon.Events do
defp event_order(query, _), do: query defp event_order(query, _), do: query
defp event_order_begins_on_asc(query),
do: order_by(query, [e], asc: e.begins_on)
defp event_order_begins_on_desc(query),
do: order_by(query, [e], desc: e.begins_on)
defp participation_filter_begins_on(query, nil, nil), defp participation_filter_begins_on(query, nil, nil),
do: participation_order_begins_on_desc(query) do: participation_order_begins_on_desc(query)