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

Refactor useApiClient hooks and update login and sign up screen #641

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
154 changes: 54 additions & 100 deletions src/hooks/useApiClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
};

function useApiClient() {
const { showErrorToast } = useToastMessages()
const { showErrorToast } = useToastMessages();

const sessionId = useAppSelector((state) => state.auth.sessionId);
const quizId = useAppSelector((state) => state.auth.user.quizId);
const user = useAppSelector((state) => state.auth.user);
Expand Down Expand Up @@ -93,24 +93,15 @@
throw new Error('Missing quizId');
}

const response = await apiCall<responses.GetPersonalValues>(
'get',
'/personal_values?quizId=' + quizId,
{
'X-Session-Id': sessionId,
}
);
const response = await apiCall<responses.GetPersonalValues>('get', '/personal_values?quizId=' + quizId, {
'X-Session-Id': sessionId,
});

return response.data;
}

async function postRegister({ firstName, lastName, email, password, quizId }: requests.PostRegister) {
const response = await apiCall<responses.PostRegister>(
'post',
'/register',
{},
{ firstName, lastName, email, password, quizId }
);
const response = await apiCall<responses.PostRegister>('post', '/register', {}, { firstName, lastName, email, password, quizId });

return response.data;
}
Expand All @@ -121,7 +112,7 @@
'/user-account',
{
'X-Session-Id': sessionId,
'Authorization': 'Bearer ' + user.accessToken,
Authorization: 'Bearer ' + user.accessToken,
},
{ currentPassword: password }
);
Expand Down Expand Up @@ -149,18 +140,20 @@
return response.data;
}

async function postGoogleLogin(credential: string, quizId: string) {
const response = await apiCall<responses.GoogleLogin>('post', '/auth/google', {}, { credential, quizId });

Check failure on line 144 in src/hooks/useApiClient.tsx

View workflow job for this annotation

GitHub Actions / build

Namespace '"/home/runner/work/frontend-native-app/frontend-native-app/src/api/responses"' has no exported member 'GoogleLogin'.

return response.data;
}

async function postRefresh() {
const refreshToken = await AsyncStorage.getItem('refreshToken');

try {
const response = await apiCall<{ access_token: string }>(
'post',
'/refresh',
{
'X-Session-Id': sessionId,
'Cookie': 'refreshToken=' + refreshToken,
},
);
const response = await apiCall<{ access_token: string }>('post', '/refresh', {
'X-Session-Id': sessionId,
Cookie: 'refreshToken=' + refreshToken,
});

// Update the auth token in the store
dispatch(setAuthToken(response.data.access_token));
Expand Down Expand Up @@ -205,13 +198,9 @@
throw new Error('Missing quizId');
}

const response = await apiCall<{ climateEffects: ClimateEffect[] }>(
'get',
'/feed?quizId=' + quizId,
{
'X-Session-Id': sessionId,
}
);
const response = await apiCall<{ climateEffects: ClimateEffect[] }>('get', '/feed?quizId=' + quizId, {
'X-Session-Id': sessionId,
});

return response.data.climateEffects;
}
Expand All @@ -225,13 +214,9 @@
throw new Error('Missing quizId');
}

const response = await apiCall<{ solutions: Solution[] }>(
'get',
'/solutions?quizId=' + quizId,
{
'X-Session-Id': sessionId,
}
);
const response = await apiCall<{ solutions: Solution[] }>('get', '/solutions?quizId=' + quizId, {
'X-Session-Id': sessionId,
});

return response.data.solutions;
}
Expand All @@ -241,13 +226,9 @@
throw new Error('Missing sessionId');
}

const response = await apiCall<{ myths: Myth[] }>(
'get',
'/myths',
{
'X-Session-Id': sessionId,
},
);
const response = await apiCall<{ myths: Myth[] }>('get', '/myths', {
'X-Session-Id': sessionId,
});

return response.data.myths;
}
Expand All @@ -257,13 +238,9 @@
throw new Error('Missing sessionId');
}

const response = await apiCall<{ myth: Myth }>(
'get',
'/myths/' + mythIri,
{
'X-Session-Id': sessionId,
},
);
const response = await apiCall<{ myth: Myth }>('get', '/myths/' + mythIri, {
'X-Session-Id': sessionId,
});

return response.data.myth;
}
Expand All @@ -274,7 +251,7 @@
'/user-account',
{
'X-Session-Id': sessionId,
'Authorization': 'Bearer ' + user.accessToken,
Authorization: 'Bearer ' + user.accessToken,
},
{ currentPassword, newPassword, confirmPassword }
);
Expand All @@ -286,7 +263,7 @@
'/email',
{
'X-Session-Id': sessionId,
'Authorization': 'Bearer ' + user.accessToken,
Authorization: 'Bearer ' + user.accessToken,
},
{ newEmail, confirmEmail, password }
);
Expand All @@ -298,7 +275,7 @@
'/conversation',
{
'X-Session-Id': sessionId,
'Authorization': 'Bearer ' + user.accessToken,
Authorization: 'Bearer ' + user.accessToken,
},
{ invitedUserName }
);
Expand All @@ -307,27 +284,19 @@
}

async function getAllConversations() {
const response = await apiCall<{ conversations: responses.GetAllConversations[] }>(
'get',
'/conversations',
{
'X-Session-Id': sessionId,
'Authorization': 'Bearer ' + user.accessToken,
},
);
const response = await apiCall<{ conversations: responses.GetAllConversations[] }>('get', '/conversations', {
'X-Session-Id': sessionId,
Authorization: 'Bearer ' + user.accessToken,
});

return response.data;
}

async function deleteConversation(conversationId: string) {
await apiCall(
'delete',
'/conversation/' + conversationId,
{
'X-Session-Id': sessionId,
'Authorization': 'Bearer ' + user.accessToken,
},
);
await apiCall('delete', '/conversation/' + conversationId, {
'X-Session-Id': sessionId,
Authorization: 'Bearer ' + user.accessToken,
});
}

async function putSingleConversation(data: requests.PutSingleConversation) {
Expand All @@ -337,7 +306,7 @@
'/conversation/' + data.conversationId,
{
'X-Session-Id': sessionId,
'Authorization': 'Bearer ' + user.accessToken,
Authorization: 'Bearer ' + user.accessToken,
},
data.updatedConversation
);
Expand All @@ -353,13 +322,9 @@
throw new Error('Missing alignmentScoresId');
}

const response = await apiCall<responses.GetAlignmentScores>(
'get',
'/alignment/' + alignmentScoresId,
{
'X-Session-Id': sessionId,
}
);
const response = await apiCall<responses.GetAlignmentScores>('get', '/alignment/' + alignmentScoresId, {
'X-Session-Id': sessionId,
});

return response.data;
}
Expand All @@ -373,13 +338,9 @@
throw new Error('Missing conversationId');
}

const response = await apiCall<responses.GetSelectedTopics>(
'get',
'/conversation/' + conversationId + '/topics',
{
'X-Session-Id': sessionId,
}
);
const response = await apiCall<responses.GetSelectedTopics>('get', '/conversation/' + conversationId + '/topics', {
'X-Session-Id': sessionId,
});

return response.data;
}
Expand All @@ -393,13 +354,9 @@
throw new Error('Missing impactId');
}

const response = await apiCall<responses.GetSharedImpactDetails>(
'get',
'/alignment/shared-impact/' + impactId,
{
'X-Session-Id': sessionId,
}
);
const response = await apiCall<responses.GetSharedImpactDetails>('get', '/alignment/shared-impact/' + impactId, {
'X-Session-Id': sessionId,
});

return response.data;
}
Expand All @@ -413,13 +370,9 @@
throw new Error('Missing solutionId');
}

const response = await apiCall<responses.GetSharedSolutionDetails>(
'get',
'/alignment/shared-solution/' + solutionId,
{
'X-Session-Id': sessionId,
}
);
const response = await apiCall<responses.GetSharedSolutionDetails>('get', '/alignment/shared-solution/' + solutionId, {
'X-Session-Id': sessionId,
});

return response.data;
}
Expand All @@ -433,6 +386,7 @@
postRegister,
deleteAccount,
postLogin,
postGoogleLogin,
postRefresh,
postPasswordResetLink,

Expand Down
Loading