Skip to content

Commit

Permalink
add useFormFieldValue hook & use in lang fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mirekys committed Nov 10, 2023
1 parent f872206 commit 7b1bb0d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const I18nRichInputField = ({
<GroupField fieldPath={fieldPath} optimized>
<LanguageSelectField
fieldPath={`${fieldPath}.lang`}
placeholder=""
required
width={lngFieldWidth}
usedLanguages={usedLanguages}
Expand All @@ -31,7 +30,6 @@ export const I18nRichInputField = ({
editorConfig={editorConfig}
// TODO: hacky fix for SUI alignment bug for case with
// field groups with empty field label on one of inputs

className={`${!label ? "mt-25" : ""}`}
fieldPath={`${fieldPath}.value`}
label={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { FieldLabel } from "react-invenio-forms";
import { LocalVocabularySelectField } from "@js/oarepo_vocabularies";
import { i18next } from "@translations/oarepo_ui/i18next";
import PropTypes from "prop-types";
import { useFormConfig } from "@js/oarepo_ui";

export const LanguageSelectField = ({
fieldPath,
Expand All @@ -17,9 +16,6 @@ export const LanguageSelectField = ({
value,
...uiProps
}) => {
const {
formConfig: { default_locale },
} = useFormConfig();

return (
<LocalVocabularySelectField
Expand All @@ -35,7 +31,6 @@ export const LanguageSelectField = ({
onChange={({ e, data, formikProps }) => {
formikProps.form.setFieldValue(fieldPath, data.value);
}}
defaultValue={multiple ? [default_locale] : default_locale}
{...uiProps}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import {
I18nTextInputField,
I18nRichInputField,
ArrayFieldItem,
useDefaultLocale,
useFormFieldValue,
} from "@js/oarepo_ui";
import { i18next } from "@translations/oarepo_ui/i18next";
import { useFormikContext, getIn } from "formik";
import _get from "lodash/get";

export const MultilingualTextInput = ({
fieldPath,
Expand All @@ -24,10 +28,21 @@ export const MultilingualTextInput = ({
lngFieldWidth,
...uiProps
}) => {
const { defaultLocale } = useDefaultLocale();
const { values } = useFormikContext();
const { usedSubValues, defaultNewValue } = useFormFieldValue({
defaultValue: defaultLocale,
fieldPath,
subValuesPath: "lang",
});
const value = getIn(values, fieldPath);
const usedLanguages = usedSubValues(value)
const newValue = defaultNewValue(emptyNewInput, usedLanguages);

return (
<ArrayField
addButtonLabel={addButtonLabel}
defaultNewValue={emptyNewInput}
defaultNewValue={newValue}
fieldPath={fieldPath}
label={
<FieldLabel htmlFor={fieldPath} icon={labelIcon ?? ""} label={label} />
Expand All @@ -52,7 +67,7 @@ export const MultilingualTextInput = ({
editorConfig={editorConfig}
optimized
required={required}
usedLanguages={array.map((v) => v.lang)}
usedLanguages={usedLanguages}
lngFieldWidth={lngFieldWidth}
{...uiProps}
/>
Expand All @@ -62,7 +77,7 @@ export const MultilingualTextInput = ({
label={textFieldLabel}
labelIcon={textFieldIcon}
required={required}
usedLanguages={array.map((v) => v.lang)}
usedLanguages={usedLanguages}
lngFieldWidth={lngFieldWidth}
{...uiProps}
/>
Expand Down
30 changes: 25 additions & 5 deletions oarepo_ui/theme/assets/semantic-ui/js/oarepo_ui/forms/hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as React from "react";
import { FormConfigContext } from "./contexts";
import { OARepoDepositApiClient, OARepoDepositSerializer } from "../api";
import { useFormikContext } from "formik";
import { useFormikContext, getIn } from "formik";
import _get from "lodash/get"
import _set from "lodash/set"
import _omit from "lodash/omit";
import _pick from "lodash/pick";
import _isEmpty from "lodash/isEmpty";
Expand All @@ -18,6 +20,14 @@ export const useFormConfig = () => {
return context;
};

export const useDefaultLocale = () => {
const {
formConfig: { default_locale },
} = useFormConfig();

return { defaultLocale: default_locale }
}

export const useVocabularyOptions = (vocabularyType) => {
const {
formConfig: { vocabularies },
Expand All @@ -35,6 +45,16 @@ export const useConfirmationModal = () => {
return { isModalOpen, handleCloseModal, handleOpenModal };
};

export const useFormFieldValue = ({ fieldPath, subValuesPath, defaultValue, subValuesUnique = true }) => {
const usedSubValues = (value) =>
value && typeof Array.isArray(value)
? value.map((val) => _get(val, "lang")) || []
: [];
const defaultNewValue = (initialVal, usedSubValues = []) => _set({...initialVal}, subValuesPath, !usedSubValues?.includes(defaultValue) || !subValuesUnique ? defaultValue : "")

return { usedSubValues, defaultNewValue }
}

export const useDepositApiClient = (
baseApiClient,
serializer,
Expand Down Expand Up @@ -71,7 +91,7 @@ export const useDepositApiClient = (
? new baseApiClient(createUrl, recordSerializer)
: new OARepoDepositApiClient(createUrl, recordSerializer);

async function save() {
async function save () {
let response;

setSubmitting(true);
Expand Down Expand Up @@ -131,7 +151,7 @@ export const useDepositApiClient = (
}
}

async function publish() {
async function publish () {
// call save and if save returns false, exit
const saveResult = await save();
if (!saveResult) return;
Expand Down Expand Up @@ -176,11 +196,11 @@ export const useDepositApiClient = (
}
}

async function read(recordUrl) {
async function read (recordUrl) {
return await apiClient.readDraft({ self: recordUrl });
}

async function _delete(redirectUrl) {
async function _delete (redirectUrl) {
if (!redirectUrl)
throw new Error(
"You must provide url where to be redirected after deleting a draft"
Expand Down

0 comments on commit 7b1bb0d

Please sign in to comment.