# All files / atlas-mobile-ts/src/components/Map/Panels/FilterPanel FilterPanel.tsx

0% Statements 0/63
0% Branches 0/4
0% Functions 0/18
0% Lines 0/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181

     ﻿/* Copyright (C) Click & Push Accessibility, Inc - All Rights Reserved
     * Unauthorized copying of this file, via any medium is strictly prohibited
     * Proprietary and confidential
     * Written and maintained by the Click & Push Development team
     * <dev@clicknpush.ca>, January 2022
     */

    import Slider from '@react-native-community/slider'
    import React, { useEffect, useState } from "react"
    import { Keyboard, Text, TextInput, TouchableOpacity, View } from "react-native"
    import Modal from 'react-native-modal'
    import Select from "react-native-multiple-select"
    import Checkbox from "@react-native-community/checkbox"
    import { SafeAreaView } from 'react-native-safe-area-context'
    import { useLandmarks } from '../../../../hooks/useLandmarks'
    import { lmTypes } from "../../../../utils/GlobalUtils"
    import { IconButton } from "../../../Buttons"
    import { Separator } from "../../../Separator"
    import { FilterLmTypes } from './FilterLmTypes'
    import { FilterMinRating } from './FilterMinRating'

    interface FilterPanelProps {
        setMinLmRating: (min: number) => void,
        setLmFilteredTypes: (type: number[]) => void,
        toggleFilter: (state: boolean) => void,
        minLmRating: number,
        lmFilteredTypes: number[],
        onlyOwned: boolean,
        visible: boolean,
        toggleOnlyOwned: (state: boolean) => void
    }

    /**
     * Modal panel that displays filter controls for the landmarks
     */
    export const FilterPanel: React.FC<FilterPanelProps> = (props) => {
        /**
         * These three state variables are copies of their mapState counterparts, and are kept local to this component. They hold the values the sets user on this panel.
         * They are kept local until "Apply filters is pressed", which will copy their values into their mapState counterparts and trigger the filtering
         * These are used locally instead of the mapState values in order to prevent the entire Map component from re-rendering every time one of these values is changed,
         * greatly improving performance of this component
        */
        const [localMinRating, setLocalMinRating] = useState<number>(props.minLmRating)
        const [localFilterTypes, setLocalFilterTypes] = useState<number[]>(props.lmFilteredTypes)
        const [localOwned, toggleLocalOwned] = useState<boolean>(props.onlyOwned)

        /**
         * Resets all map filter values
         */
        const resetFilters = () => {
            props.setLmFilteredTypes([])
            props.setMinLmRating(0)
            props.toggleOnlyOwned(false)
        }

        /**
         * Resets only local filter values
         */
        const cancelChanges = () => {
            setLocalFilterTypes([])
            setLocalMinRating(0)
            toggleLocalOwned(false)
        }

        /**
         * Sets the map's filter state to the value of the local filter state, clears the local state, closes the panel and refetches the landmarks.
         */
        const applyChanges = () => {
            props.setMinLmRating(localMinRating)
            props.setLmFilteredTypes(localFilterTypes)
            props.toggleOnlyOwned(localOwned)
            props.toggleFilter(false)
            setLocalMinRating(0)
            setLocalFilterTypes([])
            toggleLocalOwned(false)
        }

        /**
         * Sets the local state the map's current filter state. Runs when the panel is opened
         */
        const addExistingFilters = () => {
            toggleLocalOwned(props.onlyOwned)
            setLocalFilterTypes(props.lmFilteredTypes)
            setLocalMinRating(props.minLmRating)
        }

        // useEffects that update local settings based on parent changes (e.g. deleting a chip)
        useEffect(() => {
            setLocalMinRating(props.minLmRating)
        }, [props.minLmRating])

        useEffect(() => {
            setLocalFilterTypes(props.lmFilteredTypes)
        }, [props.lmFilteredTypes])

        useEffect(() => {
            toggleLocalOwned(props.onlyOwned)
        }, [props.onlyOwned])

        useEffect(() => {
            if (props.visible) console.log('[Map]: Filter panel opening')
            else console.log('[Map]: Filter panel closing')
        }, [props.visible])

        /**
         * State that holds keyboard open or close state
         */
         const [keyboardIsOpen, setKeyboardState] = useState<boolean>(false);

        /**
        * Map effects
        */
        useEffect(() => {
        const keyboardHideListener = Keyboard.addListener('keyboardDidHide', () => {
            setKeyboardState(false)
        })
        const keyboardShowListener = Keyboard.addListener('keyboardDidShow', () => {
            setKeyboardState(true)
        })
        return () => {
            keyboardHideListener.remove();
            keyboardShowListener.remove();
        }
        }, [])

        const OwnedFilter: React.FC = () => {
            return (
                <View style={{flexDirection: 'row', alignItems: 'center', marginBottom: 10}} >
                    <Text>Only show my landmarks:</Text>
                    <Checkbox value={localOwned} onValueChange={() => toggleLocalOwned(!localOwned)} boxType="square" style={{marginLeft: 20, width: 25, height: 25, marginBottom:10}} />
                </View>
            )
        }

        const FilterButtons: React.FC = () => {
            return (
                <View style={{marginTop: 20, flexDirection: 'row', alignSelf: 'flex-end'}}>
                    <TouchableOpacity style={{marginRight: 40}} onPress={resetFilters}>
                        <Text>Reset</Text>
                    </TouchableOpacity>
                    <TouchableOpacity onPress={() => applyChanges()}>
                        <Text>Apply</Text>
                    </TouchableOpacity>
                </View>
            )
        }

        return (
            <Modal
                onModalWillShow={addExistingFilters}
                useNativeDriver={true}
                useNativeDriverForBackdrop={true}
                isVisible={props.visible}
                animationIn={'slideInDown'}
                animationOut={'slideOutUp'}
                style={{justifyContent: 'flex-start', height: '100%', width: '100%', margin: 0}}
                swipeDirection={'up'}
                onBackdropPress={() => {
                    if (keyboardIsOpen) {
                        Keyboard.dismiss()
                    }
                    else {
                        cancelChanges();
                        props.toggleFilter(false);
                    }
                }} >
                    <SafeAreaView style={{padding: 20, backgroundColor: 'white'}}>
                        <FilterMinRating
                            localMinRating={localMinRating}
                            setLocalMinRating={setLocalMinRating}
                            cancelChanges={cancelChanges}
                            toggleFilter={props.toggleFilter} />
                        <FilterLmTypes localFilterTypes={localFilterTypes} setLocalFilterTypes={setLocalFilterTypes} />
                        <OwnedFilter />
                        <Separator color='grey' />
                        <FilterButtons />
                    </SafeAreaView>
                </Modal>

        )
    }