Skip to content

Commit

Permalink
feat(edit): toggle optional columns (#1127)
Browse files Browse the repository at this point in the history
* feat(edit): reducer for optional columns

* feat(edit): toggle for optional column platform in edit

* feat(edit): created toggelcompoent to add additional information for a certain tile

* chore(toggelcolumns): removed unused imports

* feat(toggelcolumn): not fixed controlled compoenent

* fix(togglecolumn): fixed uncontrolled error component

* chore(edit): renamed togglecolumns component

* chore(reducer): changed name of togglecolumn to setcolumn in reducer
  • Loading branch information
vildeopp authored Jul 14, 2023
1 parent e8f2a86 commit a6713f9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
5 changes: 2 additions & 3 deletions next-tavla/src/Admin/scenarios/Edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ToastProvider } from '@entur/alert'
import { FloatingButton } from '@entur/button'
import { StyledLink } from 'Admin/components/StyledLink'
import { ShareTable } from '../ShareTable'
import { ToggleColumns } from '../ToggleColumns'

function Edit({
initialSettings,
Expand All @@ -21,16 +22,14 @@ function Edit({
}) {
const [settings, dispatch] = useReducer(settingsReducer, initialSettings)
const linkUrl = window.location.host + '/' + documentId

return (
<SettingsDispatchContext.Provider value={dispatch}>
<ToastProvider>
<div className={classes.settings}>
<AddTile />
<TilesOverview tiles={settings.tiles} />

<ShareTable text={linkUrl} />

<ToggleColumns tile={settings.tiles[0]} />
<div className={classes.floatingButtonWrapper}>
<FloatingButton
className={classes.saveButton}
Expand Down
16 changes: 16 additions & 0 deletions next-tavla/src/Admin/scenarios/Edit/reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { arrayMove } from '@dnd-kit/sortable'
import { TAnonTiles } from 'Admin/types'
import { clone, xor } from 'lodash'
import { nanoid } from 'nanoid'
import { TColumn } from 'types/column'
import { TSettings, TTheme } from 'types/settings'
import { TQuayTile, TStopPlaceTile, TTile } from 'types/tile'

Expand All @@ -12,6 +13,7 @@ export type Action =
| { type: 'updateTile'; tileIndex: number; tile: TTile }
| { type: 'swapTiles'; oldIndex: number; newIndex: number }
| { type: 'toggleLine'; tileId: string; lineId: string }
| { type: 'setColumn'; tileId: string; column: TColumn; value: boolean }

export function settingsReducer(
settings: TSettings,
Expand Down Expand Up @@ -91,5 +93,19 @@ export function settingsReducer(
},
)
}
case 'setColumn': {
return changeTile<TStopPlaceTile | TQuayTile>(
action.tileId,
(tile) => {
return {
...tile,
columns: {
...tile.columns,
...{ [action.column]: action.value },
},
}
},
)
}
}
}
40 changes: 40 additions & 0 deletions next-tavla/src/Admin/scenarios/ToggleColumns/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Heading3 } from '@entur/typography'
import { useSettingsDispatch } from 'Admin/utils/contexts'
import { DefaultColumns, TColumn } from 'types/column'
import { TTile } from 'types/tile'
import { Switch } from '@entur/form'

function ToggleColumns({ tile }: { tile: TTile }) {
const dispatch = useSettingsDispatch()

const optionalColumns: TColumn[] = ['platform', 'via']

function handleSwitch(column: TColumn, value: boolean) {
dispatch({
type: 'setColumn',
column: column,
value: value,
tileId: tile.uuid,
})
}
const columns = { ...DefaultColumns, ...tile.columns }
return (
<div>
<Heading3>Legg til informasjon</Heading3>

{optionalColumns.map((col) => {
return (
<Switch
key={col}
checked={columns[col]}
onChange={() => handleSwitch(col, !columns[col])}
>
{col}
</Switch>
)
})}
</div>
)
}

export { ToggleColumns }
2 changes: 1 addition & 1 deletion next-tavla/src/Shared/types/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const DefaultColumns: TColumnSettings = {
platform: false,
situations: true,
time: true,
via: true,
via: false,
} as const

export type TColumnSize = { type: TColumn; size: number }

0 comments on commit a6713f9

Please sign in to comment.