
程式碼再利用 - 編輯和建立
重複使用 Employee Form
- 修改 src/components/ListItem.js,點擊 row 就會導向新增的頁面
1 | import { Text, TouchableWithoutFeedback, View } from 'react-native'; |
建立表單 v.s. 編輯表單
- 編輯表單的方式
- 使用者導向編輯表單
- EmployeeFormReducer 是空的
- 表單是空的
- 我們必須要在 FormReducer 預載入 state
- 使用者編輯表單 - 我們不改變任何 employee 的 model
- 使用者送出表單
- 我們從 reducer 儲存資料
可重複使用的表單
- 新增 src/components/EmployeeForm.js,將本來在 EmployeeCreate.js 的三個 cardSection 分離到 EmployeeForm.js
1 | import React, { Component } from 'react'; |
- 修改 src/components/EmployeeCreate.js,引入剛剛分離的 EmployeeForm.js,這邊所使用的
...this.props
是把在 Create 表單的所有 state 都傳入 EmployeeForm
1 | import { Card, CardSection, Button } from "./common"; |
獨立的 Employee 編輯表單
- 新增 src/components/EmployeeEdit.js
1 | import React, { Component } from 'react'; |
- 我們現在要為編輯 Employee 進行客製化,這代表兩件事情
- 我們必須確定這個表單總是和某一個 Employee 一起導向,然後載入那個 Employee 的資料到 Form Reducer,這就是表單一打開的初始值
- 我們要新增另外一個 Action Creator 來更新特定 Employee
從 State 初始化表單
- 修改 src/Router.js 新增 Scene
1 | import EmployeeEdit from './components/EmployeeEdit'; |
- 修改 src/components/ListItem.js 的 Action,點擊 row 之後會導向編輯 Employee 的頁面
Actions.employeeEdit({ employee: this.props.employee });
- 修改 src/components/EmployeeEdit.js 將預設值載入,並測試按鈕送出的值是否正確
1 | import _ from 'lodash'; |
更新 Firebase 的紀錄
- 在 src/actions/EmployeeActions.js 新增一個新的 Action
1 | export const employeeSave = ({ name, phone, shift, uid }) => { |
- 修改 src/components/EmployeeEdit.js 讓點擊 Button 之後啟動 employeeSave 的 Action
1 | import { employeeUpdate, employeeSave } from "../actions"; |
清除表單屬性
- 在 src/actions/types.js 新增 EMPLOYEE_SAVE_SUCCESS
export const EMPLOYEE_SAVE_SUCCESS = 'employee_save_success';
- 修改 src/actions/EmployeeActions.js 讓畫面返回並清空資料
1 | import { |
- 修改 src/reducers/EmployeeFromReducer.js 讓 Reducer 變回起始值
1 | import { |
在 Employees 傳送訊息
- React-Native-Communications 套件
- 修改 src/components/EmployeeEdit.js,點擊按鈕之後會傳送訊息給該 Employee
1 | import Communications from 'react-native-communications'; |
Modals 作為可重複使用的元件
- Modal 官方文件
- 新增 src/components/common/Confirm.js
1 | import React from 'react'; |
- 在 src/components/common/index.js 新增
export * from './Confirm';
- 設定 Modal 呈現的資料
1 | const Confirm = ({ children, visible, onAccept, onDecline }) => { |
Modal 樣式
- 在 src/components/common/Confirm.js 新增樣式
1 | const Confirm = ({ children, visible, onAccept, onDecline }) => { |
- 在 src/components/EmployeeEdit.js 新增元件
1 | import { Card, CardSection, Button, Confirm } from "./common"; |
Employee 刪除的 Action Creator
- 在 src/components/EmployeeEdit.js 新增 Decline 和 Accept 的 helper
1 | onAccept() { |
- 在 src/actions/EmployeeActions.js 新增 delete 的 function
1 | export const employeeDelete = ({ uid }) => { |
完成 Employee Delete
- 修改 src/components/EmployeeEdit.js 將 Accept function 完成
1 | import { employeeUpdate, employeeSave, employeeDelete } from "../actions"; |
完成課程
