diff options
Diffstat (limited to 'src/clients/actions.ts')
-rw-r--r-- | src/clients/actions.ts | 30 |
1 files changed, 30 insertions, 0 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 |