Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FeatureSwitchProvider and use it for 'show Gu suppliers' functionality #149

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions newswires/app/controllers/QueryController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import play.api.mvc.{
Request
}
import play.api.{Configuration, Logging}
import service.FeatureSwitchProvider

class QueryController(
val controllerComponents: ControllerComponents,
Expand Down Expand Up @@ -42,13 +43,19 @@ class QueryController(
): Action[AnyContent] = apiAuthAction { request: UserRequest[AnyContent] =>
val bucket = request.getQueryString("bucket").flatMap(SearchBuckets.get)

val suppliersToExcludeByDefault =
if (FeatureSwitchProvider.ShowGuSuppliers.isOn) List("GuReuters", "GuAP")
else Nil

val queryParams = SearchParams(
text = maybeFreeTextQuery,
keywordIncl = maybeKeywords.map(_.split(",").toList).getOrElse(Nil),
keywordExcl = paramToList(request, "keywordsExcl"),
suppliersIncl = suppliers,
suppliersExcl =
request.queryString.get("supplierExcl").map(_.toList).getOrElse(Nil),
suppliersExcl = request.queryString
.get("supplierExcl")
.map(_.toList)
.getOrElse(Nil) ++ suppliersToExcludeByDefault,
subjectsIncl = subjects,
subjectsExcl = subjectsExcl
)
Expand Down
6 changes: 4 additions & 2 deletions newswires/app/controllers/ViteController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import play.api.libs.ws.WSClient
import play.api.mvc._
import play.api.{Configuration, Mode}
import play.filters.csrf.CSRFAddToken
import service.FeatureSwitchProvider
import service.FeatureSwitchProvider.FeatureSwitch
import views.html.helper.CSRF

import java.net.URL
import scala.concurrent.{ExecutionContext, Future}
import scala.io.Source

case class ClientConfig(
suppliersToExclude: List[String]
switches: Map[String, Boolean]
)

object ClientConfig {
Expand Down Expand Up @@ -61,7 +63,7 @@ class ViteController(
def injectClientConfig(body: String): String = {
val config =
views.html.fragments.clientConfig(
ClientConfig(List("GuReuters", "GuAP"))
ClientConfig(FeatureSwitchProvider.clientSideSwitchStates)
)

body.replace(
Expand Down
34 changes: 34 additions & 0 deletions newswires/app/service/FeatureSwitchProvider.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package service

import play.api.libs.json.{Json, OFormat}

sealed trait SwitchState
case object On extends SwitchState
case object Off extends SwitchState

object FeatureSwitchProvider {

case class FeatureSwitch(
name: String,
description: String,
exposeToClient: Boolean = false,
private val safeState: SwitchState
) {
def isOn: Boolean =
safeState == On // currently we're only using safeState to determine state
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea is that at a later stage we'd turn FeatureSwitchProvider into a class or something similar, which could then take a set of overrides which would determine isOn for a given switch.

}

val ShowGuSuppliers: FeatureSwitch =
FeatureSwitch(
name = "ShowGuSuppliers",
safeState = Off,
description = "Show suppliers from the Guardian",
exposeToClient = true
)

private val switches = List(
ShowGuSuppliers
)
def clientSideSwitchStates: Map[String, Boolean] =
switches.filter(_.exposeToClient).map(s => s.name -> s.isOn).toMap
}
1 change: 0 additions & 1 deletion newswires/client/src/context/SearchContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { act, render } from '@testing-library/react';
import type React from 'react';
import { flushPendingPromises } from '../tests/testHelpers.ts';
import type { SearchContextShape } from './SearchContext.tsx';
import { SearchContextProvider, useSearch } from './SearchContext.tsx';
Expand Down
1 change: 0 additions & 1 deletion newswires/client/src/context/SearchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useContext,
useEffect,
useReducer,
useRef,
useState,
} from 'react';
import { z } from 'zod';
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const SUPPLIERS_TO_EXCLUDE = window.configuration.suppliersToExclude;
export const SUPPLIERS_TO_EXCLUDE = window.configuration.switches
.ShowGuSuppliers
? []
: ['GUAP', 'GUREUTERS'];
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
interface FeatureSwitches {
ShowGuSuppliers: boolean;
}

declare global {
/* ~ Here, declare things that go in the global namespace, or augment
*~ existing declarations in the global namespace
*/
interface Window {
configuration: {
suppliersToExclude: string[];
switches: FeatureSwitches;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion newswires/client/src/suppliers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const recognisedSuppliers = Object.keys(allSupplierData).filter(
);

export const supplierData = Object.fromEntries(
Object.entries(allSupplierData).filter(([supplier]) =>
Object.entries(allSupplierData).filter(([supplier, _]) =>
recognisedSuppliers.includes(supplier),
),
);
Loading