Skip to content

Commit

Permalink
feat: Update only changed cuf
Browse files Browse the repository at this point in the history
  • Loading branch information
d-beezee committed Jan 16, 2024
1 parent ce6091f commit a8fc270
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/pages/Profile/TabAdvanced/CustomUserFields.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Accordion, Skeleton } from "@appquality/appquality-design-system";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch } from "react-redux";
import { useGetCustomUserFieldsQuery } from "src/services/tryberApi";
import CufField from "./CufField";

export const CustomUserFields = () => {
const { i18n } = useTranslation();
const dispatch = useDispatch();
const { data: customUserFields } = useGetCustomUserFieldsQuery();

const [isLoading, setIsLoading] = useState(true);
Expand Down
21 changes: 19 additions & 2 deletions src/pages/Profile/TabAdvanced/MapCufValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,29 @@ export const MapCufValues = () => {
validationSchema: validationSchema,
};
};
export const PrepareUserCuf = (values: FormikValues) => {
export const PrepareUserCuf = (
values: FormikValues,
initialUserValues: FormikValues
) => {
const cufToSave: {
id: string;
values: ApiOperations["put-users-me-additionals-fieldId"]["requestBody"]["content"]["application/json"];
}[] = [];
Object.keys(values).forEach((key) => {

const changedCufValues = Object.entries(values).reduce(
(acc, [key, value]) => {
const hasChanged =
JSON.stringify(initialUserValues[key]) !== JSON.stringify(value);

if (hasChanged) {
acc[key] = value;
}

return acc;
},
{} as any
);
Object.keys(changedCufValues).forEach((key) => {
//key == cuf_n, certifications, certificationRadio, education, employment
if (key.includes("cuf_")) {
if (typeof values[key] === "string") {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/Profile/TabAdvanced/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ const TabAdvanced = () => {
validationSchema={yup.object(validationSchema)}
onSubmit={async (values, helpers) => {
helpers.setSubmitting(true);
const readyCuf = PrepareUserCuf(values);

const readyCuf = PrepareUserCuf(values, initialUserValues);
try {
await updateProfile({
body: {
Expand Down

0 comments on commit a8fc270

Please sign in to comment.