Skip to content

Commit

Permalink
Clear pending survey after save
Browse files Browse the repository at this point in the history
  • Loading branch information
Erikvv committed Jan 17, 2025
1 parent b8c26a6 commit cf961fc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions frontend/src/components/company-survey-v2/survey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const SurveyWithReset: FunctionComponent<{
}

useEffect(() => {
const subscription = watch((value, { name, type }) =>
const subscription = watch((value, {name, type}) =>
localStorage.setItem(localStorageKey, JSON.stringify(value))
)
return () => subscription.unsubscribe()
Expand All @@ -126,6 +126,7 @@ const SurveyWithReset: FunctionComponent<{
try {
const response = await fetch(url, {
method: 'POST',
credentials: "include",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
Expand All @@ -144,6 +145,7 @@ const SurveyWithReset: FunctionComponent<{
return
}

localStorage.removeItem(localStorageKey)
navigate('/bedankt', {
state: {
deeplink: await response.json()
Expand Down Expand Up @@ -302,7 +304,7 @@ const loadFromLocalStorage = (localStorageKey: string): any => {
return null
}

const previous = JSON.parse(previousData)
let previous = JSON.parse(previousData)

for (const tab of previous.tabs) {
// these fields were renamed and are now unknown in the back-end
Expand All @@ -313,7 +315,8 @@ const loadFromLocalStorage = (localStorageKey: string): any => {
try {
const prepared = prepareForSubmit(previous, 'Testproject')
const str = JSON.stringify(prepared)
const surveyObject = surveyFromJson(str)
const surveyObject = surveyFromJson(str).clearIds()
previous = surveyToFormData(JSON.parse(surveyObject.toPrettyJson()))
} catch (e) {
// The goal is to prevent the user from getting stuck due to schema changes by Zenmo.
const shouldContinue = confirm(`Eerder ingevulde gegevens bevatten een fout of zijn niet compleet.
Expand Down
5 changes: 5 additions & 0 deletions zummon/src/commonMain/kotlin/companysurvey/Address.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ data class Address(
*/
public val gridConnectionArray: Array<GridConnection>
get() = gridConnections.toTypedArray()

fun clearIds() = copy(
id = uuid4(),
gridConnections = gridConnections.map { it.clearId() }
)
}
4 changes: 3 additions & 1 deletion zummon/src/commonMain/kotlin/companysurvey/GridConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ data class GridConnection(
val expansionPlans: String = "",
val electrificationPlans: String = "",
val surveyFeedback: String = "",
)
) {
fun clearId() = copy(id = uuid4())
}
10 changes: 10 additions & 0 deletions zummon/src/commonMain/kotlin/companysurvey/Survey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ data class Survey(
}
return prettyJson.encodeToString(Survey.serializer(), this)
}

/**
* When you want to create a new Survey from an existing one.
*/
fun clearIds() = copy(
id = uuid4(),
createdAt = Clock.System.now().roundToMilliseconds(),
addresses = addresses.map { it.clearIds() },
createdBy = null,
)
}

@JsExport
Expand Down

0 comments on commit cf961fc

Please sign in to comment.