From 5de556b36761de38702f36af3d0f1aa7077da581 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sat, 7 Jan 2023 23:25:55 -0800 Subject: [PATCH] Create teams when arbitrary details are changed These teams have no weapons and won't show up anywhere but the user's profile. We should probably clean out completely empty teams every once in a while with a CRON job. --- components/Party/index.tsx | 76 +++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/components/Party/index.tsx b/components/Party/index.tsx index 3cd98d4b..ac7348b6 100644 --- a/components/Party/index.tsx +++ b/components/Party/index.tsx @@ -59,44 +59,60 @@ const Party = (props: Props) => { } } - function updateDetails(details: DetailsObject) { + async function updateDetails(details: DetailsObject) { if ( appState.party.name !== details.name || appState.party.description !== details.description || appState.party.raid?.id !== details.raid?.id ) { - if (appState.party.id) - api.endpoints.parties - .update(appState.party.id, { - party: { - name: details.name, - description: details.description, - raid_id: details.raid?.id, - charge_attack: details.chargeAttack, - full_auto: details.fullAuto, - auto_guard: details.autoGuard, - clear_time: details.clearTime, - button_count: details.buttonCount, - chain_count: details.chainCount, - turn_count: details.turnCount, - }, - }) - .then(() => { - appState.party.name = details.name - appState.party.description = details.description - appState.party.raid = details.raid + if (!appState.party.id) + await createParty().then((response) => { + // If the party has no ID, create a new party + const party = response.data.party + storeParty(party) - appState.party.chargeAttack = details.chargeAttack - appState.party.fullAuto = details.fullAuto - appState.party.autoGuard = details.autoGuard + // Then, push the browser history to the new party's URL + if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`) + }) - appState.party.clearTime = details.clearTime - appState.party.buttonCount = details.buttonCount - appState.party.chainCount = details.chainCount - appState.party.turnCount = details.turnCount + // Update the party + await sendUpdate(details) + } + } - appState.party.updated_at = party.updated_at - }) + async function sendUpdate(details: DetailsObject) { + if (appState.party.id) { + return await api.endpoints.parties + .update(appState.party.id, { + party: { + name: details.name, + description: details.description, + raid_id: details.raid?.id, + charge_attack: details.chargeAttack, + full_auto: details.fullAuto, + auto_guard: details.autoGuard, + clear_time: details.clearTime, + button_count: details.buttonCount, + chain_count: details.chainCount, + turn_count: details.turnCount, + }, + }) + .then(() => { + appState.party.name = details.name + appState.party.description = details.description + appState.party.raid = details.raid + + appState.party.chargeAttack = details.chargeAttack + appState.party.fullAuto = details.fullAuto + appState.party.autoGuard = details.autoGuard + + appState.party.clearTime = details.clearTime + appState.party.buttonCount = details.buttonCount + appState.party.chainCount = details.chainCount + appState.party.turnCount = details.turnCount + + appState.party.updated_at = party.updated_at + }) } }