phpapi not ready

This commit is contained in:
Peter Hoppe
2023-01-18 16:58:10 +01:00
parent da15265766
commit 3cee42fd50
9 changed files with 215 additions and 209 deletions

View File

@ -1,6 +1,6 @@
import React from 'react'
import {createContext, useState, useEffect} from 'react'
import axios from 'axios'
import React from 'react';
import {createContext, useState, useEffect} from 'react';
import { Axios } from '../services/PhpApi';
export type TUserContextProviderProps =
{
@ -45,162 +45,24 @@ export type ResponseT =
export type UserCtxT =
{
user: TUser | null,
setUser: React.Dispatch<React.SetStateAction<TUser | null>>,
registerUser: ({ email, password }: {
email: string;
password: string;
}) => Promise<any>,
updateDog: ({ email, name, phone }: {
email: string;
name: string;
phone: string;
}) => Promise<any>,
updateQR: ({qr_width_cm, qr_height_cm, qr_fontsize, qr_visible_items, qr_item_sequence}: {
qr_width_cm: number;
qr_height_cm: number;
qr_fontsize: number;
qr_visible_items: number;
qr_item_sequence: number;
}) => Promise<any>,
loginUser: (
{ email, password } :
{
email: string;
password: string;
},
) => Promise<{
success: number;
message?: undefined;
} | {
success: number;
message: any;
}>,
wait: boolean,
getUser: () => Promise<void>,
wait: boolean,
setWait: React.Dispatch<React.SetStateAction<boolean>>,
logout: () => void,
getDog: (str: string | undefined) => Promise<ResponseT>,
dog: ResponseT
dog: ResponseT,
setDog: React.Dispatch<any>
}
export const UserCtx = createContext<UserCtxT | null>(null);
export const Axios = axios.create({
// baseURL: 'http://localhost/dog/php-dog/',
// baseURL: 'https://hope-fly.de/dog/php-dog/',
baseURL: process.env.REACT_APP_PHP_ROOT,
});
export const UserCtxProvider = ({children}:TUserContextProviderProps) => {
const [user, setUser] = useState<TUser | null>(null);
const [wait, setWait] = useState(false);
const [dog, setDog] = useState<ResponseT>({});
const registerUser = async ({email,password}:
{email: string, password: string}) => {
setWait(true);
try{
const {data} = await Axios.post('register.php',{
email,
password
});
setWait(false);
return data;
}
catch(err){
setWait(false);
return {success:0, message:'Server Error!'};
}
}
const updateDog = async ({email, name, phone}:
{email: string, name: string, phone: string}) => {
setWait(true);
try{
const {data} = await Axios.post('updateDog.php',{
email,
name,
phone
});
setWait(false);
return data;
}
catch(err){
setWait(false);
return {success:0, message:'Server Error!'};
}
}
const updateQR = async ({qr_width_cm, qr_height_cm, qr_fontsize, qr_visible_items, qr_item_sequence}:
{qr_width_cm: number, qr_height_cm: number, qr_fontsize: number, qr_visible_items: number, qr_item_sequence: number}) => {
setWait(true);
try{
const {data} = await Axios.post('updateQR.php',{
qr_width_cm,
qr_height_cm,
qr_fontsize,
qr_visible_items,
qr_item_sequence
});
setWait(false);
return data;
}
catch(err){
setWait(false);
return {success:0, message:'Server Error!'};
}
}
const loginUser = async ({email,password}:{email: string, password: string}) =>
{
setWait(true);
try
{
var postReturn:
{
data:
{
success: number;
message: string;
data: TUser | null;
}
}
= { data : {success: 0, message: '', data: null } };
postReturn = await Axios.post('login.php',{
email,
password
});
const {data} = postReturn;
if(data.success && data.data)
{
setUser(data.data);
setWait(false);
return {success:1};
}
setWait(false);
return {success:0, message:data.message};
}
catch(err){
setWait(false);
return {success:0, message:'Server Error!'};
}
}
const getUser = async () =>
{
@ -235,50 +97,17 @@ export const UserCtxProvider = ({children}:TUserContextProviderProps) => {
}
asyncCall();
},[]);
const logout = async () =>
{
await Axios.get('logout.php');
setUser(null);
}
const getDog = async ( str: string | undefined) =>
{
var ret: ResponseT = {};
setWait(true);
try
{
ret = await Axios.post<ResponseT>('getDog.php',
{
qr_id: str
});
setDog(ret.data as ResponseT);
setWait(false);
return;
}
catch (error:any)
{
console.log('error message: ', error.message);
setWait(false);
return;
}
};
return (
<UserCtx.Provider value={
{
registerUser,
updateDog,
updateQR,
loginUser,
wait,
wait,
setWait,
user,
setUser,
getUser,
logout,
getDog,
dog
dog,
setDog
}}
>
{children}