diff --git a/JokesApp/navigation/Navigation.tsx b/JokesApp/navigation/Navigation.tsx index 96e3cb8..cb68ad1 100644 --- a/JokesApp/navigation/Navigation.tsx +++ b/JokesApp/navigation/Navigation.tsx @@ -32,7 +32,7 @@ export function Navigation(){ tabBarShowLabel: false, tabBarStyle: styles.top, }} > - ( } } +export const setPostJoke = (postJoke: CustomJoke): postCustomAction => { + return { + type: SampleActionType.POST_CUSTOM_JOKE, + payload: postJoke + } +} + export const getSampleJoke = () => { return async dispatch => { try { @@ -79,4 +92,30 @@ export const getCompletJokes = (id : number) => { console.log('Error---------', error); } } +} + +export const postJoke = (type : string, setup : string, punchline : string) => { + return async dispatch => { + try { + console.log('type', type, 'setup', setup, 'punchline', punchline); + const reponse = await fetch('https://iut-weather-api.azurewebsites.net/jokes/', { + method: 'POST', + headers: { + Accept: "application/json", + "Content-Type": "application/json", + }, + body: JSON.stringify( + { + type: type, + setup: setup, + punchline: punchline + } + ) + }); + const data = await reponse.json(); + dispatch(setPostJoke(data)); + } catch (error) { + console.log('Error---------', error); + } + } } \ No newline at end of file diff --git a/JokesApp/redux/reducers/sampleJokeReducer.ts b/JokesApp/redux/reducers/sampleJokeReducer.ts index 1a8cb73..490aa74 100644 --- a/JokesApp/redux/reducers/sampleJokeReducer.ts +++ b/JokesApp/redux/reducers/sampleJokeReducer.ts @@ -1,18 +1,21 @@ import {SampleJoke} from "../../model/SampleJoke"; import {Action, SampleActionType} from "../actions/sampleAction"; +import {CustomJoke} from "../../model/CustomJoke"; interface state { sampleJoke: SampleJoke[]; recentJokes: SampleJoke[]; completJoke: SampleJoke; + postJoke: CustomJoke; } // initial state for sampleJokes const initialState: state = { completJoke: {} as SampleJoke, sampleJoke: [], - recentJokes: [] + recentJokes: [], + postJoke: {} as CustomJoke, } // app reducer for sampleJokes @@ -34,6 +37,11 @@ export default appReducer = (state = initialState, action: Action) => { ...state, completJoke: action.payload, } + case SampleActionType.POST_CUSTOM_JOKE: + return { + ...state, + postJoke: action.payload, + } default: return state; diff --git a/JokesApp/screens/AddJokeScreen.tsx b/JokesApp/screens/AddJokeScreen.tsx index beed738..cc60438 100644 --- a/JokesApp/screens/AddJokeScreen.tsx +++ b/JokesApp/screens/AddJokeScreen.tsx @@ -1,33 +1,169 @@ -import React from "react"; - -import {FlatList, Image, SafeAreaView, ScrollView, SectionListComponent, StyleSheet, Text, View} from "react-native"; -import {indigo, purpleColor} from "../Theme"; +import React, { useEffect, useState } from "react"; +import { Button, StyleSheet, Text, TextInput, TouchableOpacity, View } from "react-native"; +import { darksalmonColor, indigo, purpleColor, whiteColor } from "../Theme"; +import { useDispatch, useSelector } from "react-redux"; +import { getSampleJoke, postJoke } from "../redux/actions/sampleAction"; export function AddJokeScreen() { + const [joke, setJoke] = useState(""); + const [jokeFall, setJokeFall] = useState(""); + const [category, setCategory] = useState(""); + const [categoryExceeded, setCategoryExceeded] = useState(false); + const [buttonDisabled, setButtonDisabled] = useState(true); // État pour activer/désactiver le bouton "CRÉER" + const MAX_CATEGORY_LENGTH = 10; + const dispatch = useDispatch(); + + useEffect(() => { + if (joke !== "" && jokeFall !== "" && category !== "") { + setButtonDisabled(false); + } else { + setButtonDisabled(true); + } + }, [joke, jokeFall, category]); + const postjoke = async () => { + // @ts-ignore + await dispatch(postJoke(joke, jokeFall, category)); + clearFields(); + }; + + const clearFields = () => { + setJoke(""); + setJokeFall(""); + setCategory(""); + setCategoryExceeded(false); + setButtonDisabled(true); + }; + + const handleCategoryChange = (text) => { + if (text.length > MAX_CATEGORY_LENGTH) { + setCategoryExceeded(true); + } else { + setCategoryExceeded(false); + setCategory(text); + } + }; + return ( - Ajouter une blague - In Work - - ); -} + Blague + setJoke(text)} + /> + Chute de la blague + setJokeFall(text)} + /> + Catégorie + + + + {category.length}/{MAX_CATEGORY_LENGTH} + + + + CRÉER + + + EFFACER + + + ); +} const styles = StyleSheet.create({ font: { + backgroundColor: purpleColor, + width: "100%", + height: "100%", + }, + titleTextInput: { + fontSize: 20, + color: whiteColor, + textAlign: "left", + fontWeight: "bold", + marginTop: 25, + marginVertical: 10, + marginLeft: 10, + }, + textInput: { backgroundColor: indigo, - width: '100%', - height: '100%', + height: "13%", + color: whiteColor, + textAlign: "left", + textAlignVertical: "top", + margin: 10, + padding: 30, + paddingTop: 15, + }, + textInput1: { + backgroundColor: indigo, + height: "9%", + color: whiteColor, + textAlign: "left", + textAlignVertical: "top", + margin: 10, + padding: 20, + paddingTop: 10, + fontSize: 12, + }, + viewButtonCreate: { + backgroundColor: darksalmonColor, + height: "7%", + marginLeft: 10, + marginRight: 10, + marginTop: 20, + borderRadius: 10, + }, + viewButtonClear: { + backgroundColor: whiteColor, + height: "7%", + marginLeft: 10, + marginRight: 10, + marginTop: 20, + borderRadius: 10, + }, + textButton1: { + fontSize: 16, + color: whiteColor, + alignSelf: "center", + padding: 12, + fontWeight: "bold", + }, + textButton2: { + fontSize: 16, + color: darksalmonColor, + alignSelf: "center", + padding: 12, + fontWeight: "bold", + }, + characterCountContainer: { + alignItems: 'flex-end', + bottom: 10, + right: 10, + }, + characterCountText: { + fontSize: 12, + color: whiteColor, }, - text: { - fontSize: 24, - color: 'darksalmon', - textAlign: 'center', - fontWeight: 'bold', - marginVertical: 20, - } - - });