Skip to content

Commit

Permalink
fixed api/utils and added couple of features
Browse files Browse the repository at this point in the history
  • Loading branch information
mo9a7i committed Oct 4, 2022
1 parent c7caef1 commit df63b40
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 64 deletions.
61 changes: 12 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# js_nuxt_foursquare_bot_settings

## Initial Configs

- Enabled ?
This sets weather the bot should do actions for this user or not, in general.

- Auto Check-in
This should be a separated filed set, if set yes, a list of venues should be selected, then, a crontab setting should be set.





## Build Setup

```bash
Expand All @@ -18,52 +30,3 @@ $ npm run generate
```

For detailed explanation on how things work, check out the [documentation](https://nuxtjs.org).

## Special Directories

You can create the following extra directories, some of which have special behaviors. Only `pages` is required; you can delete them if you don't want to use their functionality.

### `assets`

The assets directory contains your uncompiled assets such as Stylus or Sass files, images, or fonts.

More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/assets).

### `components`

The components directory contains your Vue.js components. Components make up the different parts of your page and can be reused and imported into your pages, layouts and even other components.

More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/components).

### `layouts`

Layouts are a great help when you want to change the look and feel of your Nuxt app, whether you want to include a sidebar or have distinct layouts for mobile and desktop.

More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/layouts).


### `pages`

This directory contains your application views and routes. Nuxt will read all the `*.vue` files inside this directory and setup Vue Router automatically.

More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/get-started/routing).

### `plugins`

The plugins directory contains JavaScript plugins that you want to run before instantiating the root Vue.js Application. This is the place to add Vue plugins and to inject functions or constants. Every time you need to use `Vue.use()`, you should create a file in `plugins/` and add its path to plugins in `nuxt.config.js`.

More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/plugins).

### `static`

This directory contains your static files. Each file inside this directory is mapped to `/`.

Example: `/static/robots.txt` is mapped as `/robots.txt`.

More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/static).

### `store`

This directory contains your Vuex store files. Creating a file in this directory automatically activates Vuex.

More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/store).
19 changes: 17 additions & 2 deletions components/Header.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
<template>
<header class="p-5 bg-light">
<div class="container">
<a href="/">Logo</a>
<div class="row justify-content-between">
<div class="col">
<a href="/">Logo</a>
This is header section
</div>
<div class="col">
<button class="btn btn-danger" @click="logout">Log Out</button>
</div>
</div>

</div>
</header>
</template>

<script>
export default {
methods:{
async logout() {
await this.$auth.logout().catch(e => {
console.log('logged out with error', e)
})
console.log('logged out')
}
}
}
</script>

Expand Down
85 changes: 81 additions & 4 deletions components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,36 @@
<div class="container">
<div class="row">
<div class="col">
<div class="">
<h1>Welcome {{ this.$auth.user.user.firstName }}</h1>
<div class="mb-2">
<h1>Welcome {{ user?.firstName }}</h1>
<small class="text-muted">This is your settings page as read from the database, your changes here will be reflectd in the next day</small>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="card card-body border-0 shadow-sm">
<div class="">
<div class="form-check form-switch ps-0">
<label class="form-check-label" for="flexSwitchEnableBot">Bot Enabled ?</label>
<div class="float-end">
<span :class="`spinner-border spinner-border-sm ${bot_enabled_spinner}`" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>
<input class="form-check-input" type="checkbox" v-model="enabled_bot" role="switch" id="flexSwitchEnableBot" />
</div>

</div>

<div class="form-check form-switch ps-0">
<label class="form-check-label" for="flexSwitchEnableAutoCheckIn">Auto Check In ?</label>
<div class="float-end">
<span :class="`spinner-border spinner-border-sm ${auto_check_in_enabled_spinner}`" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>
<input class="form-check-input" type="checkbox" v-model="enabled_auto_checki_in" role="switch" id="flexSwitchEnableAutoCheckIn" />
</div>

</div>
</div>
</div>
</div>
</div>
Expand All @@ -15,20 +43,69 @@

<script>
export default {
data(){
return {
user: null,
enabled_bot: false,
bot_enabled_spinner: '',
enabled_auto_checki_in: false,
auto_check_in_enabled_spinner: '',
}
},
mounted() {
if(this.$auth.user?.user?.firstName){
this.user = this.$auth.user.user;
}
console.log("mounted");
this.saveUser();
},
watch: {
enabled_bot: async function(val, oldVal) {
this.bot_enabled_spinner = '';
console.log(val);
try {
let toggle = await this.$MFO_UTILS.setBotStatus(val);
} catch (error) {
console.log(error)
}

this.bot_enabled_spinner = 'd-none';
},
enabled_auto_checki_in: async function(val, oldVal) {
this.auto_check_in_enabled_spinner = '';
console.log(val);
try {
let toggle = await this.$MFO_UTILS.setAutoCheckinStatus(val);
} catch (error) {
console.log(error)
}

this.auto_check_in_enabled_spinner = 'd-none';
}
},

methods: {
async saveUser() {
//console.log(this.$auth.strategy.token.get());
//console.log(this.$auth.user)
//return;
//console.log(await this.$fire.firestore.collection('users').get());
const saveUser = await this.$myUtils.createUserInFirebase();
const saveUser = await this.$MFO_UTILS.createUserInFirebase();

// get user configs
const userConfigs = await this.$myUtils.getConfigsFromFirebase();
const userConfigs = await this.$MFO_UTILS.getConfigsFromFirebase();
if(userConfigs){
if(userConfigs?.enabled == true){
this.enabled_bot = true;
}

if(userConfigs?.settings?.auto_check_in?.enabled == true){
this.enabled_auto_checki_in = true;
}

this.auto_check_in_enabled_spinner = 'd-none';
this.bot_enabled_spinner = 'd-none';
}
console.log(userConfigs)

},
Expand Down
13 changes: 11 additions & 2 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {

plugins: [
{src: '~/plugins/bootstrap.client.js'},
{src: '~/plugins/utils.js'},

],

components: true,
Expand Down Expand Up @@ -96,7 +96,16 @@ export default {
userInfo: "http://localhost:3000/api/swarm/users/self"
},
}
}
},
redirect: {
login: '/login',
logout: '/',
callback: '/login',
home: '/'
},
plugins:[
{src: '~/plugins/utils.js'},
]
},

build: {
Expand Down
1 change: 0 additions & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<script>
export default {
name: 'IndexPage',
auth: 'guest'
}
</script>
6 changes: 3 additions & 3 deletions plugins/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import myUtils from '~/utils/utils';
import MFO_UTILS from '~/utils/utils';

export default (ctx, inject) => {
// inject the repository in the context (ctx.app.$repository)
// And in the Vue instances (this.$repository in your components)
const utilsWithAxios = myUtils(ctx.$firebase)
inject('myUtils', utilsWithAxios())
const LOCAL_MFO_UTILS = MFO_UTILS(ctx.$fire, ctx.$auth)
inject('MFO_UTILS', LOCAL_MFO_UTILS())
}
33 changes: 30 additions & 3 deletions utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// * with help of Alexander Lichter, Apr 18, 2020, https://blog.lichter.io/posts/nuxt-api-call-organization-and-decoupling/
export default $firebase => () => ({
export default ($firebase, $auth) => () => ({

async createUserInFirebase() {
const userRef = $firebase.firestore.collection("users").doc($auth.user.user.id);
console.log($auth);
console.log($firebase);
const userRef = $firebase.firestore.collection("users").doc($auth.user.id);

try {
await userRef.set({
Expand All @@ -23,7 +25,7 @@ export default $firebase => () => ({
const configsRef = $firebase.firestore.collection("configs").doc($auth.user.user.id);

try {
userConfigs = await configsRef.get();
const userConfigs = await configsRef.get();
//console.log(userConfigs);
if (!userConfigs.exists) {
console.log('No such document!');
Expand All @@ -43,6 +45,31 @@ export default $firebase => () => ({

},

async setBotStatus(value){
try {
const configsRef = $firebase.firestore.collection("configs").doc($auth.user.user.id);
await configsRef.update({enabled: value});
return true;
} catch (error) {
console.log(error);
return false;
}
},

async setAutoCheckinStatus(value){
try {
const configsRef = $firebase.firestore.collection("configs").doc($auth.user.user.id);
await configsRef.update({settings: {
auto_check_in: {
enabled: value
}
} });
return true;
} catch (error) {
console.log(error);
return false;
}
}



Expand Down

0 comments on commit df63b40

Please sign in to comment.