Add hooks to send data back to the server

This commit is contained in:
Justin Edmund 2022-02-26 15:55:45 -08:00
parent 45ef4e569b
commit aae67b08af
2 changed files with 49 additions and 23 deletions

View file

@ -37,8 +37,11 @@ const Party = (props: Props) => {
// Fetch data from the server // Fetch data from the server
useEffect(() => { useEffect(() => {
const shortcode = (props.slug) ? props.slug : undefined const shortcode = (props.slug) ? props.slug : undefined
if (shortcode) fetchDetails(shortcode)
else appState.party.editable = true if (shortcode)
fetchDetails(shortcode)
else
appState.party.editable = true
}, [props.slug]) }, [props.slug])
// Methods: Creating a new party // Methods: Creating a new party
@ -53,7 +56,7 @@ const Party = (props: Props) => {
return await api.endpoints.parties.create(body, headers) return await api.endpoints.parties.create(body, headers)
} }
// Methods: Updating the party's extra flag // Methods: Updating the party's details
function checkboxChanged(event: React.ChangeEvent<HTMLInputElement>) { function checkboxChanged(event: React.ChangeEvent<HTMLInputElement>) {
appState.party.extra = event.target.checked appState.party.extra = event.target.checked
@ -64,6 +67,27 @@ const Party = (props: Props) => {
} }
} }
function updateDetails(name?: string, description?: string, raid?: Raid) {
if (appState.party.name !== name ||
appState.party.description !== description ||
appState.party.raid?.id !== raid?.id) {
if (appState.party.id)
api.endpoints.parties.update(appState.party.id, {
'party': {
'name': name,
'description': description,
'raid_id': raid?.id
}
}, headers)
.then(() => {
appState.party.name = name
appState.party.description = description
appState.party.raid = raid
})
}
}
// Methods: Navigating with segmented control // Methods: Navigating with segmented control
function segmentClicked(event: React.ChangeEvent<HTMLInputElement>) { function segmentClicked(event: React.ChangeEvent<HTMLInputElement>) {
switch(event.target.value) { switch(event.target.value) {
@ -92,18 +116,12 @@ const Party = (props: Props) => {
} }
function processResult(response: AxiosResponse) { function processResult(response: AxiosResponse) {
// Store the response appState.party.id = response.data.party.id
const party = response.data.party
// Store the party's user-generated details // Store the party's user-generated details
if (party.name) appState.party.name = response.data.party.name
appState.party.name = party.name appState.party.description = response.data.party.description
appState.party.raid = response.data.party.raid
if (party.description)
appState.party.description = party.description
if (party.raid)
appState.party.raid = party.raid
} }
function processError(error: any) { function processError(error: any) {
@ -169,6 +187,7 @@ const Party = (props: Props) => {
</section> </section>
{ <PartyDetails { <PartyDetails
editable={party.editable} editable={party.editable}
updateCallback={updateDetails}
/>} />}
</div> </div>
) )

View file

@ -13,6 +13,7 @@ import RaidDropdown from '~components/RaidDropdown'
// Props // Props
interface Props { interface Props {
editable: boolean editable: boolean
updateCallback: (name?: string, description?: string, raid?: Raid) => void
} }
const PartyDetails = (props: Props) => { const PartyDetails = (props: Props) => {
@ -20,6 +21,7 @@ const PartyDetails = (props: Props) => {
const nameInput = React.createRef<HTMLInputElement>() const nameInput = React.createRef<HTMLInputElement>()
const descriptionInput = React.createRef<HTMLTextAreaElement>() const descriptionInput = React.createRef<HTMLTextAreaElement>()
const raidSelect = React.createRef<HTMLSelectElement>()
const readOnlyClasses = classNames({ const readOnlyClasses = classNames({
'Details': true, 'Details': true,
@ -39,8 +41,6 @@ const PartyDetails = (props: Props) => {
}) })
function handleInputChange(event: React.ChangeEvent<HTMLInputElement>) { function handleInputChange(event: React.ChangeEvent<HTMLInputElement>) {
console.log(event)
event.preventDefault() event.preventDefault()
const { name, value } = event.target const { name, value } = event.target
@ -58,6 +58,14 @@ const PartyDetails = (props: Props) => {
setErrors(newErrors) setErrors(newErrors)
} }
function updateDetails(event: React.ChangeEvent) {
const nameValue = nameInput.current?.value
const descriptionValue = descriptionInput.current?.value
const raid = appSnapshot.raids.find(raid => raid.id == raidSelect.current?.value)
props.updateCallback(nameValue, descriptionValue, raid)
}
const editable = ( const editable = (
<section className={editableClasses}> <section className={editableClasses}>
<CharLimitedFieldset <CharLimitedFieldset
@ -65,22 +73,21 @@ const PartyDetails = (props: Props) => {
placeholder="Name your team" placeholder="Name your team"
value={appSnapshot.party.name} value={appSnapshot.party.name}
limit={50} limit={50}
onBlur={ () => {} } onBlur={updateDetails}
onChange={handleInputChange} onChange={handleInputChange}
error={errors.name} error={errors.name}
ref={nameInput} ref={nameInput}
/> />
<RaidDropdown /> <RaidDropdown
<select> selected={appSnapshot.party.raid?.id || ''}
<option>Belial</option> onBlur={updateDetails}
<option>Beelzebub</option> ref={raidSelect}
<option>Lucifer (Hard)</option> />
</select>
<TextFieldset <TextFieldset
fieldName="name" fieldName="name"
placeholder={"Write your notes here\n\n\nWatch out for the 50% trigger!\nMake sure to click Fediels 1 first\nGood luck with RNG!"} placeholder={"Write your notes here\n\n\nWatch out for the 50% trigger!\nMake sure to click Fediels 1 first\nGood luck with RNG!"}
value={appSnapshot.party.description} value={appSnapshot.party.description}
onBlur={ () => {} } onBlur={updateDetails}
onChange={handleTextAreaChange} onChange={handleTextAreaChange}
error={errors.description} error={errors.description}
ref={descriptionInput} ref={descriptionInput}