ajout du post redux de session et teams

redux
Alexandre GLENAT 2 years ago
parent 4754741fab
commit b643be8199

@ -1,4 +1,5 @@
import { Lap } from "./Lap"; import { Lap } from "./Lap";
import { SessionType } from "./SessionType";
export class Session { export class Session {
private name: string; private name: string;

@ -7,14 +7,13 @@ export class Team {
private users: User[]; private users: User[];
private logo: HTMLImageElement; 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.name = name;
this.owner = owner; this.owner = owner;
this.users = users; this.users = users;
this.logo = logo; this.logo = logo;
} }
getName() { getName() {
return this.name; return this.name;
} }

@ -1,3 +1,5 @@
export const FETCH_USERS = 'FETCH_USERS'; export const FETCH_USERS = 'FETCH_USERS';
export const FETCH_TEAMS = 'FETCH_TEAMS'; export const FETCH_TEAMS = 'FETCH_TEAMS';
export const FETCH_SESSIONS = 'FETCH_SESSIONS'; export const FETCH_SESSIONS = 'FETCH_SESSIONS';
export const ADD_TEAM = 'ADD_TEAM';
export const ADD_FILE = 'ADD_FILE';

@ -2,6 +2,7 @@ import { Geocalisation } from "../../core/Geocalisation";
import { Lap } from "../../core/Lap"; import { Lap } from "../../core/Lap";
import { Point } from "../../core/Point"; import { Point } from "../../core/Point";
import { Session } from "../../core/Session"; import { Session } from "../../core/Session";
import { User } from "../../core/User";
import { FETCH_SESSIONS } from "../Constants"; import { FETCH_SESSIONS } from "../Constants";
export const setSessionsList = (sessionsList: Session[]) => { 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 => { return async dispatch => {
try { 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 sessionsListJson = await sessionsPromise.json();
const sessionsList: Session[] = sessionsListJson.map(elt => { const sessionsList: Session[] = sessionsListJson.map(elt => {
const laps: Lap[] = elt.laps.map(lap => { const laps: Lap[] = elt.laps.map(lap => {

@ -1,5 +1,5 @@
import { Team } from "../../core/Team"; import { Team } from "../../core/Team";
import { FETCH_TEAMS } from "../Constants"; import { FETCH_TEAMS, ADD_TEAM } from "../Constants";
export const setTeamsList = (teamsList: Team[]) => { export const setTeamsList = (teamsList: Team[]) => {
return { 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 = () => { export const getTeamsList = () => {
return async dispatch => { return async dispatch => {
try { try {
const teamsPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Ecuries'); const teamsPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Ecuries');
console.log(teamsPromise);
const teamsListJson = await teamsPromise.json(); const teamsListJson = await teamsPromise.json();
const teamsList: Team[] = teamsListJson.map(elt => new Team(elt["name"], elt["owner"], elt["users"], elt["logo"])); const teamsList: Team[] = teamsListJson.map(elt => new Team(elt["name"], elt["owner"], elt["users"], elt["logo"]));
dispatch(setTeamsList(teamsList)); dispatch(setTeamsList(teamsList));
@ -21,4 +42,4 @@ export const getTeamsList = () => {
//dispatch(fetchDataRejected(error)) //dispatch(fetchDataRejected(error))
} }
} }
} }

@ -16,7 +16,7 @@ export const setUsersList = (usersList: User[]) => {
export const getUsersList = (team: Team) => { export const getUsersList = (team: Team) => {
return async dispatch => { return async dispatch => {
try { 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 usersListJson = await usersPromise.json();
const dto: DtoUserEcurie = usersListJson.map(elt => new DtoUserEcurie(elt["owner"], elt["members"], elt["waitingMember"])); const dto: DtoUserEcurie = usersListJson.map(elt => new DtoUserEcurie(elt["owner"], elt["members"], elt["waitingMember"]));
const usersList: User[] = [] const usersList: User[] = []

@ -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 = { const initialState = {
teams: [], teams: [],
users: [], users: [],
@ -6,11 +10,15 @@ const initialState = {
const appReducer = (state = initialState, action: { type: any; payload: any; }) => { const appReducer = (state = initialState, action: { type: any; payload: any; }) => {
switch (action.type) { 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 }; return { ...state, teams: action.payload };
case 'FETCH_USERS': case FETCH_USERS:
return { ...state, users: action.payload }; return { ...state, users: action.payload };
case 'FETCH_SESSIONS': case FETCH_SESSIONS:
return { ...state, sessions: action.payload }; return { ...state, sessions: action.payload };
default: default:
return state; return state;

@ -1,8 +1,10 @@
import React, { useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Button, Pressable } from 'react-native'; import { Button, Pressable } from 'react-native';
import { StyleSheet, Text, View, Image, TextInput } from 'react-native'; import { StyleSheet, Text, View, Image, TextInput } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context'; import { SafeAreaView } from 'react-native-safe-area-context';
import * as DocumentPicker from 'expo-document-picker'; 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 }) { export default function CreateTeam(props: { navigation: any }) {
const { navigation } = props; 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 ( return (
<SafeAreaView style={styles.container}> <SafeAreaView style={styles.container}>
<View style={styles.maincard}> <View style={styles.maincard}>

Loading…
Cancel
Save