1- import { useEffect , useState } from 'react' ;
2- import { Heading } from '../Heading/Heading' ;
31import './Settings.css' ;
4-
5- // message view option type ex : OLD/NEW
6- export type MsgViewOptionType = {
7- name : string ;
8- viewType : string ;
9- isEnabled : boolean ;
10- } ;
11-
12- // settings data type to be used to store in local storage
13- export type SettingsDataType = {
14- messageView : string ;
15- } ;
2+ import { useContext } from 'react' ;
3+ import { Heading } from '../Heading/Heading' ;
4+ import { ModalContext } from '../../../context/ModalContext' ;
5+ import { SettingsContext } from '../../../context/SettingsContext' ;
166
177export function Settings ( ) {
188 // heading of the page
@@ -21,129 +11,57 @@ export function Settings() {
2111 // description of the page
2212 const description = 'Define how you want to enable/disable components' ;
2313
24- // available message view options
25- const msgViewOptions = [
26- {
27- name : 'Old message view' ,
28- viewType : 'OLD' ,
29- isEnabled : true ,
30- } ,
31- {
32- name : 'New message view' ,
33- viewType : 'NEW' ,
34- isEnabled : true ,
35- } ,
36- ] ;
37-
38- // state to handle current message view type selected to show in chat screen
39- const [ msgViewSelected , setMsgViewSelected ] = useState < MsgViewOptionType > (
40- msgViewOptions [ 0 ] ,
41- ) ;
42-
43- const getSettingsFromLocalStorage = ( ) => {
44- // fetch settings data from local storage
45- const settingsData = localStorage . getItem ( 'settings' ) ;
46- // return data if found else return null
47- return settingsData ? JSON . parse ( settingsData ) : null ;
48- } ;
49-
50- const configureMsgView = ( ) => {
51- // fetch message view type from local storage
52- const settingsData : SettingsDataType | null =
53- getSettingsFromLocalStorage ( ) ;
54- // if message view found in local storage
55- if ( settingsData && settingsData . messageView ) {
56- // filter out the message type selected from available options
57- const msgViewType = msgViewOptions . filter (
58- ( m ) => m . viewType === settingsData . messageView ,
59- ) ;
60- // set the selected view type
61- setMsgViewSelected (
62- msgViewType . length ? msgViewType [ 0 ] : msgViewOptions [ 0 ] ,
63- ) ;
64- }
65- } ;
66-
67- const updateSettingsInLocalStorage = ( data : SettingsDataType ) => {
68- // convert JSON data of settings into string
69- const settingsData = JSON . stringify ( data ) ;
70- // save the data into local storage
71- localStorage . setItem ( 'settings' , settingsData ) ;
72- } ;
73-
74- const updateMsgView = ( msgView : MsgViewOptionType ) => {
75- // get the view type from selected message view type object
76- const { viewType } = msgView ;
77-
78- // update local state of message view type selected
79- setMsgViewSelected ( msgView ) ;
80-
81- // fetch local storage data for settings
82- const settingsData : SettingsDataType | null =
83- getSettingsFromLocalStorage ( ) ;
84-
85- // update the property of message view
86- if ( settingsData ) {
87- settingsData . messageView = viewType ;
88- }
89-
90- // create a updated object based on the data
91- const updatedSettings = settingsData
92- ? settingsData
93- : {
94- messageView : viewType ,
95- } ;
96-
97- // update the local storage with the new message view type
98- updateSettingsInLocalStorage ( updatedSettings ) ;
99- } ;
100-
101- // on screen load, get the settings data from local storage ad update the states
102- useEffect ( ( ) => {
103- configureMsgView ( ) ;
104- } , [ ] ) ;
14+ const { disabledOptions } = useContext ( ModalContext ) ;
15+ const { msgViewOptions, msgViewSelected, updateMsgView } =
16+ useContext ( SettingsContext ) ;
10517
10618 return (
10719 < div >
10820 { /* heading of the page */ }
10921 < Heading heading = { heading } description = { description } />
11022
111- { /* title and description of the property */ }
112- < div className = "settings-msg-view" >
113- < div className = "mt-2 font-weight-800 msg-view-heading" >
114- Message View
115- </ div >
116- < div className = "font-size-14 msg-view-desc" >
117- Select a view how the message should look like
118- </ div >
119- </ div >
120-
121- { /* radio button options to select the message view type */ }
122- < div >
123- < div className = "settings-option-container" >
124- { msgViewOptions . map ( ( option , index ) => (
125- < div
126- key = { index }
127- className = "radio d-flex mb-3 width-fit"
128- onClick = { ( ) => {
129- updateMsgView ( option ) ;
130- } }
131- >
132- < input
133- type = "radio"
134- value = { option . viewType }
135- checked = {
136- option . viewType === msgViewSelected . viewType
137- }
138- readOnly
139- />
140- < label className = "settings-option radio-label" >
141- { option . name }
142- </ label >
23+ { /* Show message option selection when option is not disabled */ }
24+ { ! disabledOptions . settings . messageView && (
25+ < >
26+ { /* title and description of the property */ }
27+ < div className = "settings-msg-view" >
28+ < div className = "mt-2 font-weight-800 msg-view-heading" >
29+ Message View
30+ </ div >
31+ < div className = "font-size-14 msg-view-desc" >
32+ Select a view how the message should look like
33+ </ div >
34+ </ div >
35+
36+ { /* radio button options to select the message view type */ }
37+ < div >
38+ < div className = "settings-option-container" >
39+ { msgViewOptions . map ( ( option , index ) => (
40+ < div
41+ key = { index }
42+ className = "radio d-flex mb-3 width-fit"
43+ onClick = { ( ) => {
44+ updateMsgView ( option ) ;
45+ } }
46+ >
47+ < input
48+ type = "radio"
49+ value = { option . viewType }
50+ checked = {
51+ option . viewType ===
52+ msgViewSelected . viewType
53+ }
54+ readOnly
55+ />
56+ < label className = "settings-option radio-label" >
57+ { option . name }
58+ </ label >
59+ </ div >
60+ ) ) }
14361 </ div >
144- ) ) }
145- </ div >
146- </ div >
62+ </ div >
63+ </ >
64+ ) }
14765 </ div >
14866 ) ;
14967}
0 commit comments