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

144
src/services/PhpApi.ts Normal file
View File

@ -0,0 +1,144 @@
import {UserCtx, UserCtxT, ResponseT, TUser} from '../context/UserContext';
import React, {useContext} from 'react';
import axios from 'axios'
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 updateDog = async ({email, name, phone}:
{email: string, name: string, phone: string}) =>
{
const { setWait } = useContext<UserCtxT | null>(UserCtx) as UserCtxT;
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!'};
}
}
export const getDog = async ( str: string | undefined) =>
{
const { setWait, setDog } = useContext<UserCtxT | null>(UserCtx) as UserCtxT;
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;
}
}
export const registerUser = async ({email,password}:
{email: string, password: string}) =>
{
const { setWait } = useContext<UserCtxT | null>(UserCtx) as UserCtxT;
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!'};
}
}
export 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}) =>
{
const { setWait } = useContext<UserCtxT | null>(UserCtx) as UserCtxT;
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!'};
}
}
export const loginUser = async ({email,password}:{email: string, password: string}) =>
{
const { setWait } = useContext<UserCtxT | null>(UserCtx) as UserCtxT;
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!'};
}
}
export const logout = async () =>
{
const { setUser } = useContext<UserCtxT | null>(UserCtx) as UserCtxT;
await Axios.get('logout.php');
setUser(null);
}