Skip to content
Snippets Groups Projects
Commit b482d626 authored by Hugo Brua's avatar Hugo Brua
Browse files

Merge branch 'feature/apis' of git.unistra.fr:erp-sil/rms into feature/apis

parents bb8cac46 ba7165f2
Branches
1 merge request!7🔀 V1
import axios from 'axios'; import axios from 'axios';
import { Product } from '../cart/CartTypes'; import { Product } from '../cart/CartTypes';
import { mockedStocks } from './mocks'; import { mockedStocks } from './mocks';
......
...@@ -6,7 +6,7 @@ export const mockedStocks: any[] = [ ...@@ -6,7 +6,7 @@ export const mockedStocks: any[] = [
price: 0.5, price: 0.5,
subscriber_price: 0.5, subscriber_price: 0.5,
category: { category: {
name: "Boisson" name: "Boissons"
} }
} }
}, },
...@@ -17,7 +17,7 @@ export const mockedStocks: any[] = [ ...@@ -17,7 +17,7 @@ export const mockedStocks: any[] = [
price: 0.5, price: 0.5,
subscriber_price: 0.5, subscriber_price: 0.5,
category: { category: {
name: "Boisson" name: "Boissons"
} }
} }
}, },
...@@ -28,7 +28,7 @@ export const mockedStocks: any[] = [ ...@@ -28,7 +28,7 @@ export const mockedStocks: any[] = [
price: 0.5, price: 0.5,
subscriber_price: 0.5, subscriber_price: 0.5,
category: { category: {
name: "Boisson" name: "Boissons"
} }
} }
}, },
...@@ -39,7 +39,7 @@ export const mockedStocks: any[] = [ ...@@ -39,7 +39,7 @@ export const mockedStocks: any[] = [
price: 0.6, price: 0.6,
subscriber_price: 0.6, subscriber_price: 0.6,
category: { category: {
name: "Snack" name: "Snacks"
} }
} }
}, },
...@@ -50,7 +50,7 @@ export const mockedStocks: any[] = [ ...@@ -50,7 +50,7 @@ export const mockedStocks: any[] = [
price: 0.3, price: 0.3,
subscriber_price: 0.3, subscriber_price: 0.3,
category: { category: {
name: "Snack" name: "Snacks"
} }
} }
}, },
...@@ -61,7 +61,7 @@ export const mockedStocks: any[] = [ ...@@ -61,7 +61,7 @@ export const mockedStocks: any[] = [
price: 0.5, price: 0.5,
subscriber_price: 0.5, subscriber_price: 0.5,
category: { category: {
name: "Snack" name: "Snacks"
} }
} }
}, },
...@@ -77,3 +77,26 @@ export const mockedStocks: any[] = [ ...@@ -77,3 +77,26 @@ export const mockedStocks: any[] = [
} }
}, },
]; ];
export const mockedUsers: any[] = [
{
"student_number": 12345678,
"username": "12345678",
"lastname": "Princelle",
"firstname": "Maxime",
"email": "princelle@etu.unistra.fr",
"date_subscription": null,
"picture": "https://cdn.pixabay.com/photo/2018/09/06/18/26/person-3658927_960_720.png",
"date_birth": "1999-06-05",
},
{
"student_number": 87654321,
"username": "Test User",
"lastname": "Test",
"firstname": "User",
"email": "test@test.test",
"date_subscription": "2021-01-01",
"picture": "https://cdn.pixabay.com/photo/2018/09/06/18/26/person-3658927_960_720.png",
"date_birth": "2000-01-01",
},
];
import axios from 'axios'; import axios from 'axios';
import { mockedUsers } from './mocks';
export class User { export class User {
nom: string; nom: string;
prenom: string; prenom: string;
...@@ -30,7 +32,7 @@ export async function getUsers(): Promise<User[]> { ...@@ -30,7 +32,7 @@ export async function getUsers(): Promise<User[]> {
}).then(({ data }) => data) }).then(({ data }) => data)
.catch(error => { .catch(error => {
console.error(error); console.error(error);
return []; return mockedUsers;
}); });
// Format users data // Format users data
...@@ -46,6 +48,24 @@ export async function getUsers(): Promise<User[]> { ...@@ -46,6 +48,24 @@ export async function getUsers(): Promise<User[]> {
return users; return users;
}; };
export async function setAdhesion() : Promise<number> {
// Fetch users data
await axios.post(`${BASE_URL}/api/pay_adhesion`, {
headers: {
'Content-Type': 'application/json',
},
body: {
'student_number': '12345678',
}
}).then(({ data }) => data)
.catch(error => {
console.error(error);
return 0;
});
return 1;
}
function validateSubscriptionDate(dateStr: string): boolean { function validateSubscriptionDate(dateStr: string): boolean {
const YEAR_IN_MS = 31536000000; const YEAR_IN_MS = 31536000000;
const date = Date.parse(dateStr) ?? Date.parse('01 Jan 1970 00:00:00 GMT'); const date = Date.parse(dateStr) ?? Date.parse('01 Jan 1970 00:00:00 GMT');
......
...@@ -6,6 +6,7 @@ import { XIcon } from '@heroicons/react/outline' ...@@ -6,6 +6,7 @@ import { XIcon } from '@heroicons/react/outline'
import { cartContext } from '../../cart/CartStore'; import { cartContext } from '../../cart/CartStore';
import { updateProductQuantity } from '../../cart/CartActions'; import { updateProductQuantity } from '../../cart/CartActions';
import {setAdhesion} from '../../apis/student';
const Cart = () => { const Cart = () => {
const { cartState, dispatch } = useContext(cartContext); const { cartState, dispatch } = useContext(cartContext);
...@@ -15,6 +16,25 @@ const Cart = () => { ...@@ -15,6 +16,25 @@ const Cart = () => {
return `${price.toFixed(2).replace(".", ",")} €`; return `${price.toFixed(2).replace(".", ",")} €`;
} }
function doValidate(): number {
const existingProduct = cartState.cart.find(p => p.name === "Adhésion");
if (existingProduct){
setAdhesion().then((users) => {
console.log("Envoi adhésion à STUDENT: 12345678");
}).catch((_) => {
console.log("error adhesion");
});
}
cartState.totalPrice = 0;
cartState.cart.forEach((product) => {
if (product.quantity) {
cartState.totalPrice += product.price * product.quantity;
}
});
console.log("Envoi transaction à MONEY: ", cartState);
return 1;
}
let [totalPrice, setTotalPrice] = useState(0); let [totalPrice, setTotalPrice] = useState(0);
useEffect(() => { useEffect(() => {
...@@ -164,8 +184,9 @@ const Cart = () => { ...@@ -164,8 +184,9 @@ const Cart = () => {
</div> </div>
<div className="mt-10"> <div className="mt-10">
<button <button
type="submit" onClick={() => doValidate()}
className="w-full bg-indigo-600 border border-transparent rounded-md shadow-sm py-3 px-4 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-50 focus:ring-indigo-500" type="button"
className="w-full bg-indigo-600 border border-transparent rounded-md shadow-sm py-3 px-4 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-50 focus:ring-indigo-500"
> >
Valider Valider
</button> </button>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment