Skip to content

Commit f1055b8

Browse files
authored
Merge pull request #130 from topcoder-platform/develop
fix: trait intermittently not saving
2 parents 93a4bd1 + fa53c29 commit f1055b8

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/routes/BuildMyProfile/index.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,13 @@ const BuildMyProfile = () => {
512512
// eslint-disable-next-line no-console
513513
console.log("Failed to create traits", err);
514514
}
515+
516+
try {
517+
await updateTraits(authUser.handle, traitsToCreate);
518+
} catch (err) {
519+
// eslint-disable-next-line no-console
520+
console.log("Failed to create traits", err);
521+
}
515522
}
516523

517524
if (traitsToUpdate.length > 0) {

src/routes/ContactDetails/index.jsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
isNullOrEmpty,
3838
} from "utils/";
3939
import { sortBy } from "lodash";
40+
import { checkUserTrait } from "services/traits";
4041

4142
const ContactDetails = () => {
4243
const authUser = useSelector((state) => state.authUser);
@@ -173,14 +174,14 @@ const ContactDetails = () => {
173174
});
174175
}, [countries, authUser]);
175176

176-
const saveMyAddress = (basicInfo) => {
177+
const saveMyAddress = async (basicInfo) => {
177178
// update address
178179
let addressMapped = {
179180
streetAddr1: addressLine1 || "",
180181
streetAddr2: addressLine2 || "",
181-
zip: zipCode || "",
182182
city: city || "",
183183
stateCode: state || "",
184+
zip: zipCode || "",
184185
type: "HOME",
185186
};
186187

@@ -198,16 +199,17 @@ const ContactDetails = () => {
198199
}
199200
}
200201

201-
// check if basic info already exists. if so, update(put data). otherwise, post data.
202-
if (basicInfo == null && isAddressFormEmpty(addressMapped, basicInfo)) {
203-
return addMyAddress(
202+
// hack to check if the user has an existing basic_info trait object
203+
const exists = await checkUserTrait(authUser.handle, "basic_info");
204+
if (!exists) {
205+
await addMyAddress(
204206
authUser.handle,
205207
addressMapped,
206208
country != null ? countryObj : null
207209
);
208210
} else {
209211
if (isAddressFormEmpty(addressMapped, basicInfo)) {
210-
return updateMyAddress(
212+
await updateMyAddress(
211213
authUser.handle,
212214
basicInfo,
213215
addressMapped,
@@ -219,7 +221,7 @@ const ContactDetails = () => {
219221
}
220222
};
221223

222-
const saveContactDetails = (contactDetailsOnServer) => {
224+
const saveContactDetails = async (contactDetailsOnServer) => {
223225
// saving contact details
224226
// map data before passing to server
225227
let contactDetailsMapped = {
@@ -236,11 +238,10 @@ const ContactDetails = () => {
236238
contactDetailsOnServer == null &&
237239
isContactFormEmpty(contactDetailsMapped)
238240
) {
239-
createContactDetails(authUser.handle, contactDetailsMapped);
240-
return Promise.resolve();
241+
await createContactDetails(authUser.handle, contactDetailsMapped);
241242
} else {
242243
if (isContactFormEmpty(contactDetailsMapped)) {
243-
return updateContactDetails(
244+
await updateContactDetails(
244245
authUser.handle,
245246
contactDetailsOnServer,
246247
contactDetailsMapped

src/routes/GetStarted/index.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
isNullOrEmpty,
5555
} from "utils/";
5656
import { getAllCountries } from "services/countries";
57+
import { checkUserTrait } from "services/traits";
5758

5859
const GetStarted = () => {
5960
// states
@@ -252,10 +253,14 @@ const GetStarted = () => {
252253
return getMyBasicInfo(authUser.handle)
253254
.then(async (result) => {
254255
const basicInfoTraits = getTraits(result?.data[0]);
256+
const exists = result?.data[0].createdAt != null;
257+
255258
if (
256259
// v3 requires a create call (for traitId = "basic_info") if country is missing
257-
(basicInfoTraits == null || isNullOrEmpty(basicInfoTraits.country)) &&
258-
isGetStartedFormDataEmpty(myInterestsFlat)
260+
!exists ||
261+
((basicInfoTraits == null ||
262+
isNullOrEmpty(basicInfoTraits.country)) &&
263+
isGetStartedFormDataEmpty(myInterestsFlat))
259264
) {
260265
if (isNullOrEmpty(basicInfoTraits.country)) {
261266
const response = await getAllCountries();

src/services/traits.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,20 @@ export function createTraits(handle, data) {
1818
export function updateTraits(handle, data) {
1919
return axios.put(`${config.API.V3}/members/${handle}/traits`, wrapV3(data));
2020
}
21+
22+
export async function checkUserTrait(handle, traitId) {
23+
let isExists = false;
24+
let response = await axios.get(
25+
`${config.API.V3}/members/${handle}/traits?traitIds=${traitId}`
26+
);
27+
const dataResponse = response.data;
28+
29+
if (dataResponse.result && dataResponse.result.content.length > 0) {
30+
const trait = dataResponse.result.content[0];
31+
if (trait.createdAt) {
32+
isExists = true;
33+
}
34+
}
35+
36+
return isExists;
37+
}

0 commit comments

Comments
 (0)