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

Firebase #1

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ yarn-error.log*
*.njsproj
*.sln
*.sw?

/src/config/development.env.js
/src/config/production.env.js
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"dependencies": {
"core-js": "^2.6.5",
"element-ui": "^2.11.1",
"vue": "^2.6.10"
"firebase": "^6.5.0",
"store": "^2.0.12",
"vue": "^2.6.10",
"vuex": "^3.1.1"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^3.11.0",
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>rent-cost</title>
</head>
Expand Down
24 changes: 24 additions & 0 deletions src/components/Comment.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<div class="comments">
<el-main>
<div v-for="rent in RentData">
<p>{{rent.link}}</p>
<p>{{rent.price}} 元 / 月,{{rent.space_size}} 坪</p>
<p>心得: {{rent.comment}}</p>
</div>
</el-main>
</div>
</template>

<script>
export default {
data: () => ({
rents: {}
}),
computed: {
RentData () {
return this.$store.state.Rent.data
}
}
}
</script>
29 changes: 22 additions & 7 deletions src/components/CostCalculator.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<template>
<div class="cost-calculator">
<el-header>
<h1>租屋虧多少</h1>
</el-header>

<el-main>
<p>當前網站 {{currentUrl}}</p>
<p>坪數{{spaceSize}}</p>

<div class="rent-price-wrapper">
Expand All @@ -22,11 +17,24 @@

<div>每坪數約 {{ calculateAverageCostBySize }} 元</div>
<div>總計裸屋成本約:{{ calculateTotalCost }} </div>

<el-input
type="textarea"
:autosize="{ minRows: 2, maxRows: 4}"
placeholder="請輸入內容"
v-model="comment">
</el-input>

<el-row>
<el-button type="primary" round v-on:click="rentComment">存檔</el-button>
</el-row>
</el-main>
</div>
</template>

<script>
import { insertRentData } from "../utils/RentModel"

const additionGroupOptions = ["沒有洗衣機", "沒有床墊", "沒有網路", "沒有飲水機", "沒有代收垃圾"]

export default {
Expand All @@ -42,7 +50,8 @@ export default {
},
currentUrl: null,
rentPrice: 0,
spaceSize: 0
spaceSize: 0,
comment: ""
}),
mounted() {
this.fetchRentPrice()
Expand Down Expand Up @@ -81,7 +90,6 @@ export default {
let integerPrice = parseInt(priceText.replace(",", ""))
let integerSpaceSize = spaceSizeText.match(/\d+/)[0]


setRentPrice(integerPrice)
setSpaceSize(integerSpaceSize)
});
Expand All @@ -92,6 +100,13 @@ export default {

setCurrentUrl(tabs[0].url)
});
},
rentComment() {
const { currentUrl, rentPrice, spaceSize, comment } = this

insertRentData(currentUrl, rentPrice, spaceSize, comment)

alert("OK")
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/config/config.env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import DevelopmentConfig from "./development.env";
import ProductionConfig from "./production.env";

const envConfigs = {
development: DevelopmentConfig,
production: ProductionConfig
};

const Config = process.env.NODE_ENV === "production" ? envConfigs.production : envConfigs.development;

export default Config;
11 changes: 11 additions & 0 deletions src/config/development.env.js.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const DevelopmentConfig = {
apiKey: "xxx",
authDomain: "xxxx",
databaseURL: "xxx",
projectId: "rent-cost-xxxx",
storageBucket: "xxx",
messagingSenderId: "xxx",
appId: "xxx"
};

export default DevelopmentConfig;
11 changes: 11 additions & 0 deletions src/config/production.env.js.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const ProductionConfig = {
apiKey: "xxx",
authDomain: "xxxx",
databaseURL: "xxx",
projectId: "rent-cost-xxxx",
storageBucket: "xxx",
messagingSenderId: "xxx",
appId: "xxx"
};

export default ProductionConfig;
10 changes: 9 additions & 1 deletion src/popup/App/App.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<template>
<div class="main-app">
<el-container>
<el-main>
<div v-if="isShowCalculator">
<cost-calculator></cost-calculator>
<comment></comment>
</div>

<div v-else>
<announcement></announcement>
</div>


</el-main>
</el-container>
</div>
</template>
Expand All @@ -16,16 +21,19 @@

import CostCalculator from "../../components/CostCalculator"
import Announcement from "../../components/Announcement"
import Comment from "../../components/Comment"

const rentHost = "rent.591.com.tw"

export default {
name: 'App',
components: { CostCalculator, Readme },
components: { CostCalculator, Announcement, Comment },
data: () => ({
isShowCalculator: false
}),
created () {
this.$store.dispatch('FETCH_RENTS')

const setIsShowCalculator = (boolean) => { this.isShowCalculator = boolean }

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
Expand Down
2 changes: 1 addition & 1 deletion src/popup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
</noscript>
<div id="app"></div>
</body>
</html>
</html>
4 changes: 4 additions & 0 deletions src/popup/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Vue from "vue";
import App from "./App/App.vue";
import store from '../store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.config.productionTip = false

Vue.use(ElementUI)

new Vue({
el: "#app",
store,
components: { App },
render: h => h(App)
});
12 changes: 12 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Vue from 'vue'
import Vuex from 'vuex'

import modules from './modules'

Vue.use(Vuex)

export default new Vuex.Store({
modules,
// TODO process.env.NODE_ENV
strict: process.env.NODE_ENV !== 'production'
})
29 changes: 29 additions & 0 deletions src/store/modules/Rent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getRents } from "../../utils/RentModel";

const state = {
data: {}
}

const mutations = {
SAVE_RENT (state, rent) {
state.data = Object.assign({}, state.data, rent)
}
}

const actions = {
FETCH_RENTS ({ commit }) {
getRents().then(querySnapshot => {
let rents = querySnapshot.docs.map((rent) => {
return rent.data()
})

commit('SAVE_RENT', rents)
})
}
}

export default {
state,
mutations,
actions
}
14 changes: 14 additions & 0 deletions src/store/modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* The file enables `@/store/index.js` to import all vuex modules
* in a one-shot manner. There should not be any reason to edit this file.
*/

const files = require.context('.', false, /\.js$/)
const modules = {}

files.keys().forEach(key => {
if (key === './index.js') return
modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
})

export default modules
38 changes: 38 additions & 0 deletions src/utils/RentModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Config from "../config/config.env";
import * as firebase from "firebase";
import "firebase/firestore";

const {
apiKey,
appId,
authDomain,
projectId,
databaseURL,
storageBucket,
messagingSenderId,
} = Config.firebase;

firebase.initializeApp({
apiKey: apiKey,
authDomain: authDomain,
projectId: projectId,
databaseURL: databaseURL,
storageBucket: storageBucket,
messagingSenderId: messagingSenderId,
appId: appId
});

let db = firebase.firestore();

export function insertRentData(link, price, space_size, comment) {
db.collection("rents").add({
link: link,
price: price,
space_size: space_size,
comment: comment
})
}

export function getRents() {
return db.collection("rents").get()
}
Loading