diff options
author | HombreLaser <sebastian-440@live.com> | 2023-05-15 20:35:39 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-05-15 20:35:39 -0600 |
commit | d138c15dcd4272cd2358d28867ef35d1550b39cd (patch) | |
tree | 5807176d23b26bb2851a7c7a341ddc3f48eb4623 /src/clients | |
parent | cbebedd51e1b4a2f709341b792dd073bac83f15d (diff) |
Mejora renderizado de formularios
Diffstat (limited to 'src/clients')
-rw-r--r-- | src/clients/actions.ts | 30 | ||||
-rw-r--r-- | src/clients/api_client.ts | 6 | ||||
-rw-r--r-- | src/clients/loaders.ts | 11 |
3 files changed, 44 insertions, 3 deletions
diff --git a/src/clients/actions.ts b/src/clients/actions.ts index 36935cb..ea3c332 100644 --- a/src/clients/actions.ts +++ b/src/clients/actions.ts @@ -1,6 +1,7 @@ import { redirect } from "react-router-dom"; import { ApiClient } from "./api_client"; import { deleteEmptyFields } from "../lib/form_utils"; +import Token from "../lib/token"; export async function editAccount({ request }) { const client = new ApiClient(); @@ -15,4 +16,33 @@ export async function editAccount({ request }) { client.token.setRefresh(response.data.refresh); return redirect("/account"); +} + +export async function editAddress({ params, request }) { + + const client = new ApiClient(); + let form_data = await request.formData(); + form_data = deleteEmptyFields(form_data); + + try { + const response = await client.put(`/account/addresses/${params.addressId}`, form_data); + + if(response.status == 401 || response.status == 404) + return redirect("/products"); + + return redirect("/account"); + } catch(error) { + if(error.response.status == 401) { + new Token().logout; + + return redirect("/products") + } + else { + for(const [key, value] of Object.entries(error.response.data.errors)) { + sessionStorage.setItem(key, value); + } + + return redirect(`/account/addresses/${params.addressId}/edit`); + } + } }
\ No newline at end of file diff --git a/src/clients/api_client.ts b/src/clients/api_client.ts index 93d713a..4362c4d 100644 --- a/src/clients/api_client.ts +++ b/src/clients/api_client.ts @@ -39,9 +39,9 @@ export class ApiClient { 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; + const response = await axios.put(request_url, data, options); + return response } async getProduct(id: string) { @@ -67,7 +67,7 @@ export class ApiClient { private authorizationHeaders() { return { headers: { - Authorization: this.token.get() + Authorization: `Bearer ${this.token.get()}` } }; } diff --git a/src/clients/loaders.ts b/src/clients/loaders.ts index 8d0f06f..618e593 100644 --- a/src/clients/loaders.ts +++ b/src/clients/loaders.ts @@ -9,6 +9,17 @@ export async function loader({ request }) { return response; } +export async function addressLoader({ params }) { + const client = new ApiClient(); + const path = `/account/addresses/${params.addressId}`; + const request = await client.authenticatedGet(path); + + if(request.response) + return redirect("/account"); + + return request; +} + export async function accountLoader() { const client = new ApiClient(); |