Skip to content

Commit

Permalink
Merge pull request #3247 from andrzejewsky/feature/3095-refactoring-u…
Browse files Browse the repository at this point in the history
…ser-vuex-module

Refactoring user vuex module
  • Loading branch information
patzick authored Jul 23, 2019
2 parents 49c8e4a + 32a23e2 commit 76211f7
Show file tree
Hide file tree
Showing 19 changed files with 483 additions and 294 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added catching of errors when ES is down - @qiqqq
- Added debounce for updating quantity method in the cart - @andrzejewsky (#3191)
- New modules API and rewrite - @filrak, @JCown (#3144)
- Refactored the vuex user module - @andrzejewsky (#3095)

## [1.10.0-rc.2] - UNRELEASED

Expand Down
107 changes: 107 additions & 0 deletions core/data-resolver/UserService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { DataResolver } from './types/DataResolver';
import { UserProfile } from '@vue-storefront/core/modules/user/types/UserProfile'
import { TaskQueue } from '@vue-storefront/core/lib/sync'
import Task from '@vue-storefront/core/lib/sync/types/Task'
import { processLocalizedURLAddress } from '@vue-storefront/core/helpers'
import config from 'config'

const headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}

const resetPassword = async (email: string): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.resetPassword_endpoint),
payload: {
method: 'POST',
mode: 'cors',
headers,
body: JSON.stringify({ email })
}
})

const login = async (username: string, password: string): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.login_endpoint),
payload: {
method: 'POST',
mode: 'cors',
headers,
body: JSON.stringify({ username, password })
}
})

const register = async (customer: DataResolver.Customer, password: string): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.create_endpoint),
payload: {
method: 'POST',
headers,
body: JSON.stringify({ customer, password })
}
})

const updateProfile = async (userProfile: UserProfile): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.me_endpoint),
payload: {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
mode: 'cors',
body: JSON.stringify(userProfile)
}
})

const getProfile = async () =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.me_endpoint),
payload: {
method: 'GET',
mode: 'cors',
headers
}
})

const getOrdersHistory = async (): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.history_endpoint),
payload: {
method: 'GET',
mode: 'cors',
headers
}
})

const changePassword = async (passwordData: DataResolver.PasswordData): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.changePassword_endpoint),
payload: {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(passwordData)
}
})

const invalidateToken = async (refreshToken: string): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(config.users.refresh_endpoint),
payload: {
method: 'POST',
mode: 'cors',
headers,
body: JSON.stringify({ refreshToken })
}
})

export const UserService: DataResolver.UserService = {
resetPassword,
login,
register,
updateProfile,
getProfile,
getOrdersHistory,
changePassword,
invalidateToken
}
4 changes: 3 additions & 1 deletion core/data-resolver/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CategoryService } from './CategoryService';
import { UserService } from './UserService';

export {
CategoryService
CategoryService,
UserService
}
25 changes: 25 additions & 0 deletions core/data-resolver/types/DataResolver.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Category } from 'core/modules/catalog-next/types/Category';
import { UserProfile } from 'core/modules/user/types/UserProfile'
import Task from '@vue-storefront/core/lib/sync/types/Task'

declare namespace DataResolver {

Expand All @@ -15,7 +17,30 @@ declare namespace DataResolver {
excludeFields?: string[]
}

interface Customer {
email: string,
firstname: string,
lastname: string,
addresses: string
}

interface PasswordData {
currentPassword: string,
newPassword: string
}

interface CategoryService {
getCategories: (searchRequest?: CategorySearchOptions) => Promise<Category[]>
}

interface UserService {
resetPassword: (email: string) => Promise<Task>,
login: (username: string, password: string) => Promise<Task>,
register: (customer: Customer, pssword: string) => Promise<Task>,
updateProfile: (userProfile: UserProfile) => Promise<Task>,
getProfile: () => Promise<Task>,
getOrdersHistory: () => Promise<Task>,
changePassword: (passwordData: PasswordData) => Promise<Task>,
invalidateToken: (refreshToken: string) => Promise<Task>
}
}
9 changes: 9 additions & 0 deletions core/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ import config from 'config'
import { sha3_224 } from 'js-sha3'
import { unicodeAlpha, unicodeAlphaNum } from './validators'
import store from '@vue-storefront/core/store'
import { adjustMultistoreApiUrl } from '@vue-storefront/core/lib/multistore'

export const processURLAddress = (url: string = '') => {
if (url.startsWith('/')) return `${config.api.url}${url}`
return url
}

export const processLocalizedURLAddress = (url: string = '') => {
if (config.storeViews.multistore) {
return processURLAddress(adjustMultistoreApiUrl(url))
}

return processURLAddress(url)
}

/**
* Create slugify -> "create-slugify" permalink of text
* @param {String} text
Expand Down
1 change: 1 addition & 0 deletions core/lib/sync/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
task.resultCode = jsonResponse.code
task.code = jsonResponse.code // backward compatibility to fetch()
task.acknowledged = false
task.meta = jsonResponse.meta

if (task.callback_event) {
if (task.callback_event.startsWith('store:')) {
Expand Down
3 changes: 2 additions & 1 deletion core/lib/sync/types/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default interface Task {
transmited: boolean,
transmited_at: Date,
url: string,
is_result_cacheable?: boolean
is_result_cacheable?: boolean,
meta: any
}
Loading

0 comments on commit 76211f7

Please sign in to comment.