-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: incorrect key name for date key
fixes #778
- Loading branch information
Simon
committed
Jun 19, 2024
1 parent
4e3dc10
commit e4e3370
Showing
2 changed files
with
94 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
client/src/components/ManageArtist/AlbumFormComponents/SavingInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from "react"; | ||
import { useFormContext } from "react-hook-form"; | ||
|
||
import { InputEl } from "components/common/Input"; | ||
import TextArea from "components/common/TextArea"; | ||
|
||
import api from "services/api"; | ||
|
||
import LoadingSpinner from "components/common/LoadingSpinner"; | ||
import { css } from "@emotion/css"; | ||
import useErrorHandler from "services/useErrorHandler"; | ||
import { FaCheck } from "react-icons/fa"; | ||
|
||
const SavingInput: React.FC<{ | ||
formKey: string; | ||
url: string; | ||
extraData: Object; | ||
rows?: number; | ||
required?: boolean; | ||
type?: string; | ||
}> = ({ formKey, url, extraData, type, required, rows }) => { | ||
const { register, getValues } = useFormContext(); | ||
const errorHandler = useErrorHandler(); | ||
|
||
const [isSaving, setIsSaving] = React.useState(false); | ||
const [saveSuccess, setSaveSuccess] = React.useState(false); | ||
|
||
const saveOnBlur = React.useCallback(async () => { | ||
try { | ||
setSaveSuccess(false); | ||
setIsSaving(true); | ||
let value = getValues(formKey); | ||
|
||
if (formKey === "releaseDate") { | ||
value = new Date(value).toISOString(); | ||
} else if (formKey === "minPrice") { | ||
value = value ? value * 100 : undefined; | ||
} | ||
console.log("value", value); | ||
|
||
await api.put<unknown, TrackGroup>(url, { | ||
[formKey]: value, | ||
...extraData, | ||
}); | ||
let timeout2: NodeJS.Timeout; | ||
const timeout = setTimeout(() => { | ||
setIsSaving(false); | ||
setSaveSuccess(true); | ||
timeout2 = setTimeout(() => { | ||
setSaveSuccess(false); | ||
}, 1000); | ||
}, 1000); | ||
return () => { | ||
clearTimeout(timeout2); | ||
clearTimeout(timeout); | ||
}; | ||
} catch (e) { | ||
errorHandler(e); | ||
setIsSaving(false); | ||
} | ||
}, [formKey, getValues, url, extraData]); | ||
|
||
return ( | ||
<div | ||
className={css` | ||
display: flex; | ||
width: 100%; | ||
align-items: center; | ||
input, | ||
textarea { | ||
margin-right: 1rem; | ||
} | ||
`} | ||
> | ||
{!rows && ( | ||
<InputEl | ||
{...register(formKey)} | ||
onBlur={saveOnBlur} | ||
type={type} | ||
required={required} | ||
/> | ||
)} | ||
{rows && <TextArea {...register(formKey)} rows={7} onBlur={saveOnBlur} />} | ||
{isSaving && <LoadingSpinner />} | ||
{saveSuccess && <FaCheck />} | ||
</div> | ||
); | ||
}; | ||
|
||
export default SavingInput; |