Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ducica committed Dec 8, 2023
1 parent aaa6773 commit 849ae0f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions oarepo_ui/theme/assets/semantic-ui/js/oarepo_ui/forms/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,20 @@ export const useDepositApiClient = (
isSubmitting,
values,
validateForm,
setErrors,
setSubmitting,
setValues,
setFieldError,
setFieldValue,
setErrors,
} = formik;
const {
formConfig: { createUrl },
} = useFormConfig();

const [isSaving, setIsSaving] = React.useState(false);
const [isPublishing, setIsPublishing] = React.useState(false);
const [isDeleting, setIsDeleting] = React.useState(false);

const recordSerializer = serializer
? new serializer(internalFieldsArray, keysToRemove)
: new OARepoDepositSerializer(internalFieldsArray, keysToRemove);
Expand All @@ -166,6 +170,8 @@ export const useDepositApiClient = (
let response;

setSubmitting(true);
setIsSaving(true);
setValues(_omit(values, internalFieldsArray));
setErrors({});
try {
response = await apiClient.saveOrCreateDraft(values);
Expand Down Expand Up @@ -220,12 +226,14 @@ export const useDepositApiClient = (
return false;
} finally {
setSubmitting(false);
setIsSaving(false);
}
}

async function publish() {
// call save and if save returns false, exit
const saveResult = await save();

if (!saveResult) {
setFieldValue(
"BEvalidationErrors.errorMessage",
Expand All @@ -248,6 +256,7 @@ export const useDepositApiClient = (
return;
}
setSubmitting(true);
setIsPublishing(true);
let response;
try {
response = await apiClient.publishDraft(saveResult);
Expand Down Expand Up @@ -282,6 +291,7 @@ export const useDepositApiClient = (
return false;
} finally {
setSubmitting(false);
setIsPublishing(false);
}
}

Expand All @@ -295,6 +305,7 @@ export const useDepositApiClient = (
"You must provide url where to be redirected after deleting a draft"
);
setSubmitting(true);
setIsDeleting(true);
try {
let response = await apiClient.deleteDraft(values);

Expand All @@ -314,13 +325,17 @@ export const useDepositApiClient = (
return false;
} finally {
setSubmitting(false);
setIsDeleting(false);
}
}
// we return also recordSerializer and apiClient instances, if someone wants to use this hook
// inside of another hook, they don't have to initialize the instances manually
return {
values,
isSubmitting,
isSaving,
isPublishing,
isDeleting,
save,
publish,
read,
Expand All @@ -335,7 +350,7 @@ export const useDepositApiClient = (
export const useDepositFileApiClient = (file, baseApiClient) => {
const formik = useFormikContext();

const { isSubmitting, values, setSubmitting, setFieldValue, setValues } =
const { isSubmitting, values, setFieldValue, setSubmitting, setValues } =
formik;

const apiClient = baseApiClient
Expand All @@ -348,9 +363,18 @@ export const useDepositFileApiClient = (file, baseApiClient) => {
// TODO: The issue is that files state is in FileUploader component, but the delete function is here=> not sure if best to keep delete func in nrdocs in uploader
// or like this. Or maybe to keep file state in formik's state similar like we keep the record and then delete this key before submitting
async function _delete(file, handleDeletionInUi) {
setSubmitting(true);
setValues(
_omit(values, [
"errors",
"BEvalidationErrors",
"FEvalidationErrors",
"httpErrors",
"successMessage",
])
);

try {
setSubmitting(true);
let response = await apiClient.deleteFile(file?.links);
if (response.status === 204 && handleDeletionInUi) {
handleDeletionInUi(file);
Expand Down

0 comments on commit 849ae0f

Please sign in to comment.