From c60b9887e3e022985052e4d6d1e4ebccaf2bae37 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 3 Jul 2023 19:06:11 -0700 Subject: [PATCH] Update weapon should happen in WeaponUnit Previously, this happened in WeaponModal. It happens in CharacterUnit on that end, so this change brings us in line with how we're doing things elsewhere --- components/weapon/WeaponUnit/index.tsx | 39 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/components/weapon/WeaponUnit/index.tsx b/components/weapon/WeaponUnit/index.tsx index e9c199b7..310505b2 100644 --- a/components/weapon/WeaponUnit/index.tsx +++ b/components/weapon/WeaponUnit/index.tsx @@ -1,7 +1,12 @@ -import React, { useEffect, useState, MouseEvent } from 'react' +import React, { useEffect, useState } from 'react' import { useRouter } from 'next/router' import { Trans, useTranslation } from 'next-i18next' +import { AxiosResponse } from 'axios' import classNames from 'classnames' +import clonedeep from 'lodash.clonedeep' + +import api from '~utils/api' +import { appState } from '~utils/appState' import Alert from '~components/common/Alert' import SearchModal from '~components/search/SearchModal' @@ -16,7 +21,7 @@ import WeaponHovercard from '~components/weapon/WeaponHovercard' import UncapIndicator from '~components/uncap/UncapIndicator' import Button from '~components/common/Button' -import type { SearchableObject } from '~types' +import type { GridWeaponObject, SearchableObject } from '~types' import ax from '~data/ax' @@ -130,6 +135,35 @@ const WeaponUnit = ({ setAlertOpen(false) } + // Methods: Data fetching and manipulation + + async function updateWeapon(object: GridWeaponObject) { + if (gridWeapon) { + return await api.endpoints.grid_weapons + .update(gridWeapon.id, object) + .then((response) => processResult(response)) + .catch((error) => processError(error)) + } + } + + function processResult(response: AxiosResponse) { + const gridWeapon: GridWeapon = response.data + + if (gridWeapon.mainhand) { + appState.grid.weapons.mainWeapon = gridWeapon + appState.party.element = gridWeapon.object.element + } else if (!gridWeapon.mainhand && gridWeapon.position !== null) { + let weapon = clonedeep(gridWeapon) + weapon.element = gridWeapon.object.element + + appState.grid.weapons.allWeapons[gridWeapon.position] = weapon + } + } + + function processError(error: any) { + console.error(error) + } + // Methods: Data fetching and manipulation function getCanonicalAxSkill(index: number) { if ( @@ -430,6 +464,7 @@ const WeaponUnit = ({ gridWeapon={gridWeapon} open={detailsModalOpen} onOpenChange={handleWeaponModalOpenChange} + updateWeapon={updateWeapon} /> ) }