Arga_Fauzianto

Select

Nov 7th, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.51 KB | Source Code | 0 0
  1. import {
  2. FlatList,
  3. SafeAreaView,
  4. StyleSheet,
  5. Text,
  6. View,
  7. TouchableOpacity,
  8. } from 'react-native';
  9. import React, {useState, useEffect, useMemo} from 'react';
  10. import {AccordionItem, Button, Gap, Header, Input} from '../../components';
  11. import {colors, fonts, getData, useForm} from '../../utils';
  12. import {IcNotFound} from '../../assets';
  13. import axios from 'axios';
  14. import base64 from 'react-native-base64';
  15. import {Dropdown} from 'react-native-element-dropdown';
  16. import {API_HOST} from '../../config';
  17. import Modal from 'react-native-modal';
  18. import {ScrollView} from 'react-native-gesture-handler';
  19. import {useDispatch, useSelector} from 'react-redux';
  20. import {store} from '../../redux/store';
  21. import {editHistory} from '../../redux/reducer/history';
  22.  
  23. // import { TouchableOpacity } from 'react-native-gesture-handler'
  24.  
  25. const DetailHistorySps = ({route}) => {
  26. const [kodeDepo, setKodeDepo] = useState('');
  27. const [depoData, setDepoData] = useState([]);
  28. const [dataDepo, setDataDepo] = useState(null);
  29. const [isFocus, setIsFocus] = useState(false);
  30. const [selectedDepo, setSelectedDepo] = useState(null);
  31.  
  32. // const [filteredHistoryData, setFilteredHistoryData] = useState([]);
  33.  
  34. const getDepo = () => {
  35. let username = 'admin';
  36. let adminPassword = 'Databa53';
  37. const authHeader = 'Basic ' + base64.encode(`${username}:${adminPassword}`);
  38. axios
  39. .get(`${API_HOST.url_staging}/depo`, {
  40. headers: {
  41. Authorization: authHeader,
  42. },
  43. })
  44. .then(res => {
  45. // console.log('data line :', res.data);
  46. let count = Object.keys(res.data).length;
  47. let depoArray = [];
  48. for (let i = 0; i < count; i++) {
  49. depoArray.push({
  50. id: res.data[i].kode_depo,
  51. label: res.data[i].depo,
  52. // total: res.data[i].qty
  53. });
  54. }
  55. setDepoData(depoArray);
  56. setKodeDepo(depoArray[0].id);
  57. setSelectedDepo(depoArray[0].id)
  58. })
  59. .catch(err => {
  60. console.log('gagal', err);
  61. });
  62. }; // mengambil data listing depo
  63.  
  64. useEffect(() => {
  65. getDepo();
  66. }, []);
  67.  
  68. const getEdit = id_stock => {
  69. //get data edit ketika tombol edit history di click
  70.  
  71. setIsModalVisible(true);
  72. let username = 'admin';
  73. let adminPassword = 'Databa53';
  74. const authHeader = 'Basic ' + base64.encode(`${username}:${adminPassword}`);
  75.  
  76. const today = new Date().toISOString().slice(0, 10);
  77.  
  78. axios
  79. .get(`${API_HOST.url_staging}/stock_sps/data_stock/` + id_stock, {
  80. headers: {
  81. Authorization: authHeader,
  82. },
  83. params: {
  84. id_stock,
  85. },
  86. })
  87. .then(res => {
  88. // dispatch({type: 'SET_GET_HISTORY', value: res.data})
  89. console.log('successs gan": ', res.data.data[0]);
  90. dispatch({
  91. type: 'SET_EDIT_HISTORY',
  92. value: {
  93. id_line: res.data.data[0].id_line,
  94. titik_pallet: res.data.data[0].titik_pallet,
  95. total_qty_titik: res.data.data[0].total_qty_titik,
  96. total_qty_line: res.data.data[0].total_qty_line,
  97. qty_add: res.data.data[0].qty_add,
  98. tidak_layak: res.data.data[0].tidak_layak,
  99. rusak_karton: res.data.data[0].rusak_karton,
  100. total_stock: res.data.data[0].total_stock,
  101. },
  102. });
  103. })
  104. .catch(err => console.log(err));
  105. };
  106. const [isModalVisible, setIsModalVisible] = useState(false);
  107.  
  108. const {historyData} = route.params; // mengirim parameter dari screen History
  109.  
  110. const dispatch = useDispatch();
  111.  
  112. const historyEdit = useSelector(state => state.historyEditReducer);
  113.  
  114. console.log('returned', historyEdit);
  115.  
  116.  
  117. //trigger button edit value history
  118. const changeText = (key, value) => {
  119. if (key === 'qty_add' || key === 'total_qty_titik') {
  120. const qtyAdd = parseFloat(value);
  121. const titikPallet = parseFloat(historyEdit.titik_pallet);
  122. const totalQtyLine = parseFloat(historyEdit.total_qty_line);
  123. const totalPallet = parseFloat(historyEdit.total_qty_titik)
  124.  
  125. const totalLine = !isNaN(totalQtyLine) && !isNaN(titikPallet)
  126. ? totalQtyLine * titikPallet
  127. : 0
  128.  
  129. const newTotalQtyTitik = !isNaN(totalPallet)
  130. ? totalLine
  131. : 0;
  132.  
  133. const tidakLayak = parseFloat(historyEdit.tidak_layak) || 0;
  134. const rusakKarton = parseFloat(historyEdit.rusak_karton) || 0;
  135.  
  136. const newTotalStock = newTotalQtyTitik + tidakLayak + rusakKarton + qtyAdd;
  137.  
  138. dispatch({
  139. type: 'SET_EDIT_HISTORY',
  140. value: {
  141. ...historyEdit,
  142. [key]: value,
  143. total_qty_titik: newTotalQtyTitik.toString(),
  144. total_stock: newTotalStock.toString(),
  145. },
  146. });
  147. } else {
  148. dispatch({
  149. type: 'SET_EDIT_HISTORY',
  150. value: {
  151. ...historyEdit,
  152. [key]: value,
  153. },
  154. });
  155. }
  156. }; //trigger button edit value history
  157.  
  158.  
  159. //action button batal
  160. const cancelBtn = () => {
  161. return (
  162. setIsModalVisible(!isModalVisible)
  163. )
  164. }
  165.  
  166.  
  167. const saveEdit = () => {
  168. let username = 'admin';
  169. let adminPassword = 'Databa53';
  170. const authHeader = 'Basic ' + base64.encode(`${username}:${adminPassword}`);
  171. const today = new Date().toISOString().slice(0,10);
  172.  
  173. const formData = new FormData();
  174. formData.append('tanggal', today); // Assuming 'tanggal' is available in historyEdit
  175. formData.append('id_line', historyEdit.id_line);
  176. formData.append('titik_pallet', historyEdit.titik_pallet);
  177.  
  178. axios.post(`${API_HOST.url_staging}/stock_sps`, formData, {
  179. headers: {
  180. Authorization: authHeader,
  181. 'Content-Type': 'application/x-www-form-urlencoded',
  182. },
  183. })
  184. .then(res => {
  185. console.log('berhasil edit: ', res)
  186. })
  187. .catch(err => {
  188. console.log('gagal edit: ', err)
  189. })
  190. }
  191.  
  192.  
  193.  
  194. return (
  195. <>
  196. <Header title="Detail History SPS" type="default" />
  197. <View
  198. style={{
  199. flex: 1,
  200. top: 10,
  201. paddingHorizontal: 12,
  202. backgroundColor: '#FFFFFF',
  203. }}>
  204. <View style={{flex: 1}}>
  205. {kodeDepo === '001' && historyData.length > 0 ? (
  206. <>
  207. <Dropdown
  208. style={styles.dropdownCover}
  209. placeholderStyle={styles.placeholderStyle}
  210. selectedTextStyle={styles.selectedTextStyle}
  211. inputSearchStyle={styles.inputSearchStyle}
  212. search
  213. labelField="label"
  214. valueField="value"
  215. data={depoData}
  216. maxHeight={300}
  217. value={depoData}
  218. placeholder={!isFocus ? 'Pilih Depo' : '....'}
  219. searchPlaceholder="Search..."
  220. onChange={item => {
  221. setSelectedDepo(item.id);
  222. setKodeDepo(item.id);
  223. // setDataDepo(item.id)
  224. }}
  225. itemTextStyle={{
  226. color: colors.secondary,
  227. fontSize: 13,
  228. fontFamily: fonts.primary[200],
  229. }}
  230. />
  231.  
  232. <FlatList
  233. data={
  234. selectedDepo
  235. ? historyData.filter(item => item.id_depo === selectedDepo)
  236. : historyData
  237. }
  238. keyExtractor={(item, index) => index.toString()}
  239. renderItem={({item, index}) => (
  240. <AccordionItem
  241. key={index}
  242. id_line={item.line}
  243. title={item.line}
  244. depo={item.depo}
  245. tanggal={item.tanggal}
  246. sku={item.nama_produk}
  247. qty_pallet={item.titik_pallet}
  248. total={item.total_stock}
  249. rusak_karton={item.rusak_karton}
  250. afkir={item.afkir}
  251. button={
  252. <TouchableOpacity
  253. style={styles.btnEdit}
  254. activeOpacity={0.8}
  255. onPress={() => getEdit(item.id_stock)}>
  256. <Text style={styles.textEdit}>Edit History</Text>
  257. </TouchableOpacity>
  258. } // hasil dari function getEdit ditampilkan di component ini
  259. />
  260. )}
  261. />
  262. </>
  263. ) : (
  264. <FlatList
  265. data={historyData}
  266. keyExtractor={(item, index) => index.toString()}
  267. renderItem={({item, index}) => (
  268. <AccordionItem
  269. key={index}
  270. id_line={item.line}
  271. title={item.line}
  272. depo={item.depo}
  273. tanggal={item.tanggal}
  274. sku={item.nama_produk}
  275. qty_pallet={item.titik_pallet}
  276. total={item.total_stock}
  277. rusak_karton={item.rusak_karton}
  278. afkir={item.afkir}
  279. // button={buttonEdit()}
  280. />
  281. )}
  282. />
  283. )}
  284. </View>
  285.  
  286. <View>
  287. <Modal
  288. isVisible={isModalVisible}
  289. backdropColor="black"
  290. animationIn={'slideInDown'}
  291. animationInTiming={550}
  292. animationOutTiming={350}
  293. animationOut={'slideOutUp'}
  294. backdropOpacity={0.5}
  295. deviceHeight={850}
  296. >
  297. <ScrollView showsVerticalScrollIndicator={false} style={{ flex:1 }}>
  298. <View
  299. style={{flex: 1, borderRadius: 5, backgroundColor: '#FFFF'}}>
  300. <Text
  301. style={{
  302. fontFamily: fonts.primary[300],
  303. fontSize: 16,
  304. left: 20,
  305. top: 16,
  306. }}>
  307. Edit History Anda
  308. </Text>
  309. <Gap height={20} />
  310. <View style={{paddingHorizontal: 16, marginBottom: 25, flex: 1}}>
  311. <Input
  312. label="Line"
  313. value={historyEdit.id_line}
  314. onChangeText={value => changeText('id_line', value)}
  315. disable
  316. />
  317.  
  318. <Gap height={15} />
  319. <Input
  320. label="Titik Pallet"
  321. value={historyEdit.titik_pallet}
  322. onChangeText={value => changeText('titik_pallet', value)}
  323. keyboardType="numeric"
  324. />
  325.  
  326. <Gap height={15} />
  327. <Input
  328. label="Total Qty Line"
  329. value={historyEdit.total_qty_line}
  330. onChangeText={value => changeText('total_qty_line', value)}
  331. disable
  332. />
  333. <Gap height={15} />
  334. <Input
  335. label="Quantity Pallet"
  336. value={historyEdit.total_qty_titik}
  337. disable
  338. />
  339. <Gap height={15} />
  340. <Input
  341. label="Quantity Add"
  342. value={historyEdit.qty_add}
  343. onChangeText={value => changeText('qty_add', value)}
  344. keyboardType="numeric"
  345. />
  346.  
  347.  
  348. <Gap height={15} />
  349. <Input
  350. label="Tidak Layak"
  351. value={historyEdit.tidak_layak}
  352. onChangeText={value => changeText('tidak_layak', value)}
  353. disable
  354. />
  355.  
  356. <Gap height={15} />
  357. <Input
  358. label="Rusak Karton"
  359. value={historyEdit.rusak_karton}
  360. onChangeText={value => changeText('rusak_karton', value)}
  361. disable
  362. />
  363. <Gap height={15} />
  364. <Input
  365. label="Total Semua"
  366. value={historyEdit.total_stock}
  367. onChangeText={value => changeText('total-stock', value)}
  368. // value={total.toString()}
  369. disable
  370. />
  371. <View style={{ flexDirection: 'row', top: 25, marginBottom: 25, justifyContent: 'center'}}>
  372. <TouchableOpacity style={styles.btnCancel}
  373. activeOpacity={0.7}
  374. onPress={cancelBtn}
  375. >
  376. <Text style={styles.textCancel}>Batal</Text>
  377. </TouchableOpacity>
  378. <TouchableOpacity style={styles.btnSimpanEdit} onPress={saveEdit}>
  379. <Text style={styles.textCancel}>Simpan</Text>
  380. </TouchableOpacity>
  381. </View>
  382. </View>
  383. </View>
  384. </ScrollView>
  385. </Modal>
  386. </View>
  387. </View>
  388. </>
  389. );
  390. };
  391.  
  392. export default DetailHistorySps;
  393.  
  394. const styles = StyleSheet.create({
  395. dropdownCover: {
  396. height: 50,
  397. backgroundColor: '#FFFFFF',
  398. borderRadius: 8,
  399. padding: 12,
  400. shadowColor: '#000',
  401. shadowOffset: {
  402. width: 0,
  403. height: 1,
  404. },
  405. borderWidth: 0.4,
  406. shadowOpacity: 0.2,
  407. shadowRadius: 1.41,
  408. elevation: 2,
  409. },
  410.  
  411. selectedTextStyle: {
  412. fontSize: 16,
  413. color: 'blue',
  414. },
  415.  
  416. inputSearchStyle: {
  417. height: 50,
  418. fontSize: 15,
  419. fontFamily: fonts.primary[200],
  420. color: colors.secondary,
  421. borderColor: colors.secondary,
  422. borderRadius: 4,
  423. },
  424.  
  425. selectedTextStyle: {
  426. fontFamily: fonts.primary[200],
  427. fontSize: 14,
  428. color: colors.primary,
  429. },
  430.  
  431. btnEdit: {
  432. backgroundColor: colors.secondary,
  433. justifyContent: 'center',
  434. padding: 12,
  435. width: 120,
  436. borderRadius: 5,
  437. marginLeft: 38,
  438. },
  439.  
  440. textEdit: {
  441. fontSize: 14,
  442. color: '#FFFFFF',
  443. textAlign: 'center',
  444. fontFamily: fonts.primary[300],
  445. },
  446.  
  447. btnCancel: {
  448. padding: 16,
  449. width: '45%',
  450. borderRadius: 5,
  451. backgroundColor: "#F74D4D",
  452. alignItems: 'center'
  453. },
  454. textCancel: {
  455. fontSize: 16,
  456. color: "#FFF",
  457. fontFamily: fonts.primary[300]
  458. },
  459.  
  460. btnSimpanEdit: {
  461. padding: 16,
  462. width: '45%',
  463. borderRadius: 5,
  464. backgroundColor: "#39C822",
  465. alignItems: 'center',
  466. marginLeft: 15
  467. }
  468. });
  469.  
Add Comment
Please, Sign In to add comment