Skip to content

Commit 0bd81c5

Browse files
Merge pull request #78 from JerryFalimanana/develop
Develop
2 parents 8bee2ee + 9ead7d9 commit 0bd81c5

File tree

6 files changed

+99
-16
lines changed

6 files changed

+99
-16
lines changed

assets/js/config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const API_URL = "http://localhost:8000/api/";
2+
export const CUSTOMERS_API = API_URL + "customers";
3+
export const INVOICES_API = API_URL + "invoices";
4+
export const USERS_API = API_URL + "users";
5+
export const LOGIN_API = API_URL + "login_check";

assets/js/services/authAPI.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import axios from "axios";
22
import jwtDecode from "jwt-decode";
3+
import { LOGIN_API } from "../config";
34

45
function authenticate(credentials) {
5-
return axios.post("http://localhost:8000/api/login_check", credentials)
6+
return axios.post(LOGIN_API, credentials)
67
.then(response => response.data.token)
78
.then(token => {
89
window.localStorage.setItem("authToken", token);

assets/js/services/cache.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const cache = {};
2+
3+
function set(key, data) {
4+
cache[key] = {
5+
data,
6+
cachedAt: new Date().getTime()
7+
}
8+
}
9+
10+
function get(key) {
11+
return new Promise((resolve) => {
12+
resolve(cache[key] && cache[key].cachedAt + 15*60*1000 > new Date().getTime() ? cache[key].data : null);
13+
});
14+
}
15+
16+
function invalidate(key) {
17+
delete cache[key];
18+
}
19+
20+
export default {
21+
set,
22+
get,
23+
invalidate
24+
}

assets/js/services/customersAPI.js

+60-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,76 @@
11
import axios from "axios";
2+
import { CUSTOMERS_API } from "../config";
3+
import cache from "./cache";
24

3-
function findAll() {
4-
return axios.get("http://localhost:8000/api/customers")
5-
.then(response => response.data["hydra:member"]);
5+
async function findAll() {
6+
const cachedCustomers = await cache.get("customers");
7+
8+
if (cachedCustomers) {
9+
return cachedCustomers;
10+
}
11+
12+
return axios.get(CUSTOMERS_API)
13+
.then(response => {
14+
const customers = response.data["hydra:member"];
15+
cache.set("customers", customers);
16+
return customers;
17+
});
618
}
719

8-
function find(id) {
9-
return axios.get("http://localhost:8000/api/customers/" + id)
10-
.then(response => response.data);
20+
async function find(id) {
21+
const cachedCustomer = await cache.get("customers." + id);
22+
if (cachedCustomer) {
23+
return cachedCustomer;
24+
}
25+
return axios.get(CUSTOMERS_API + "/" + id)
26+
.then(response => {
27+
const customer = response.data;
28+
cache.set("customers." + id, customer);
29+
30+
return customer;
31+
});
1132
}
1233

1334
function create(customer) {
14-
return axios.post("http://localhost:8000/api/customers", customer);
35+
return axios.post(CUSTOMERS_API, customer)
36+
.then(async response => {
37+
const cachedCustomers = await cache.get("customers");
38+
if (cachedCustomers) {
39+
cache.set("customers", [...cachedCustomers, response.data]);
40+
}
41+
return response;
42+
});
1543
}
1644

1745
function update(id, customer) {
18-
return axios.put("http://localhost:8000/api/customers/" + id, customer);
46+
return axios.put(CUSTOMERS_API + "/" + id, customer)
47+
.then(async response => {
48+
const cachedCustomers = await cache.get("customers");
49+
const cachedCustomer = await cache.get("customers." + id);
50+
51+
if (cachedCustomer) {
52+
cache.set("customers." + id, response.data);
53+
}
54+
55+
if (cachedCustomers) {
56+
const index = cachedCustomers.findIndex(c => c.id == id);
57+
58+
cachedCustomers[index] = response.data;
59+
}
60+
61+
return response;
62+
});
1963
}
2064

2165
function deleteCustomer(id) {
22-
return axios.delete("http://localhost:8000/api/customers/" + id);
66+
return axios.delete(CUSTOMERS_API + "/" + id)
67+
.then(async response => {
68+
const cachedCustomers = await cache.get("customers");
69+
if (cachedCustomers) {
70+
cache.set("customers", cachedCustomers.filter(c => c.id != id));
71+
}
72+
return response;
73+
});
2374
}
2475

2576
export default {

assets/js/services/invoicesAPI.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import axios from "axios";
2+
import { INVOICES_API } from "../config";
23

34
function findAll() {
4-
return axios.get("http://localhost:8000/api/invoices")
5+
return axios.get(INVOICES_API)
56
.then(response => response.data["hydra:member"]);
67
}
78

89
function find(id) {
910
return axios
10-
.get("http://localhost:8000/api/invoices/" + id)
11+
.get(INVOICES_API + "/" + id)
1112
.then(response => response.data);
1213
}
1314

1415
function create(invoice) {
15-
return axios.post("http://localhost:8000/api/invoices", {...invoice, customer: `/api/customers/${invoice.customer}`});
16+
return axios.post(INVOICES_API, {...invoice, customer: `/api/customers/${invoice.customer}`});
1617
}
1718

1819
function update(id, invoice) {
19-
return axios.put("http://localhost:8000/api/invoices/" + id, {...invoice, customer: `/api/customers/${invoice.customer}`});
20+
return axios.put(INVOICES_API + "/" + id, {...invoice, customer: `/api/customers/${invoice.customer}`});
2021
}
2122

2223
function deleteInvoice(id) {
23-
return axios.delete("http://localhost:8000/api/invoices/" + id);
24+
return axios.delete(INVOICES_API + "/" + id);
2425
}
2526

2627
export default {

assets/js/services/usersAPI.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import axios from "axios";
2+
import { USERS_API } from "../config";
23

34
function register(user) {
4-
return axios.post("http://localhost:8000/api/users", user);
5+
return axios.post(USERS_API, user);
56
}
67

78
export default {

0 commit comments

Comments
 (0)