All files store.ts

100% Statements 125/125
100% Branches 41/41
100% Functions 28/28
100% Lines 124/124

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 3653x 3x 3x 3x                                                                 3x 5x 5x 5x 5x 5x 5x 5x         5x                                                   5x                         5x       4x   4x   4x         5x       3x 8x 2x       1x       3x 9x 2x       1x         1x   1x 1x         1x 1x       3x 3x 1x   2x         10x   10x 55x 55x 30x               25x 4x   25x 25x   25x       10x       15x   15x 1x     14x   14x 3x 11x 1x     14x   14x       1x       3x   3x 3x   3x         2x   2x 1x   1x         1x 6x 3x   3x           1x 6x 2x   2x           1x 4x     1x   1x   1x       1x 4x 3x       1x   1x   1x       19x   19x 99x 59x       19x       11x   11x 62x 19x       11x       4x   4x 2x 2x     2x   2x   2x 4x 4x 4x     2x 2x 2x 2x 2x             80x       26x 58x 12x           11x             11x   11x 11x 11x         2x         2x       2x 2x 2x                  
import { makeObservable, action, observable, computed } from 'mobx'
import Cookies, { CookieSetOptions } from 'universal-cookie'
import { merge } from 'lodash'
import { ServiceInformations, ServiceOptions, Service, ConsentResponse } from './service'
import { Denormalizable, Normalizable } from '@code-202/serializer'
 
export interface PartialStoreOptions {
    customizable?: boolean
    cookie?: {
        name?: string
        path?: string
        domain?: string
        maxAge?: number
        secure?: boolean
    }
}
 
export interface StoreOptions {
    customizable: boolean
    cookie: {
        name: string
        path: string
        domain?: string
        maxAge: number
        secure: boolean
    }
}
 
export interface TypeOptions {
    id: string
    needConsent: boolean
    choice: ConsentResponse
    expanded: boolean
    services: ServiceInformations[]
}
 
export class Store implements Normalizable<StoreNormalized>, Denormalizable<StoreNormalized> {
    public services: Service[] = []
    public noCookie: boolean | undefined = undefined
    public globalConsent: ConsentResponse = 'unknown'
    public dialogIsOpened: boolean = false
    public newServiceSinceLastConsent: boolean = false
    public customizing: boolean = false
    public typesExpanded: string[] = []
    protected _options: StoreOptions
    protected _cookies: Cookies
 
    constructor(options: PartialStoreOptions, cookies?: string) {
        makeObservable(this, {
            services: observable,
            noCookie: observable,
            globalConsent: observable,
            dialogIsOpened: observable,
            newServiceSinceLastConsent: observable,
            customizing: observable,
            typesExpanded: observable,
 
            consents: computed,
            isDeclineAll: computed,
            isAcceptAll: computed,
            nbNeedConcentServices: computed,
            types: computed,
 
            initialize: action,
            toggleDialog: action,
            toggleCustomize: action,
            addService: action,
            accept: action,
            decline: action,
            acceptAll: action,
            declineAll: action,
            toggleType: action,
        })
 
        this._options = merge(
            {
                customizable: false,
                cookie: {
                    name: '_cc',
                    path: '/',
                    maxAge: 365 * 24 * 60 * 60,
                    secure: true
                },
            } as StoreOptions,
            options
        )
 
        this._cookies = new Cookies(cookies)
    }
 
    public initialize (): void {
        this.loadTokenFromCookie()
 
        this.dialogIsOpened = !this.isClosable
 
        this.newServiceSinceLastConsent = this.noCookie === false && this.nbNeedConcentServices > 0
    }
 
    public get isClosable(): boolean
    {
        return this.noCookie !== true && this.nbNeedConcentServices == 0
    }
 
    public get isAcceptAll(): boolean {
        for (const service of this.services) {
            if (service.consent != 'yes') {
                return false
            }
        }
 
        return true
    }
 
    public get isDeclineAll(): boolean {
        for (const service of this.services) {
            if (service.consent != 'no' && service.needConsent) {
                return false
            }
        }
 
        return true
    }
 
    public toggleDialog (): void {
 
        this.dialogIsOpened = !this.dialogIsOpened
 
        if (!this.dialogIsOpened && !this.isClosable) {
            this.dialogIsOpened = true
        }
    }
 
    public toggleCustomize (): void {
        this.globalConsent = 'unknown'
        this.customizing = !this.customizing
    }
 
    public toggleType(type: string): void {
        const index = this.typesExpanded.indexOf(type)
        if (index >= 0) {
            this.typesExpanded.splice(index, 1)
        } else {
            this.typesExpanded.push(type)
        }
    }
 
    public get types (): TypeOptions[] {
        const types: Record<string, TypeOptions> = {}
 
        for (const service of this.services) {
            const key = service.type+''
            if (types[key] === undefined) {
                types[key] = {
                    id: key,
                    needConsent: service.needConsent,
                    choice: service.consent,
                    expanded: this.typesExpanded.indexOf(key) >= 0,
                    services: [ service ]
                }
            } else {
                if (types[key].choice != service.consent) {
                    types[key].choice = 'unknown'
                }
                if (service.needConsent) {
                    types[key].needConsent = true
                }
                types[key].services.push(service)
            }
        }
 
        return Object.values(types)
    }
 
    public addService (options: ServiceOptions): boolean {
        const already = this.findService(options.id)
 
        if (already) {
            return false
        }
 
        const service = new Service(options, this._cookies)
 
        if (this.globalConsent == 'yes' || !service.needConsent) {
            service.accept()
        } else if (this.globalConsent == 'no') {
            service.decline()
        }
 
        this.services.push(service)
 
        return true
    }
 
    public get isCustomizable(): boolean {
        return this._options.customizable
    }
 
    public accept(id: string): void {
        const service = this.findService(id)
 
        if (service) {
            service.accept()
 
            this.saveConsentsInCookie()
        }
    }
 
    public decline(id: string): void {
        const service = this.findService(id)
 
        if (service && service.needConsent) {
            service.decline()
 
            this.saveConsentsInCookie()
        }
    }
 
    public acceptType(type: string): void {
        for (const service of this.services) {
            if (service.type == type) {
                service.accept()
 
                this.saveConsentsInCookie()
            }
        }
    }
 
    public declineType(type: string): void {
        for (const service of this.services) {
            if (service.type == type && service.needConsent) {
                service.decline()
 
                this.saveConsentsInCookie()
            }
        }
    }
 
    public acceptAll(): void {
        for (const service of this.services) {
            service.accept()
        }
 
        this.globalConsent = 'yes'
 
        this.saveConsentsInCookie()
 
        this.dialogIsOpened = false
    }
 
    public declineAll(): void {
        for (const service of this.services) {
            if (service.needConsent) {
                service.decline()
            }
        }
 
        this.globalConsent = 'no'
 
        this.saveConsentsInCookie()
 
        this.dialogIsOpened = false
    }
 
    public get consents (): string[] {
        const consents: string[] = []
 
        for (const service of this.services) {
            if (service.consent == 'yes') {
                consents.push(service.id)
            }
        }
 
        return consents
    }
 
    public get unconsents (): string[] {
        const unconsents: string[] = []
 
        for (const service of this.services) {
            if (service.consent == 'no') {
                unconsents.push(service.id)
            }
        }
 
        return unconsents
    }
 
    protected loadTokenFromCookie (): void {
        const cookie = this._cookies.get(this._options.cookie.name)
 
        if (cookie === undefined) {
            this.noCookie = true
            return
        }
 
        this.noCookie = false
 
        const [c, u] = cookie.split('!')
 
        for (const id of c.split('|')) {
            const service = this.findService(id)
            if (service) {
                service.accept()
            }
        }
        if (u) {
            for (const id of u.split('|')) {
                const service = this.findService(id)
                if (service) {
                    service.decline()
                }
            }
        }
    }
 
    public get nbNeedConcentServices (): number {
        return (this.services.filter((s: Service) => s.needConsent && s.consent == 'unknown')).length
    }
 
    protected findService (id: string): Service | undefined {
        for (const service of this.services) {
            if (service.id === id) {
                return service
            }
        }
    }
 
    protected saveConsentsInCookie (): void {
        const options: CookieSetOptions = {
            path: this._options.cookie.path,
            domain: this._options.cookie.domain,
            maxAge: this._options.cookie.maxAge,
            secure: this._options.cookie.secure,
        }
 
        this._cookies.set(this._options.cookie.name, this.consents.join('|')+'!'+this.unconsents.join('|'), options)
 
        action(() => {
            this.noCookie = false
            this.newServiceSinceLastConsent = this.nbNeedConcentServices > 0
        })()
    }
 
    normalize (): StoreNormalized {
        const data = {
            noCookie: this.noCookie,
            dialogIsOpened: this.dialogIsOpened,
        };
 
        return data
    }
 
    denormalize (data: StoreNormalized) {
        action(() => {
            this.noCookie = data.noCookie
            this.dialogIsOpened = data.dialogIsOpened
        })()
    }
}
 
export interface StoreNormalized {
    noCookie: boolean | undefined
    dialogIsOpened: boolean
}