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

[ORG-121] Cancel participation in my events page #108

Merged
merged 5 commits into from
Jan 12, 2024
Merged
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
58 changes: 56 additions & 2 deletions organizator/components/CardMyEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React from "react";
// @ts-ignore
import styled from "styled-components/native";
import { FontAwesome5 } from "@expo/vector-icons";
import { Text } from "react-native";
import { Pressable, Text } from "react-native";
import FontAwesome from "@expo/vector-icons/FontAwesome";
import {
getColorForApplicationStatus,
parseDate,
Expand All @@ -13,6 +14,9 @@ interface CardProps {
startDate: string;
headerImage: string;
status: string;
id: string;
setShowCancelAlert: (showCancelAlert: boolean) => void;
setIdToCancel: (idToCancel: string) => void;
}

const CardContainer = styled.View<{ isPast: boolean }>`
Expand Down Expand Up @@ -88,8 +92,43 @@ const TagStatus = styled.View<{ backgroundColor: string }>`
text-align: center;
`;

const ButtonsContainer = styled.View`
display: flex;
width: 100%;
justify-content: center;
align-items: center;
`;

const CancelButton = styled(Pressable)`
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
padding: 10px 30px;
margin-top: 20px;
border-radius: 20px;
gap: 10px;
background-color: #a65858;
width: 50%;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
`;

const CancelButtonText = styled.Text`
font-size: 16px;
color: white;
font-weight: bold;
`;

export default function CardMyEvents(props: CardProps) {
const { title, startDate, headerImage, status } = props;
const {
title,
startDate,
headerImage,
status,
id,
setIdToCancel,
setShowCancelAlert,
} = props;

const isPast = () => startDate < new Date().toISOString();

Expand All @@ -108,6 +147,21 @@ export default function CardMyEvents(props: CardProps) {
<Text>{status}</Text>
</TagStatus>
</TagContainer>
<ButtonsContainer>
{status !== "Cancelled" &&
status !== "Rejected" &&
status !== "Invalid" && (
<CancelButton
onPress={() => {
setIdToCancel(id);
setShowCancelAlert(true);
}}
>
<FontAwesome name="close" size={16} color="white" />
<CancelButtonText>Cancel</CancelButtonText>
</CancelButton>
)}
</ButtonsContainer>
</CardTextContainer>
</CardContainer>
{isPast() && (
Expand Down
114 changes: 97 additions & 17 deletions organizator/components/MyEventsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { View } from "react-native";
import { ScrollView, View } from "react-native";
// @ts-ignore
import styled from "styled-components/native";
import React, { useEffect, useState } from "react";
import { getMyApplications } from "../utils/api/axiosApplications";
import Toast from "react-native-toast-message";
import { ConfirmDialog } from "react-native-simple-dialogs";
import {
cancelApplication,
getMyApplications,
} from "../utils/api/axiosApplications";
import EmptyPage from "./EmptyPage";
import { getToken } from "../utils/sessionCalls";
import LoadingPage from "./LodingPage";
Expand All @@ -20,7 +25,12 @@ const CardsContainer = styled(View)`

export default function MyEventsPage() {
const [loading, setLoading] = useState(true);
const [events, setEvents] = useState<ApplicationInformationWithoutUser[]>([]);
const [applications, setApplications] = useState<
ApplicationInformationWithoutUser[]
>([]);
const [trigger, setTrigger] = useState(false);
const [showCancelAlert, setShowCancelAlert] = useState(false);
const [idToCancel, setIdToCancel] = useState("");

useEffect(() => {
const fetchData = async () => {
Expand All @@ -30,37 +40,107 @@ export default function MyEventsPage() {

fetchData().then((response) => {
setLoading(false);
setEvents(response.applications || []);
setApplications(response.applications || []);
});
}, []);
}, [trigger]);

function cancel() {
const fetchData = async () => {
const t = await getToken();
return cancelApplication(t || "", idToCancel);
};

fetchData().then((response) => {
if (response.error) {
Toast.show({
type: "error",
text1: "Error",
text2: response.error,
visibilityTime: 8000,
});
setShowCancelAlert(false);
} else {
setTrigger(!trigger);
Toast.show({
type: "success",
text1: "Success",
text2: "Your application has been cancelled!",
visibilityTime: 8000,
});
setShowCancelAlert(false);
}
});
}

return (
<View>
{loading ? (
<LoadingPage />
) : (
<View>
{events.length === 0 ? (
<ScrollView>
{applications.length === 0 ? (
<EmptyPage
title="You have no events"
subtitle="Go back to homepage to see all our available events!"
image={require("../assets/empty.png")}
/>
) : (
<CardsContainer>
{events.map((event: ApplicationInformationWithoutUser) => (
<CardMyEvents
key={event.id}
title={event.event.name}
headerImage={event.event.header_image}
startDate={event.event.start_date}
status={event.status}
/>
))}
{applications.map(
(application: ApplicationInformationWithoutUser) => (
<CardMyEvents
key={application.id}
title={application.event.name}
headerImage={application.event.header_image}
startDate={application.event.start_date}
status={application.status}
id={application.id}
setIdToCancel={setIdToCancel}
setShowCancelAlert={setShowCancelAlert}
/>
),
)}
</CardsContainer>
)}
</View>
</ScrollView>
)}
<ConfirmDialog
title="Are you sure you want to cancel your application?"
message="Are you sure about that? This only way you can request your participation is by contacting the organizers"
onTouchOutside={() => setShowCancelAlert(false)}
visible={showCancelAlert}
negativeButton={{
title: "Cancel",
onPress: () => {
setShowCancelAlert(false);
},
titleStyle: {
color: "red",
fontSize: 20,
},
style: {
backgroundColor: "transparent",
paddingHorizontal: 10,
},
}}
positiveButton={{
title: "Cancel!",
onPress: () => {
cancel();
},
titleStyle: {
color: "blue",
fontSize: 20,
},
style: {
backgroundColor: "transparent",
paddingHorizontal: 10,
},
}}
contentInsetAdjustmentBehavior="automatic"
onRequestClose={() => setShowCancelAlert(false)}
/>
<Toast />
</View>
);
}
23 changes: 23 additions & 0 deletions organizator/utils/api/axiosApplications.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
import {
applicationStatusResponse,
cancelApplicationResponse,
CreateNewApplicationResponse,
GetMyApplicationsResponse,
GetParticipantsResponse,
Expand Down Expand Up @@ -136,3 +137,25 @@ export async function getApplicationStatus(
};
}
}

export async function cancelApplication(
token: string,
applicationId: string,
): Promise<cancelApplicationResponse> {
try {
await axios({
method: "post",
url: `${applicationsAPI}/cancel/${applicationId}`,
headers: {
Authorization: `${token}`,
},
});
return {
error: null,
};
} catch (error: any) {
return {
error: error.response.data,
};
}
}
4 changes: 4 additions & 0 deletions organizator/utils/interfaces/Applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ export interface applicationStatusResponse {
readonly status: string | null;
readonly notApplied: boolean;
}

export interface cancelApplicationResponse {
readonly error: string | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self) -> None:
def execute(self, application_id: uuid.UUID, token: uuid.UUID) -> None:
application = self.application_repository.get(application_id)

if application.user.id != token:
if application.user.token != token:
raise ApplicationIsNotFromUser

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self) -> None:
def execute(self, application_id: uuid.UUID, token: uuid.UUID) -> None:
application = self.application_repository.get(application_id)

if application.user.id != token:
if application.user.token != token:
raise ApplicationIsNotFromUser

if application.status != ApplicationStatus.INVITED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def _to_domain_model(self, orm_application: ORMEventApplication) -> Application:
date_of_birth=orm_application.user.date_of_birth,
study=orm_application.user.study,
work=orm_application.user.work,
token=orm_application.user.token,
),
event=Event(
id=orm_application.event.id,
Expand Down
Loading