Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import LoginPage from "pages/LoginPage";
import StorePage from "pages/StorePage";
import React, { useContext, useEffect, FC } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import { INVENTORY_URI } from "./api/";
import { StateContext } from "./state/state";
import { setInventory } from "./state/actions";
import { fetchInventory } from "./api/";
Expand All @@ -15,7 +14,7 @@ const App: FC = () => {
const { user } = state;
useEffect(() => {
const getData = async () => {
const data = await fetchInventory(INVENTORY_URI);
const data = await fetchInventory();

if (!state.inventory.length) {
dispatch(setInventory(data));
Expand Down
27 changes: 5 additions & 22 deletions src/api/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
import {
authorizedGet,
authorizedPost,
LOGIN_URI,
REGISTER_RFID_URI,
} from "api";
import { LOGIN_URL, REGISTER_URL, get, post } from "api";
import { UserResponse } from "types/api";
import { User } from "types/user";
import { fetchToken } from "api/token";

const getUser = async (url: string): Promise<Response> => {
const response = await authorizedGet({ url });
// No token
if (response.status == 401) {
await fetchToken();
const retryResponse = await authorizedGet({ url });
return retryResponse;
}
return response;
};

export const login = async (rfid: string): Promise<UserResponse> => {
const url = LOGIN_URI(rfid);
const response = await getUser(url);
const url = LOGIN_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!, rfid);
const response = await get({ url });
const json = await response.json();
return json as UserResponse;
};

export const handleRfid = async (rfid: string): Promise<User | null> => {
const user = await login(rfid);
if (user.count) {
// As it returns a weird response
const { pk, saldo, first_name } = user.results[0]; // The first and only user
return { pk, balance: saldo, first_name };
}
Expand All @@ -41,10 +23,11 @@ export const registerUser = (
password: string,
rfid: string
): Promise<Response> => {
const url = REGISTER_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!);
const data = {
username,
password,
rfid,
};
return authorizedPost({ url: REGISTER_RFID_URI, body: data });
return post({ url, body: data });
};
56 changes: 10 additions & 46 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { loadToken } from "api/token";
import { Product } from "../types/inventory";

export const CLIENT_ID = encodeURIComponent(
process.env.REACT_APP_CLIENT_ID || ""
);
export const CLIENT_SECRET = encodeURIComponent(
process.env.REACT_APP_CLIENT_SECRET || ""
);
export const LOGIN_URL = (code: string, rfid: string): string =>
`https://nibble4-validator.azurewebsites.net/api/login?code=${code}&&rfid=${rfid}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should make https://nibble4-validator.azurewebsites.net/api/ into a base variable.

export const FETCH_INVENTORY_URL = (code: string): string =>
`https://nibble4-validator.azurewebsites.net/api/fetchInventory?code=${code}`;
export const PURCHASE_URL = (code: string): string =>
`https://nibble4-validator.azurewebsites.net/api/purchase?code=${code}`;
export const REGISTER_URL = (code: string): string =>
`https://nibble4-validator.azurewebsites.net/api/createUser?code=${code}`;

const API_BASE = process.env.REACT_APP_API_BASE || "";
export const AUTHORIZE_URI = `${API_BASE}/auth/`;
export const REGISTER_RFID_URI = `${API_BASE}/rfid/`;
export const INVENTORY_URI = `${process.env.REACT_APP_API_BASE}/inventory/`;
export const BALANCE_URI = `${process.env.REACT_APP_API_BASE}/transactions/`; // Update balance
export const TRANSACTION_URI = `${process.env.REACT_APP_API_BASE}/orderline/`; // purchase item
export const LOGIN_URI = (rfid: string): string =>
`${API_BASE}/usersaldo/?rfid=${rfid}`;
export const IMAGE_URI = (sm: string): string => `https://online.ntnu.no/${sm}`;

type AJAXArguments = {
Expand All @@ -31,21 +24,6 @@ export const get = ({ url, body, headers }: AJAXArguments): Promise<Response> =>
headers: headers,
});

export const authorizedGet = ({
url,
body,
headers,
}: AJAXArguments): Promise<Response> =>
get({
url,
body,
headers: {
...headers,
Authorization: `Bearer ${loadToken()}`,
"Content-Type": "application/json",
},
});

export const post = ({
url,
body,
Expand All @@ -57,22 +35,8 @@ export const post = ({
headers,
});

export const authorizedPost = ({
url,
body,
headers,
}: AJAXArguments): Promise<Response> =>
post({
url,
body,
headers: {
...headers,
"Content-Type": "application/json",
Authorization: `Bearer ${loadToken()}`,
},
});

export const fetchInventory = async (url: string): Promise<Product[]> => {
export const fetchInventory = async (): Promise<Product[]> => {
const url = FETCH_INVENTORY_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!);
const response = await get({ url });
if (response.ok) {
const json = await response.json();
Expand Down
5 changes: 3 additions & 2 deletions src/api/order.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { TRANSACTION_URI as url, authorizedPost } from "./index";
import { PURCHASE_URL, post } from "./index";
import { CartItem } from "../types/inventory";
import { OrderLineFormat } from "../types/api";

const purchaseItems = (
id: number,
orders: { [id: number]: CartItem }
): Promise<Response> => {
const url = PURCHASE_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!);
const body: OrderLineFormat = {
user: id,
orders: Object.values(orders),
};
return authorizedPost({ url, body });
return post({ url, body });
};

export default purchaseItems;
20 changes: 0 additions & 20 deletions src/api/token.ts

This file was deleted.