Set last account id on account change

This commit is contained in:
Muhsin Keloth 2022-09-05 12:45:38 +05:30
parent 550df2f661
commit 5ac69a54d1
5 changed files with 56 additions and 1 deletions

View file

@ -147,4 +147,13 @@ export default {
deleteAvatar() {
return axios.delete(endPoints('deleteAvatar').url);
},
setActiveAccount({ accountId }) {
const urlData = endPoints('setActiveAccount');
return axios.put(urlData.url, {
profile: {
account_id: accountId,
},
});
},
};

View file

@ -40,6 +40,10 @@ const endPoints = {
deleteAvatar: {
url: '/api/v1/profile/avatar',
},
setActiveAccount: {
url: '/api/v1/profile/set_active_account',
},
};
export default page => {

View file

@ -13,7 +13,7 @@
:key="account.id"
class="account-selector"
>
<a :href="`/app/accounts/${account.id}/dashboard`">
<a @click="onChangeAccount(account.id)">
<fluent-icon
v-if="account.id === accountId"
class="selected--account"
@ -58,5 +58,14 @@ export default {
globalConfig: 'globalConfig/get',
}),
},
methods: {
async onChangeAccount(accountId) {
await this.$store.dispatch('setActiveAccount', {
accountId: accountId,
});
const accountUrl = `/app/accounts/${accountId}/dashboard`;
window.location.href = accountUrl;
},
},
};
</script>

View file

@ -143,6 +143,16 @@ export const actions = {
}
},
// eslint-disable-next-line no-empty-pattern
setActiveAccount: async ({}, { accountId }) => {
// eslint-disable-next-line no-useless-catch
try {
await authAPI.setActiveAccount({ accountId });
} catch (error) {
throw error;
}
},
updateUISettings: async ({ commit }, params) => {
try {
commit(types.SET_CURRENT_USER_UI_SETTINGS, params);

View file

@ -195,4 +195,27 @@ RSpec.describe 'Profile API', type: :request do
end
end
end
describe 'PUT /api/v1/profile/set_active_account' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/profile/set_active_account'
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, password: 'Test123!', account: account, role: :agent) }
it 'updates the last active account id' do
put '/api/v1/profile/set_active_account',
params: { profile: { account_id: account.id } },
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
end
end
end
end