Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trait intermittently not saving #130

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/routes/BuildMyProfile/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ const BuildMyProfile = () => {
// eslint-disable-next-line no-console
console.log("Failed to create traits", err);
}

try {
await updateTraits(authUser.handle, traitsToCreate);
} catch (err) {
// eslint-disable-next-line no-console
console.log("Failed to create traits", err);
}
}

if (traitsToUpdate.length > 0) {
Expand Down
21 changes: 11 additions & 10 deletions src/routes/ContactDetails/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
isNullOrEmpty,
} from "utils/";
import { sortBy } from "lodash";
import { checkUserTrait } from "services/traits";

const ContactDetails = () => {
const authUser = useSelector((state) => state.authUser);
Expand Down Expand Up @@ -173,14 +174,14 @@ const ContactDetails = () => {
});
}, [countries, authUser]);

const saveMyAddress = (basicInfo) => {
const saveMyAddress = async (basicInfo) => {
// update address
let addressMapped = {
streetAddr1: addressLine1 || "",
streetAddr2: addressLine2 || "",
zip: zipCode || "",
city: city || "",
stateCode: state || "",
zip: zipCode || "",
type: "HOME",
};

Expand All @@ -198,16 +199,17 @@ const ContactDetails = () => {
}
}

// check if basic info already exists. if so, update(put data). otherwise, post data.
if (basicInfo == null && isAddressFormEmpty(addressMapped, basicInfo)) {
return addMyAddress(
// hack to check if the user has an existing basic_info trait object
const exists = await checkUserTrait(authUser.handle, "basic_info");
if (!exists) {
await addMyAddress(
authUser.handle,
addressMapped,
country != null ? countryObj : null
);
} else {
if (isAddressFormEmpty(addressMapped, basicInfo)) {
return updateMyAddress(
await updateMyAddress(
authUser.handle,
basicInfo,
addressMapped,
Expand All @@ -219,7 +221,7 @@ const ContactDetails = () => {
}
};

const saveContactDetails = (contactDetailsOnServer) => {
const saveContactDetails = async (contactDetailsOnServer) => {
// saving contact details
// map data before passing to server
let contactDetailsMapped = {
Expand All @@ -236,11 +238,10 @@ const ContactDetails = () => {
contactDetailsOnServer == null &&
isContactFormEmpty(contactDetailsMapped)
) {
createContactDetails(authUser.handle, contactDetailsMapped);
return Promise.resolve();
await createContactDetails(authUser.handle, contactDetailsMapped);
} else {
if (isContactFormEmpty(contactDetailsMapped)) {
return updateContactDetails(
await updateContactDetails(
authUser.handle,
contactDetailsOnServer,
contactDetailsMapped
Expand Down
9 changes: 7 additions & 2 deletions src/routes/GetStarted/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
isNullOrEmpty,
} from "utils/";
import { getAllCountries } from "services/countries";
import { checkUserTrait } from "services/traits";

const GetStarted = () => {
// states
Expand Down Expand Up @@ -252,10 +253,14 @@ const GetStarted = () => {
return getMyBasicInfo(authUser.handle)
.then(async (result) => {
const basicInfoTraits = getTraits(result?.data[0]);
const exists = result?.data[0].createdAt != null;

if (
// v3 requires a create call (for traitId = "basic_info") if country is missing
(basicInfoTraits == null || isNullOrEmpty(basicInfoTraits.country)) &&
isGetStartedFormDataEmpty(myInterestsFlat)
!exists ||
((basicInfoTraits == null ||
isNullOrEmpty(basicInfoTraits.country)) &&
isGetStartedFormDataEmpty(myInterestsFlat))
) {
if (isNullOrEmpty(basicInfoTraits.country)) {
const response = await getAllCountries();
Expand Down
17 changes: 17 additions & 0 deletions src/services/traits.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ export function createTraits(handle, data) {
export function updateTraits(handle, data) {
return axios.put(`${config.API.V3}/members/${handle}/traits`, wrapV3(data));
}

export async function checkUserTrait(handle, traitId) {
let isExists = false;
let response = await axios.get(
`${config.API.V3}/members/${handle}/traits?traitIds=${traitId}`
);
const dataResponse = response.data;

if (dataResponse.result && dataResponse.result.content.length > 0) {
const trait = dataResponse.result.content[0];
if (trait.createdAt) {
isExists = true;
}
}

return isExists;
}