summaryrefslogtreecommitdiff
path: root/src/clients
diff options
context:
space:
mode:
Diffstat (limited to 'src/clients')
-rw-r--r--src/clients/actions.ts18
-rw-r--r--src/clients/api_client.ts30
-rw-r--r--src/clients/loaders.ts1
3 files changed, 37 insertions, 12 deletions
diff --git a/src/clients/actions.ts b/src/clients/actions.ts
new file mode 100644
index 0000000..36935cb
--- /dev/null
+++ b/src/clients/actions.ts
@@ -0,0 +1,18 @@
+import { redirect } from "react-router-dom";
+import { ApiClient } from "./api_client";
+import { deleteEmptyFields } from "../lib/form_utils";
+
+export async function editAccount({ request }) {
+ const client = new ApiClient();
+ let form_data = await request.formData();
+ form_data = deleteEmptyFields(form_data);
+ const response = await client.put("/account", form_data);
+
+ if(response.status == 401)
+ return redirect("/products");
+
+ client.token.set(response.data.token);
+ client.token.setRefresh(response.data.refresh);
+
+ return redirect("/account");
+} \ No newline at end of file
diff --git a/src/clients/api_client.ts b/src/clients/api_client.ts
index fc2d361..93d713a 100644
--- a/src/clients/api_client.ts
+++ b/src/clients/api_client.ts
@@ -3,27 +3,19 @@ import Token from "../lib/token";
export class ApiClient {
readonly url = "http://localhost:3000/api";
- token = new Token();
+ public readonly token = new Token();
async authenticatedGet(path: string) {
const request_url = `${ this.url }${ path }`;
let request: any;
- let options = {
- headers: {
- Authorization: this.token.get()
- }
- };
+ let options = this.authorizationHeaders();
request = await this.makeGetRequest(request_url, options);
if(request.response) {
// Let's try with a refreshed token.
this.token.refresh()
- options = {
- headers: {
- Authorization: this.token.getRefresh()
- }
- };
+ options = this.authorizationHeaders();
request = await this.makeGetRequest(request_url, options);
}
@@ -44,6 +36,14 @@ export class ApiClient {
return response;
}
+ async put(path: string, data: FormData) {
+ const request_url = `${ this.url }${ path }`;
+ const options = this.authorizationHeaders();
+ const response = await axios.put(request_url, data, options);
+
+ return response;
+ }
+
async getProduct(id: string) {
const request_url = `${ this.url }/products/${ id }`;
const [product_response, product_reviews] = await Promise.all([
@@ -63,4 +63,12 @@ export class ApiClient {
return error;
}
}
+
+ private authorizationHeaders() {
+ return {
+ headers: {
+ Authorization: this.token.get()
+ }
+ };
+ }
} \ No newline at end of file
diff --git a/src/clients/loaders.ts b/src/clients/loaders.ts
index d9972d5..8d0f06f 100644
--- a/src/clients/loaders.ts
+++ b/src/clients/loaders.ts
@@ -1,5 +1,4 @@
import { redirect } from "react-router-dom";
-import Token from "../lib/token";
import { ApiClient } from "./api_client";
export async function loader({ request }) {