From b643be819998fc47e147fe8b3c446ca43892fe84 Mon Sep 17 00:00:00 2001 From: Alexandre GLENAT Date: Thu, 23 Mar 2023 11:17:21 +0100 Subject: [PATCH] ajout du post redux de session et teams --- R-Dash/core/Session.ts | 1 + R-Dash/core/Team.ts | 3 +-- R-Dash/redux/Constants.ts | 4 +++- R-Dash/redux/actions/sessions.ts | 20 ++++++++++++++++++-- R-Dash/redux/actions/teams.ts | 27 ++++++++++++++++++++++++--- R-Dash/redux/actions/users.ts | 2 +- R-Dash/redux/reducers/appReducer.ts | 14 +++++++++++--- R-Dash/screens/CreateTeam.tsx | 15 ++++++++++++++- 8 files changed, 73 insertions(+), 13 deletions(-) diff --git a/R-Dash/core/Session.ts b/R-Dash/core/Session.ts index 80e1c40..3adabdb 100644 --- a/R-Dash/core/Session.ts +++ b/R-Dash/core/Session.ts @@ -1,4 +1,5 @@ import { Lap } from "./Lap"; +import { SessionType } from "./SessionType"; export class Session { private name: string; diff --git a/R-Dash/core/Team.ts b/R-Dash/core/Team.ts index fe9811a..bafbecf 100644 --- a/R-Dash/core/Team.ts +++ b/R-Dash/core/Team.ts @@ -7,14 +7,13 @@ export class Team { private users: User[]; private logo: HTMLImageElement; - constructor(name: string, owner: User,users: User[] = [], logo: HTMLImageElement = require('../assets/images/people.jpg')) { + constructor(name: string, owner: User ,users: User[] = [], logo: HTMLImageElement = require('../assets/images/people.jpg')) { this.name = name; this.owner = owner; this.users = users; this.logo = logo; } - getName() { return this.name; } diff --git a/R-Dash/redux/Constants.ts b/R-Dash/redux/Constants.ts index e063f3e..c64d810 100644 --- a/R-Dash/redux/Constants.ts +++ b/R-Dash/redux/Constants.ts @@ -1,3 +1,5 @@ export const FETCH_USERS = 'FETCH_USERS'; export const FETCH_TEAMS = 'FETCH_TEAMS'; -export const FETCH_SESSIONS = 'FETCH_SESSIONS'; \ No newline at end of file +export const FETCH_SESSIONS = 'FETCH_SESSIONS'; +export const ADD_TEAM = 'ADD_TEAM'; +export const ADD_FILE = 'ADD_FILE'; \ No newline at end of file diff --git a/R-Dash/redux/actions/sessions.ts b/R-Dash/redux/actions/sessions.ts index 627162a..f883fd5 100644 --- a/R-Dash/redux/actions/sessions.ts +++ b/R-Dash/redux/actions/sessions.ts @@ -2,6 +2,7 @@ import { Geocalisation } from "../../core/Geocalisation"; import { Lap } from "../../core/Lap"; import { Point } from "../../core/Point"; import { Session } from "../../core/Session"; +import { User } from "../../core/User"; import { FETCH_SESSIONS } from "../Constants"; export const setSessionsList = (sessionsList: Session[]) => { @@ -11,10 +12,25 @@ export const setSessionsList = (sessionsList: Session[]) => { }; } -export const getSessionsList = () => { +export const addXlsFile = async (file: File) => { + try { + const formData = new FormData(); + formData.append('file', file); + const response = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/upload', { + method: 'POST', + body: formData + }); + const data = await response.json(); + return data; + } catch (error) { + console.log('Error---------', error); + } +}; + +export const getSessionsList = (user: User) => { return async dispatch => { try { - const sessionsPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Sessions'); + const sessionsPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Sessions/'+user.getUsername); const sessionsListJson = await sessionsPromise.json(); const sessionsList: Session[] = sessionsListJson.map(elt => { const laps: Lap[] = elt.laps.map(lap => { diff --git a/R-Dash/redux/actions/teams.ts b/R-Dash/redux/actions/teams.ts index 8da7204..5f6c042 100644 --- a/R-Dash/redux/actions/teams.ts +++ b/R-Dash/redux/actions/teams.ts @@ -1,5 +1,5 @@ import { Team } from "../../core/Team"; -import { FETCH_TEAMS } from "../Constants"; +import { FETCH_TEAMS, ADD_TEAM } from "../Constants"; export const setTeamsList = (teamsList: Team[]) => { return { @@ -8,11 +8,32 @@ export const setTeamsList = (teamsList: Team[]) => { }; } +export const addNewTeam = (newTeam: Team) => { + return async dispatch => { + try { + const response = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Ecuries', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(newTeam) + }); + const team = await response.json(); + dispatch({ + type: ADD_TEAM, + payload: team + }); + } catch (error) { + console.log('Error---------', error); + //dispatch(fetchDataRejected(error)) + } + } +} + export const getTeamsList = () => { return async dispatch => { try { const teamsPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Ecuries'); - console.log(teamsPromise); const teamsListJson = await teamsPromise.json(); const teamsList: Team[] = teamsListJson.map(elt => new Team(elt["name"], elt["owner"], elt["users"], elt["logo"])); dispatch(setTeamsList(teamsList)); @@ -21,4 +42,4 @@ export const getTeamsList = () => { //dispatch(fetchDataRejected(error)) } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/R-Dash/redux/actions/users.ts b/R-Dash/redux/actions/users.ts index dc0a7ec..32f89b7 100644 --- a/R-Dash/redux/actions/users.ts +++ b/R-Dash/redux/actions/users.ts @@ -16,7 +16,7 @@ export const setUsersList = (usersList: User[]) => { export const getUsersList = (team: Team) => { return async dispatch => { try { - const usersPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Pilotes'); + const usersPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Pilotes/'+team); const usersListJson = await usersPromise.json(); const dto: DtoUserEcurie = usersListJson.map(elt => new DtoUserEcurie(elt["owner"], elt["members"], elt["waitingMember"])); const usersList: User[] = [] diff --git a/R-Dash/redux/reducers/appReducer.ts b/R-Dash/redux/reducers/appReducer.ts index 362b746..0962117 100644 --- a/R-Dash/redux/reducers/appReducer.ts +++ b/R-Dash/redux/reducers/appReducer.ts @@ -1,3 +1,7 @@ +import { addXlsFile } from "../actions/sessions"; +import { addNewTeam } from "../actions/teams"; +import { FETCH_SESSIONS, FETCH_TEAMS, FETCH_USERS, ADD_TEAM, ADD_FILE } from "../Constants"; + const initialState = { teams: [], users: [], @@ -6,11 +10,15 @@ const initialState = { const appReducer = (state = initialState, action: { type: any; payload: any; }) => { switch (action.type) { - case 'FETCH_TEAMS': + case ADD_TEAM: + return { ...state, teams: [...state.teams, addNewTeam] }; + case ADD_FILE: + return { ...state, sessions: [...state.sessions, addXlsFile] }; + case FETCH_TEAMS: return { ...state, teams: action.payload }; - case 'FETCH_USERS': + case FETCH_USERS: return { ...state, users: action.payload }; - case 'FETCH_SESSIONS': + case FETCH_SESSIONS: return { ...state, sessions: action.payload }; default: return state; diff --git a/R-Dash/screens/CreateTeam.tsx b/R-Dash/screens/CreateTeam.tsx index 2a836b8..292cc8b 100644 --- a/R-Dash/screens/CreateTeam.tsx +++ b/R-Dash/screens/CreateTeam.tsx @@ -1,8 +1,10 @@ -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { Button, Pressable } from 'react-native'; import { StyleSheet, Text, View, Image, TextInput } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import * as DocumentPicker from 'expo-document-picker'; +import { useDispatch, useSelector } from 'react-redux'; +import { addNewTeam } from '../redux/actions/teams'; export default function CreateTeam(props: { navigation: any }) { const { navigation } = props; @@ -20,6 +22,17 @@ export default function CreateTeam(props: { navigation: any }) { } }; + const nList = useSelector(state => state.appReducer.teams); + + const dispatch = useDispatch(); + + useEffect(() => { + const addTeams = async () => { + await dispatch(addNewTeam()); + }; + addTeams(); + }, [dispatch]); + return (