mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 18:35:09 +01:00
Specify and applying naming conventions 📃 for source and store methods
This commit is contained in:
parent
7ce7f4d17d
commit
f0325f2473
@ -7,9 +7,9 @@ class UserActions {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.generateActions(
|
this.generateActions(
|
||||||
'fetchCurrentUser',
|
'fetchCurrentUser',
|
||||||
'receiveCurrentUser',
|
'successFetchCurrentUser',
|
||||||
'logoutCurrentUser',
|
'logoutCurrentUser',
|
||||||
'deleteCurrentUser',
|
'successLogoutCurrentUser',
|
||||||
'currentUserFailed'
|
'currentUserFailed'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
59
js/sources/NAMING_CONVENTIONS.md
Normal file
59
js/sources/NAMING_CONVENTIONS.md
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
# Naming conventions for sources
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
When using alt.js's sources, we don't want the source's method to clash with the store/action's method names.
|
||||||
|
|
||||||
|
While actions will still be named by the following schema:
|
||||||
|
|
||||||
|
```
|
||||||
|
<verb><ObjectToManipulateInTheStore>
|
||||||
|
```
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
|
||||||
|
```
|
||||||
|
fetchCurrentUser
|
||||||
|
logoutCurrentUser
|
||||||
|
fetchApplication
|
||||||
|
refreshApplicationToken
|
||||||
|
```
|
||||||
|
|
||||||
|
we cannot repeat this for a sources' methods as patterns like this would emerge in the stores:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
onFetchCurrentUser(invalidateCache) {
|
||||||
|
this.invalidateCache = invalidateCache;
|
||||||
|
|
||||||
|
if(!this.getInstance().isLoading()) {
|
||||||
|
this.getInstance().fetchCurrentUser(); // does not call a flux "action" but a method in user_source.js - which is confusing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Therefore we're introducing the following naming convention:
|
||||||
|
|
||||||
|
## Rules
|
||||||
|
|
||||||
|
1. All source methods that perform a data lookup of any kind (be it cached or not), are called `lookup<ObjectToManipulateInTheStore>`. As a mnemonic aid, "You *lookup* something in a *source*".
|
||||||
|
2. For all methods that do not fit 1.), we prepend `perform`.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Examples for Rule 1.)
|
||||||
|
*HTTP GET'ing the current User*
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
UserActions.fetchCurrentUser
|
||||||
|
UserStore.onFetchCurrentUser
|
||||||
|
UserSource.lookupCurrentUser
|
||||||
|
```
|
||||||
|
|
||||||
|
### Examples for Rule 2.)
|
||||||
|
*HTTP GET'ing a certain user endpoint, that logs the user out :sad_face:(, as this should not be a GET request anyway)*
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
UserActions.logoutCurrentUser
|
||||||
|
UserStore.onLogoutCurrentUser
|
||||||
|
UserSource.performLogoutCurrentUser
|
||||||
|
```
|
@ -7,7 +7,7 @@ import UserActions from '../actions/user_actions';
|
|||||||
|
|
||||||
|
|
||||||
const UserSource = {
|
const UserSource = {
|
||||||
fetchCurrentUser: {
|
lookupCurrentUser: {
|
||||||
remote() {
|
remote() {
|
||||||
return requests.get('user');
|
return requests.get('user');
|
||||||
},
|
},
|
||||||
@ -15,18 +15,18 @@ const UserSource = {
|
|||||||
local(state) {
|
local(state) {
|
||||||
return state.currentUser && state.currentUser.email ? state : {};
|
return state.currentUser && state.currentUser.email ? state : {};
|
||||||
},
|
},
|
||||||
success: UserActions.receiveCurrentUser,
|
success: UserActions.successFetchCurrentUser,
|
||||||
error: UserActions.currentUserFailed,
|
error: UserActions.currentUserFailed,
|
||||||
shouldFetch(state) {
|
shouldFetch(state) {
|
||||||
return state.invalidateCache || state.currentUser && !state.currentUser.email;
|
return state.invalidateCache || state.currentUser && !state.currentUser.email;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
logoutCurrentUser: {
|
performLogoutCurrentUser: {
|
||||||
remote() {
|
remote() {
|
||||||
return requests.get(ApiUrls.users_logout);
|
return requests.get(ApiUrls.users_logout);
|
||||||
},
|
},
|
||||||
success: UserActions.deleteCurrentUser,
|
success: UserActions.successLogoutCurrentUser,
|
||||||
error: UserActions.currentUserFailed
|
error: UserActions.currentUserFailed
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -20,16 +20,20 @@ class UserStore {
|
|||||||
this.invalidateCache = invalidateCache;
|
this.invalidateCache = invalidateCache;
|
||||||
|
|
||||||
if(!this.getInstance().isLoading()) {
|
if(!this.getInstance().isLoading()) {
|
||||||
this.getInstance().fetchCurrentUser();
|
this.getInstance().lookupCurrentUser();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onReceiveCurrentUser({users: [user]}) {
|
onSuccessFetchCurrentUser({users: [user]}) {
|
||||||
this.invalidateCache = false;
|
this.invalidateCache = false;
|
||||||
this.currentUser = user;
|
this.currentUser = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
onDeleteCurrentUser() {
|
onLogoutCurrentUser() {
|
||||||
|
this.getInstance().performLogoutCurrentUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
onSuccessLogoutCurrentUser() {
|
||||||
this.currentUser = {};
|
this.currentUser = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user